AMDPlugin.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. } = require("../ModuleTypeConstants");
  10. const RuntimeGlobals = require("../RuntimeGlobals");
  11. const {
  12. approve,
  13. evaluateToIdentifier,
  14. evaluateToString,
  15. toConstantDependency
  16. } = require("../javascript/JavascriptParserHelpers");
  17. const AMDDefineDependency = require("./AMDDefineDependency");
  18. const AMDDefineDependencyParserPlugin = require("./AMDDefineDependencyParserPlugin");
  19. const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
  20. const AMDRequireContextDependency = require("./AMDRequireContextDependency");
  21. const AMDRequireDependenciesBlockParserPlugin = require("./AMDRequireDependenciesBlockParserPlugin");
  22. const AMDRequireDependency = require("./AMDRequireDependency");
  23. const AMDRequireItemDependency = require("./AMDRequireItemDependency");
  24. const {
  25. AMDDefineRuntimeModule,
  26. AMDOptionsRuntimeModule
  27. } = require("./AMDRuntimeModules");
  28. const ConstDependency = require("./ConstDependency");
  29. const LocalModuleDependency = require("./LocalModuleDependency");
  30. const UnsupportedDependency = require("./UnsupportedDependency");
  31. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  32. /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
  33. /** @typedef {import("../Compiler")} Compiler */
  34. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  35. /** @typedef {import("../javascript/JavascriptParser")} Parser */
  36. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  37. const PLUGIN_NAME = "AMDPlugin";
  38. class AMDPlugin {
  39. /**
  40. * @param {Record<string, any>} amdOptions the AMD options
  41. */
  42. constructor(amdOptions) {
  43. this.amdOptions = amdOptions;
  44. }
  45. /**
  46. * Apply the plugin
  47. * @param {Compiler} compiler the compiler instance
  48. * @returns {void}
  49. */
  50. apply(compiler) {
  51. const amdOptions = this.amdOptions;
  52. compiler.hooks.compilation.tap(
  53. PLUGIN_NAME,
  54. (compilation, { contextModuleFactory, normalModuleFactory }) => {
  55. compilation.dependencyTemplates.set(
  56. AMDRequireDependency,
  57. new AMDRequireDependency.Template()
  58. );
  59. compilation.dependencyFactories.set(
  60. AMDRequireItemDependency,
  61. normalModuleFactory
  62. );
  63. compilation.dependencyTemplates.set(
  64. AMDRequireItemDependency,
  65. new AMDRequireItemDependency.Template()
  66. );
  67. compilation.dependencyTemplates.set(
  68. AMDRequireArrayDependency,
  69. new AMDRequireArrayDependency.Template()
  70. );
  71. compilation.dependencyFactories.set(
  72. AMDRequireContextDependency,
  73. contextModuleFactory
  74. );
  75. compilation.dependencyTemplates.set(
  76. AMDRequireContextDependency,
  77. new AMDRequireContextDependency.Template()
  78. );
  79. compilation.dependencyTemplates.set(
  80. AMDDefineDependency,
  81. new AMDDefineDependency.Template()
  82. );
  83. compilation.dependencyTemplates.set(
  84. UnsupportedDependency,
  85. new UnsupportedDependency.Template()
  86. );
  87. compilation.dependencyTemplates.set(
  88. LocalModuleDependency,
  89. new LocalModuleDependency.Template()
  90. );
  91. compilation.hooks.runtimeRequirementInModule
  92. .for(RuntimeGlobals.amdDefine)
  93. .tap(PLUGIN_NAME, (module, set) => {
  94. set.add(RuntimeGlobals.require);
  95. });
  96. compilation.hooks.runtimeRequirementInModule
  97. .for(RuntimeGlobals.amdOptions)
  98. .tap(PLUGIN_NAME, (module, set) => {
  99. set.add(RuntimeGlobals.requireScope);
  100. });
  101. compilation.hooks.runtimeRequirementInTree
  102. .for(RuntimeGlobals.amdDefine)
  103. .tap(PLUGIN_NAME, (chunk, set) => {
  104. compilation.addRuntimeModule(chunk, new AMDDefineRuntimeModule());
  105. });
  106. compilation.hooks.runtimeRequirementInTree
  107. .for(RuntimeGlobals.amdOptions)
  108. .tap(PLUGIN_NAME, (chunk, set) => {
  109. compilation.addRuntimeModule(
  110. chunk,
  111. new AMDOptionsRuntimeModule(amdOptions)
  112. );
  113. });
  114. /**
  115. * @param {Parser} parser parser parser
  116. * @param {JavascriptParserOptions} parserOptions parserOptions
  117. * @returns {void}
  118. */
  119. const handler = (parser, parserOptions) => {
  120. if (parserOptions.amd !== undefined && !parserOptions.amd) return;
  121. /**
  122. * @param {string} optionExpr option expression
  123. * @param {string} rootName root name
  124. * @param {function(): TODO} getMembers callback
  125. */
  126. const tapOptionsHooks = (optionExpr, rootName, getMembers) => {
  127. parser.hooks.expression
  128. .for(optionExpr)
  129. .tap(
  130. PLUGIN_NAME,
  131. toConstantDependency(parser, RuntimeGlobals.amdOptions, [
  132. RuntimeGlobals.amdOptions
  133. ])
  134. );
  135. parser.hooks.evaluateIdentifier
  136. .for(optionExpr)
  137. .tap(
  138. PLUGIN_NAME,
  139. evaluateToIdentifier(optionExpr, rootName, getMembers, true)
  140. );
  141. parser.hooks.evaluateTypeof
  142. .for(optionExpr)
  143. .tap(PLUGIN_NAME, evaluateToString("object"));
  144. parser.hooks.typeof
  145. .for(optionExpr)
  146. .tap(
  147. PLUGIN_NAME,
  148. toConstantDependency(parser, JSON.stringify("object"))
  149. );
  150. };
  151. new AMDRequireDependenciesBlockParserPlugin(parserOptions).apply(
  152. parser
  153. );
  154. new AMDDefineDependencyParserPlugin(parserOptions).apply(parser);
  155. tapOptionsHooks("define.amd", "define", () => "amd");
  156. tapOptionsHooks("require.amd", "require", () => ["amd"]);
  157. tapOptionsHooks(
  158. "__webpack_amd_options__",
  159. "__webpack_amd_options__",
  160. () => []
  161. );
  162. parser.hooks.expression.for("define").tap(PLUGIN_NAME, expr => {
  163. const dep = new ConstDependency(
  164. RuntimeGlobals.amdDefine,
  165. /** @type {Range} */ (expr.range),
  166. [RuntimeGlobals.amdDefine]
  167. );
  168. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  169. parser.state.module.addPresentationalDependency(dep);
  170. return true;
  171. });
  172. parser.hooks.typeof
  173. .for("define")
  174. .tap(
  175. PLUGIN_NAME,
  176. toConstantDependency(parser, JSON.stringify("function"))
  177. );
  178. parser.hooks.evaluateTypeof
  179. .for("define")
  180. .tap(PLUGIN_NAME, evaluateToString("function"));
  181. parser.hooks.canRename.for("define").tap(PLUGIN_NAME, approve);
  182. parser.hooks.rename.for("define").tap(PLUGIN_NAME, expr => {
  183. const dep = new ConstDependency(
  184. RuntimeGlobals.amdDefine,
  185. /** @type {Range} */ (expr.range),
  186. [RuntimeGlobals.amdDefine]
  187. );
  188. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  189. parser.state.module.addPresentationalDependency(dep);
  190. return false;
  191. });
  192. parser.hooks.typeof
  193. .for("require")
  194. .tap(
  195. PLUGIN_NAME,
  196. toConstantDependency(parser, JSON.stringify("function"))
  197. );
  198. parser.hooks.evaluateTypeof
  199. .for("require")
  200. .tap(PLUGIN_NAME, evaluateToString("function"));
  201. };
  202. normalModuleFactory.hooks.parser
  203. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  204. .tap(PLUGIN_NAME, handler);
  205. normalModuleFactory.hooks.parser
  206. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  207. .tap(PLUGIN_NAME, handler);
  208. }
  209. );
  210. }
  211. }
  212. module.exports = AMDPlugin;