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