ProvidePlugin.js 3.6 KB

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