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 |
|
---|
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, __webpack_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, __webpack_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:
|
---|
71 | "!(XXXmodule = { id: YYY, exports: {}, loaded: false }, XXX = (#).call(XXXmodule.exports, __webpack_require__, XXXmodule.exports, XXXmodule), XXXmodule.loaded = true, XXX === undefined && (XXX = XXXmodule.exports))",
|
---|
72 | requests: [RuntimeGlobals.require, RuntimeGlobals.module]
|
---|
73 | },
|
---|
74 | lo: {
|
---|
75 | definition: "var XXX;",
|
---|
76 | content: "!(XXX = #)",
|
---|
77 | requests: []
|
---|
78 | },
|
---|
79 | lof: {
|
---|
80 | definition: "var XXX, XXXfactory, XXXmodule;",
|
---|
81 | content:
|
---|
82 | "!(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))",
|
---|
83 | requests: [RuntimeGlobals.require, RuntimeGlobals.module]
|
---|
84 | },
|
---|
85 | laf: {
|
---|
86 | definition: "var __WEBPACK_AMD_DEFINE_ARRAY__, XXX, XXXexports;",
|
---|
87 | content:
|
---|
88 | "!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, XXX = (#).apply(XXXexports = {}, __WEBPACK_AMD_DEFINE_ARRAY__), XXX === undefined && (XXX = XXXexports))",
|
---|
89 | requests: []
|
---|
90 | },
|
---|
91 | lao: {
|
---|
92 | definition: "var XXX;",
|
---|
93 | content: "!(#, XXX = #)",
|
---|
94 | requests: []
|
---|
95 | },
|
---|
96 | laof: {
|
---|
97 | definition: "var XXXarray, XXXfactory, XXXexports, XXX;",
|
---|
98 | content: `!(XXXarray = #, XXXfactory = (#),
|
---|
99 | (typeof XXXfactory === 'function' ?
|
---|
100 | ((XXX = XXXfactory.apply(XXXexports = {}, XXXarray)), XXX === undefined && (XXX = XXXexports)) :
|
---|
101 | (XXX = XXXfactory)
|
---|
102 | ))`,
|
---|
103 | requests: []
|
---|
104 | }
|
---|
105 | };
|
---|
106 |
|
---|
107 | class AMDDefineDependency extends NullDependency {
|
---|
108 | constructor(range, arrayRange, functionRange, objectRange, namedModule) {
|
---|
109 | super();
|
---|
110 | this.range = range;
|
---|
111 | this.arrayRange = arrayRange;
|
---|
112 | this.functionRange = functionRange;
|
---|
113 | this.objectRange = objectRange;
|
---|
114 | this.namedModule = namedModule;
|
---|
115 | this.localModule = null;
|
---|
116 | }
|
---|
117 |
|
---|
118 | get type() {
|
---|
119 | return "amd define";
|
---|
120 | }
|
---|
121 |
|
---|
122 | serialize(context) {
|
---|
123 | const { write } = context;
|
---|
124 | write(this.range);
|
---|
125 | write(this.arrayRange);
|
---|
126 | write(this.functionRange);
|
---|
127 | write(this.objectRange);
|
---|
128 | write(this.namedModule);
|
---|
129 | write(this.localModule);
|
---|
130 | super.serialize(context);
|
---|
131 | }
|
---|
132 |
|
---|
133 | deserialize(context) {
|
---|
134 | const { read } = context;
|
---|
135 | this.range = read();
|
---|
136 | this.arrayRange = read();
|
---|
137 | this.functionRange = read();
|
---|
138 | this.objectRange = read();
|
---|
139 | this.namedModule = read();
|
---|
140 | this.localModule = read();
|
---|
141 | super.deserialize(context);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | makeSerializable(
|
---|
146 | AMDDefineDependency,
|
---|
147 | "webpack/lib/dependencies/AMDDefineDependency"
|
---|
148 | );
|
---|
149 |
|
---|
150 | AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
|
---|
151 | NullDependency.Template
|
---|
152 | ) {
|
---|
153 | /**
|
---|
154 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
155 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
156 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
157 | * @returns {void}
|
---|
158 | */
|
---|
159 | apply(dependency, source, { runtimeRequirements }) {
|
---|
160 | const dep = /** @type {AMDDefineDependency} */ (dependency);
|
---|
161 | const branch = this.branch(dep);
|
---|
162 | const { definition, content, requests } = DEFINITIONS[branch];
|
---|
163 | for (const req of requests) {
|
---|
164 | runtimeRequirements.add(req);
|
---|
165 | }
|
---|
166 | this.replace(dep, source, definition, content);
|
---|
167 | }
|
---|
168 |
|
---|
169 | localModuleVar(dependency) {
|
---|
170 | return (
|
---|
171 | dependency.localModule &&
|
---|
172 | dependency.localModule.used &&
|
---|
173 | dependency.localModule.variableName()
|
---|
174 | );
|
---|
175 | }
|
---|
176 |
|
---|
177 | branch(dependency) {
|
---|
178 | const localModuleVar = this.localModuleVar(dependency) ? "l" : "";
|
---|
179 | const arrayRange = dependency.arrayRange ? "a" : "";
|
---|
180 | const objectRange = dependency.objectRange ? "o" : "";
|
---|
181 | const functionRange = dependency.functionRange ? "f" : "";
|
---|
182 | return localModuleVar + arrayRange + objectRange + functionRange;
|
---|
183 | }
|
---|
184 |
|
---|
185 | replace(dependency, source, definition, text) {
|
---|
186 | const localModuleVar = this.localModuleVar(dependency);
|
---|
187 | if (localModuleVar) {
|
---|
188 | text = text.replace(/XXX/g, localModuleVar.replace(/\$/g, "$$$$"));
|
---|
189 | definition = definition.replace(
|
---|
190 | /XXX/g,
|
---|
191 | localModuleVar.replace(/\$/g, "$$$$")
|
---|
192 | );
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (dependency.namedModule) {
|
---|
196 | text = text.replace(/YYY/g, JSON.stringify(dependency.namedModule));
|
---|
197 | }
|
---|
198 |
|
---|
199 | const texts = text.split("#");
|
---|
200 |
|
---|
201 | if (definition) source.insert(0, definition);
|
---|
202 |
|
---|
203 | let current = dependency.range[0];
|
---|
204 | if (dependency.arrayRange) {
|
---|
205 | source.replace(current, dependency.arrayRange[0] - 1, texts.shift());
|
---|
206 | current = dependency.arrayRange[1];
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (dependency.objectRange) {
|
---|
210 | source.replace(current, dependency.objectRange[0] - 1, texts.shift());
|
---|
211 | current = dependency.objectRange[1];
|
---|
212 | } else if (dependency.functionRange) {
|
---|
213 | source.replace(current, dependency.functionRange[0] - 1, texts.shift());
|
---|
214 | current = dependency.functionRange[1];
|
---|
215 | }
|
---|
216 | source.replace(current, dependency.range[1] - 1, texts.shift());
|
---|
217 | if (texts.length > 0) throw new Error("Implementation error");
|
---|
218 | }
|
---|
219 | };
|
---|
220 |
|
---|
221 | module.exports = AMDDefineDependency;
|
---|