AMDDefineDependency.js 8.6 KB

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