AMDPlugin.js 6.2 KB

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