Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AMDPlugin.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const path = require("path");
  7. const AMDRequireDependency = require("./AMDRequireDependency");
  8. const AMDRequireItemDependency = require("./AMDRequireItemDependency");
  9. const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
  10. const AMDRequireContextDependency = require("./AMDRequireContextDependency");
  11. const AMDDefineDependency = require("./AMDDefineDependency");
  12. const UnsupportedDependency = require("./UnsupportedDependency");
  13. const LocalModuleDependency = require("./LocalModuleDependency");
  14. const NullFactory = require("../NullFactory");
  15. const AMDRequireDependenciesBlockParserPlugin = require("./AMDRequireDependenciesBlockParserPlugin");
  16. const AMDDefineDependencyParserPlugin = require("./AMDDefineDependencyParserPlugin");
  17. const AliasPlugin = require("enhanced-resolve/lib/AliasPlugin");
  18. const ParserHelpers = require("../ParserHelpers");
  19. class AMDPlugin {
  20. constructor(options, amdOptions) {
  21. this.amdOptions = amdOptions;
  22. this.options = options;
  23. }
  24. apply(compiler) {
  25. const options = this.options;
  26. const amdOptions = this.amdOptions;
  27. compiler.hooks.compilation.tap(
  28. "AMDPlugin",
  29. (compilation, { contextModuleFactory, normalModuleFactory }) => {
  30. compilation.dependencyFactories.set(
  31. AMDRequireDependency,
  32. new NullFactory()
  33. );
  34. compilation.dependencyTemplates.set(
  35. AMDRequireDependency,
  36. new AMDRequireDependency.Template()
  37. );
  38. compilation.dependencyFactories.set(
  39. AMDRequireItemDependency,
  40. normalModuleFactory
  41. );
  42. compilation.dependencyTemplates.set(
  43. AMDRequireItemDependency,
  44. new AMDRequireItemDependency.Template()
  45. );
  46. compilation.dependencyFactories.set(
  47. AMDRequireArrayDependency,
  48. new NullFactory()
  49. );
  50. compilation.dependencyTemplates.set(
  51. AMDRequireArrayDependency,
  52. new AMDRequireArrayDependency.Template()
  53. );
  54. compilation.dependencyFactories.set(
  55. AMDRequireContextDependency,
  56. contextModuleFactory
  57. );
  58. compilation.dependencyTemplates.set(
  59. AMDRequireContextDependency,
  60. new AMDRequireContextDependency.Template()
  61. );
  62. compilation.dependencyFactories.set(
  63. AMDDefineDependency,
  64. new NullFactory()
  65. );
  66. compilation.dependencyTemplates.set(
  67. AMDDefineDependency,
  68. new AMDDefineDependency.Template()
  69. );
  70. compilation.dependencyFactories.set(
  71. UnsupportedDependency,
  72. new NullFactory()
  73. );
  74. compilation.dependencyTemplates.set(
  75. UnsupportedDependency,
  76. new UnsupportedDependency.Template()
  77. );
  78. compilation.dependencyFactories.set(
  79. LocalModuleDependency,
  80. new NullFactory()
  81. );
  82. compilation.dependencyTemplates.set(
  83. LocalModuleDependency,
  84. new LocalModuleDependency.Template()
  85. );
  86. const handler = (parser, parserOptions) => {
  87. if (parserOptions.amd !== undefined && !parserOptions.amd) return;
  88. const setExpressionToModule = (outerExpr, module) => {
  89. parser.hooks.expression.for(outerExpr).tap("AMDPlugin", expr => {
  90. const dep = new AMDRequireItemDependency(module, expr.range);
  91. dep.userRequest = outerExpr;
  92. dep.loc = expr.loc;
  93. parser.state.current.addDependency(dep);
  94. return true;
  95. });
  96. };
  97. new AMDRequireDependenciesBlockParserPlugin(options).apply(parser);
  98. new AMDDefineDependencyParserPlugin(options).apply(parser);
  99. setExpressionToModule("require.amd", "!!webpack amd options");
  100. setExpressionToModule("define.amd", "!!webpack amd options");
  101. setExpressionToModule("define", "!!webpack amd define");
  102. parser.hooks.expression
  103. .for("__webpack_amd_options__")
  104. .tap("AMDPlugin", () =>
  105. parser.state.current.addVariable(
  106. "__webpack_amd_options__",
  107. JSON.stringify(amdOptions)
  108. )
  109. );
  110. parser.hooks.evaluateTypeof
  111. .for("define.amd")
  112. .tap(
  113. "AMDPlugin",
  114. ParserHelpers.evaluateToString(typeof amdOptions)
  115. );
  116. parser.hooks.evaluateTypeof
  117. .for("require.amd")
  118. .tap(
  119. "AMDPlugin",
  120. ParserHelpers.evaluateToString(typeof amdOptions)
  121. );
  122. parser.hooks.evaluateIdentifier
  123. .for("define.amd")
  124. .tap(
  125. "AMDPlugin",
  126. ParserHelpers.evaluateToIdentifier("define.amd", true)
  127. );
  128. parser.hooks.evaluateIdentifier
  129. .for("require.amd")
  130. .tap(
  131. "AMDPlugin",
  132. ParserHelpers.evaluateToIdentifier("require.amd", true)
  133. );
  134. parser.hooks.typeof
  135. .for("define")
  136. .tap(
  137. "AMDPlugin",
  138. ParserHelpers.toConstantDependency(
  139. parser,
  140. JSON.stringify("function")
  141. )
  142. );
  143. parser.hooks.evaluateTypeof
  144. .for("define")
  145. .tap("AMDPlugin", ParserHelpers.evaluateToString("function"));
  146. parser.hooks.canRename
  147. .for("define")
  148. .tap("AMDPlugin", ParserHelpers.approve);
  149. parser.hooks.rename.for("define").tap("AMDPlugin", expr => {
  150. const dep = new AMDRequireItemDependency(
  151. "!!webpack amd define",
  152. expr.range
  153. );
  154. dep.userRequest = "define";
  155. dep.loc = expr.loc;
  156. parser.state.current.addDependency(dep);
  157. return false;
  158. });
  159. parser.hooks.typeof
  160. .for("require")
  161. .tap(
  162. "AMDPlugin",
  163. ParserHelpers.toConstantDependency(
  164. parser,
  165. JSON.stringify("function")
  166. )
  167. );
  168. parser.hooks.evaluateTypeof
  169. .for("require")
  170. .tap("AMDPlugin", ParserHelpers.evaluateToString("function"));
  171. };
  172. normalModuleFactory.hooks.parser
  173. .for("javascript/auto")
  174. .tap("AMDPlugin", handler);
  175. normalModuleFactory.hooks.parser
  176. .for("javascript/dynamic")
  177. .tap("AMDPlugin", handler);
  178. }
  179. );
  180. compiler.hooks.afterResolvers.tap("AMDPlugin", () => {
  181. compiler.resolverFactory.hooks.resolver
  182. .for("normal")
  183. .tap("AMDPlugin", resolver => {
  184. new AliasPlugin(
  185. "described-resolve",
  186. {
  187. name: "amdefine",
  188. alias: path.join(
  189. __dirname,
  190. "..",
  191. "..",
  192. "buildin",
  193. "amd-define.js"
  194. )
  195. },
  196. "resolve"
  197. ).apply(resolver);
  198. new AliasPlugin(
  199. "described-resolve",
  200. {
  201. name: "webpack amd options",
  202. alias: path.join(
  203. __dirname,
  204. "..",
  205. "..",
  206. "buildin",
  207. "amd-options.js"
  208. )
  209. },
  210. "resolve"
  211. ).apply(resolver);
  212. new AliasPlugin(
  213. "described-resolve",
  214. {
  215. name: "webpack amd define",
  216. alias: path.join(
  217. __dirname,
  218. "..",
  219. "..",
  220. "buildin",
  221. "amd-define.js"
  222. )
  223. },
  224. "resolve"
  225. ).apply(resolver);
  226. });
  227. });
  228. }
  229. }
  230. module.exports = AMDPlugin;