AMDDefineDependency.js 7.4 KB

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