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