AMDDefineDependency.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 makeSerializable = require("../util/makeSerializable");
  8. const NullDependency = require("./NullDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("./LocalModule")} LocalModule */
  11. /** @typedef {import("../Dependency")} Dependency */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  16. /** @type {Record<string, { definition: string, content: string, requests: string[] }>} */
  17. const DEFINITIONS = {
  18. f: {
  19. definition: "var __WEBPACK_AMD_DEFINE_RESULT__;",
  20. content: `!(__WEBPACK_AMD_DEFINE_RESULT__ = (#).call(exports, ${RuntimeGlobals.require}, exports, module),
  21. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`,
  22. requests: [
  23. RuntimeGlobals.require,
  24. RuntimeGlobals.exports,
  25. RuntimeGlobals.module
  26. ]
  27. },
  28. o: {
  29. definition: "",
  30. content: "!(module.exports = #)",
  31. requests: [RuntimeGlobals.module]
  32. },
  33. of: {
  34. definition:
  35. "var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_RESULT__;",
  36. content: `!(__WEBPACK_AMD_DEFINE_FACTORY__ = (#),
  37. __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
  38. (__WEBPACK_AMD_DEFINE_FACTORY__.call(exports, ${RuntimeGlobals.require}, exports, module)) :
  39. __WEBPACK_AMD_DEFINE_FACTORY__),
  40. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`,
  41. requests: [
  42. RuntimeGlobals.require,
  43. RuntimeGlobals.exports,
  44. RuntimeGlobals.module
  45. ]
  46. },
  47. af: {
  48. definition:
  49. "var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;",
  50. content: `!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = (#).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
  51. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`,
  52. requests: [RuntimeGlobals.exports, RuntimeGlobals.module]
  53. },
  54. ao: {
  55. definition: "",
  56. content: "!(#, module.exports = #)",
  57. requests: [RuntimeGlobals.module]
  58. },
  59. aof: {
  60. definition:
  61. "var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;",
  62. content: `!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_FACTORY__ = (#),
  63. __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
  64. (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
  65. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`,
  66. requests: [RuntimeGlobals.exports, RuntimeGlobals.module]
  67. },
  68. lf: {
  69. definition: "var XXX, XXXmodule;",
  70. content: `!(XXXmodule = { id: YYY, exports: {}, loaded: false }, XXX = (#).call(XXXmodule.exports, ${RuntimeGlobals.require}, XXXmodule.exports, XXXmodule), XXXmodule.loaded = true, XXX === undefined && (XXX = XXXmodule.exports))`,
  71. requests: [RuntimeGlobals.require, RuntimeGlobals.module]
  72. },
  73. lo: {
  74. definition: "var XXX;",
  75. content: "!(XXX = #)",
  76. requests: []
  77. },
  78. lof: {
  79. definition: "var XXX, XXXfactory, XXXmodule;",
  80. content: `!(XXXfactory = (#), (typeof XXXfactory === 'function' ? ((XXXmodule = { id: YYY, exports: {}, loaded: false }), (XXX = XXXfactory.call(XXXmodule.exports, ${RuntimeGlobals.require}, XXXmodule.exports, XXXmodule)), (XXXmodule.loaded = true), XXX === undefined && (XXX = XXXmodule.exports)) : XXX = XXXfactory))`,
  81. requests: [RuntimeGlobals.require, RuntimeGlobals.module]
  82. },
  83. laf: {
  84. definition: "var __WEBPACK_AMD_DEFINE_ARRAY__, XXX, XXXexports;",
  85. content:
  86. "!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, XXX = (#).apply(XXXexports = {}, __WEBPACK_AMD_DEFINE_ARRAY__), XXX === undefined && (XXX = XXXexports))",
  87. requests: []
  88. },
  89. lao: {
  90. definition: "var XXX;",
  91. content: "!(#, XXX = #)",
  92. requests: []
  93. },
  94. laof: {
  95. definition: "var XXXarray, XXXfactory, XXXexports, XXX;",
  96. content: `!(XXXarray = #, XXXfactory = (#),
  97. (typeof XXXfactory === 'function' ?
  98. ((XXX = XXXfactory.apply(XXXexports = {}, XXXarray)), XXX === undefined && (XXX = XXXexports)) :
  99. (XXX = XXXfactory)
  100. ))`,
  101. requests: []
  102. }
  103. };
  104. class AMDDefineDependency extends NullDependency {
  105. /**
  106. * @param {Range} range range
  107. * @param {Range | null} arrayRange array range
  108. * @param {Range | null} functionRange function range
  109. * @param {Range | null} objectRange object range
  110. * @param {string | null} namedModule true, when define is called with a name
  111. */
  112. constructor(range, arrayRange, functionRange, objectRange, namedModule) {
  113. super();
  114. this.range = range;
  115. this.arrayRange = arrayRange;
  116. this.functionRange = functionRange;
  117. this.objectRange = objectRange;
  118. this.namedModule = namedModule;
  119. /** @type {LocalModule | null} */
  120. this.localModule = null;
  121. }
  122. get type() {
  123. return "amd define";
  124. }
  125. /**
  126. * @param {ObjectSerializerContext} context context
  127. */
  128. serialize(context) {
  129. const { write } = context;
  130. write(this.range);
  131. write(this.arrayRange);
  132. write(this.functionRange);
  133. write(this.objectRange);
  134. write(this.namedModule);
  135. write(this.localModule);
  136. super.serialize(context);
  137. }
  138. /**
  139. * @param {ObjectDeserializerContext} context context
  140. */
  141. deserialize(context) {
  142. const { read } = context;
  143. this.range = read();
  144. this.arrayRange = read();
  145. this.functionRange = read();
  146. this.objectRange = read();
  147. this.namedModule = read();
  148. this.localModule = read();
  149. super.deserialize(context);
  150. }
  151. }
  152. makeSerializable(
  153. AMDDefineDependency,
  154. "webpack/lib/dependencies/AMDDefineDependency"
  155. );
  156. AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
  157. NullDependency.Template
  158. ) {
  159. /**
  160. * @param {Dependency} dependency the dependency for which the template should be applied
  161. * @param {ReplaceSource} source the current replace source which can be modified
  162. * @param {DependencyTemplateContext} templateContext the context object
  163. * @returns {void}
  164. */
  165. apply(dependency, source, { runtimeRequirements }) {
  166. const dep = /** @type {AMDDefineDependency} */ (dependency);
  167. const branch = this.branch(dep);
  168. const { definition, content, requests } = DEFINITIONS[branch];
  169. for (const req of requests) {
  170. runtimeRequirements.add(req);
  171. }
  172. this.replace(dep, source, definition, content);
  173. }
  174. /**
  175. * @param {AMDDefineDependency} dependency dependency
  176. * @returns {string | false | null} variable name
  177. */
  178. localModuleVar(dependency) {
  179. return (
  180. dependency.localModule &&
  181. dependency.localModule.used &&
  182. dependency.localModule.variableName()
  183. );
  184. }
  185. /**
  186. * @param {AMDDefineDependency} dependency dependency
  187. * @returns {string} branch
  188. */
  189. branch(dependency) {
  190. const localModuleVar = this.localModuleVar(dependency) ? "l" : "";
  191. const arrayRange = dependency.arrayRange ? "a" : "";
  192. const objectRange = dependency.objectRange ? "o" : "";
  193. const functionRange = dependency.functionRange ? "f" : "";
  194. return localModuleVar + arrayRange + objectRange + functionRange;
  195. }
  196. /**
  197. * @param {AMDDefineDependency} dependency dependency
  198. * @param {ReplaceSource} source source
  199. * @param {string} definition definition
  200. * @param {string} text text
  201. */
  202. replace(dependency, source, definition, text) {
  203. const localModuleVar = this.localModuleVar(dependency);
  204. if (localModuleVar) {
  205. text = text.replace(/XXX/g, localModuleVar.replace(/\$/g, "$$$$"));
  206. definition = definition.replace(
  207. /XXX/g,
  208. localModuleVar.replace(/\$/g, "$$$$")
  209. );
  210. }
  211. if (dependency.namedModule) {
  212. text = text.replace(/YYY/g, JSON.stringify(dependency.namedModule));
  213. }
  214. const texts = text.split("#");
  215. if (definition) source.insert(0, definition);
  216. let current = dependency.range[0];
  217. if (dependency.arrayRange) {
  218. source.replace(
  219. current,
  220. dependency.arrayRange[0] - 1,
  221. /** @type {string} */ (texts.shift())
  222. );
  223. current = dependency.arrayRange[1];
  224. }
  225. if (dependency.objectRange) {
  226. source.replace(
  227. current,
  228. dependency.objectRange[0] - 1,
  229. /** @type {string} */ (texts.shift())
  230. );
  231. current = dependency.objectRange[1];
  232. } else if (dependency.functionRange) {
  233. source.replace(
  234. current,
  235. dependency.functionRange[0] - 1,
  236. /** @type {string} */ (texts.shift())
  237. );
  238. current = dependency.functionRange[1];
  239. }
  240. source.replace(
  241. current,
  242. dependency.range[1] - 1,
  243. /** @type {string} */ (texts.shift())
  244. );
  245. if (texts.length > 0) throw new Error("Implementation error");
  246. }
  247. };
  248. module.exports = AMDDefineDependency;