ProvidePlugin.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ConstDependency = require("./dependencies/ConstDependency");
  7. const ProvidedDependency = require("./dependencies/ProvidedDependency");
  8. const { approve } = require("./javascript/JavascriptParserHelpers");
  9. /** @typedef {import("./Compiler")} Compiler */
  10. class ProvidePlugin {
  11. /**
  12. * @param {Record<string, string | string[]>} definitions the provided identifiers
  13. */
  14. constructor(definitions) {
  15. this.definitions = definitions;
  16. }
  17. /**
  18. * Apply the plugin
  19. * @param {Compiler} compiler the compiler instance
  20. * @returns {void}
  21. */
  22. apply(compiler) {
  23. const definitions = this.definitions;
  24. compiler.hooks.compilation.tap(
  25. "ProvidePlugin",
  26. (compilation, { normalModuleFactory }) => {
  27. compilation.dependencyTemplates.set(
  28. ConstDependency,
  29. new ConstDependency.Template()
  30. );
  31. compilation.dependencyFactories.set(
  32. ProvidedDependency,
  33. normalModuleFactory
  34. );
  35. compilation.dependencyTemplates.set(
  36. ProvidedDependency,
  37. new ProvidedDependency.Template()
  38. );
  39. const handler = (parser, parserOptions) => {
  40. Object.keys(definitions).forEach(name => {
  41. const request = [].concat(definitions[name]);
  42. const splittedName = name.split(".");
  43. if (splittedName.length > 0) {
  44. splittedName.slice(1).forEach((_, i) => {
  45. const name = splittedName.slice(0, i + 1).join(".");
  46. parser.hooks.canRename.for(name).tap("ProvidePlugin", approve);
  47. });
  48. }
  49. parser.hooks.expression.for(name).tap("ProvidePlugin", expr => {
  50. const nameIdentifier = name.includes(".")
  51. ? `__webpack_provided_${name.replace(/\./g, "_dot_")}`
  52. : name;
  53. const dep = new ProvidedDependency(
  54. request[0],
  55. nameIdentifier,
  56. request.slice(1),
  57. expr.range
  58. );
  59. dep.loc = expr.loc;
  60. parser.state.module.addDependency(dep);
  61. return true;
  62. });
  63. parser.hooks.call.for(name).tap("ProvidePlugin", expr => {
  64. const nameIdentifier = name.includes(".")
  65. ? `__webpack_provided_${name.replace(/\./g, "_dot_")}`
  66. : name;
  67. const dep = new ProvidedDependency(
  68. request[0],
  69. nameIdentifier,
  70. request.slice(1),
  71. expr.callee.range
  72. );
  73. dep.loc = expr.callee.loc;
  74. parser.state.module.addDependency(dep);
  75. parser.walkExpressions(expr.arguments);
  76. return true;
  77. });
  78. });
  79. };
  80. normalModuleFactory.hooks.parser
  81. .for("javascript/auto")
  82. .tap("ProvidePlugin", handler);
  83. normalModuleFactory.hooks.parser
  84. .for("javascript/dynamic")
  85. .tap("ProvidePlugin", handler);
  86. normalModuleFactory.hooks.parser
  87. .for("javascript/esm")
  88. .tap("ProvidePlugin", handler);
  89. }
  90. );
  91. }
  92. }
  93. module.exports = ProvidePlugin;