webpack 4.10.1 → 4.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/AmdMainTemplatePlugin.js +3 -1
- package/lib/AsyncDependencyToInitialChunkError.js +4 -4
- package/lib/BannerPlugin.js +3 -1
- package/lib/BasicEvaluatedExpression.js +14 -11
- package/lib/CachePlugin.js +12 -5
- package/lib/CaseSensitiveModulesWarning.js +41 -45
- package/lib/Chunk.js +56 -19
- package/lib/ChunkGroup.js +14 -6
- package/lib/CommentCompilationWarning.js +22 -0
- package/lib/Compilation.js +72 -29
- package/lib/Compiler.js +22 -10
- package/lib/ConstPlugin.js +25 -9
- package/lib/ContextModule.js +88 -36
- package/lib/ContextModuleFactory.js +18 -7
- package/lib/ContextReplacementPlugin.js +14 -7
- package/lib/DefinePlugin.js +15 -6
- package/lib/DependenciesBlock.js +3 -1
- package/lib/DependenciesBlockVariable.js +2 -1
- package/lib/DllPlugin.js +4 -2
- package/lib/DynamicEntryPlugin.js +4 -2
- package/lib/EntryModuleNotFoundError.js +21 -22
- package/lib/ErrorHelpers.js +5 -2
- package/lib/EvalSourceMapDevToolPlugin.js +2 -1
- package/lib/FlagDependencyUsagePlugin.js +11 -5
- package/lib/FunctionModuleTemplatePlugin.js +8 -6
- package/lib/HarmonyLinkingError.js +1 -2
- package/lib/HotModuleReplacement.runtime.js +13 -4
- package/lib/HotModuleReplacementPlugin.js +13 -6
- package/lib/JavascriptGenerator.js +2 -1
- package/lib/JavascriptModulesPlugin.js +4 -9
- package/lib/JsonParser.js +2 -1
- package/lib/LibraryTemplatePlugin.js +2 -1
- package/lib/LoaderOptionsPlugin.js +2 -1
- package/lib/MainTemplate.js +2 -1
- package/lib/Module.js +13 -5
- package/lib/ModuleBuildError.js +22 -12
- package/lib/ModuleDependencyError.js +4 -4
- package/lib/ModuleDependencyWarning.js +4 -4
- package/lib/ModuleError.js +13 -5
- package/lib/ModuleFilenameHelpers.js +20 -8
- package/lib/ModuleNotFoundError.js +23 -26
- package/lib/ModuleParseError.js +27 -14
- package/lib/ModuleWarning.js +13 -7
- package/lib/MultiCompiler.js +19 -7
- package/lib/MultiModule.js +5 -2
- package/lib/NodeStuffPlugin.js +2 -1
- package/lib/NormalModule.js +80 -16
- package/lib/NormalModuleFactory.js +67 -24
- package/lib/OptionsDefaulter.js +7 -3
- package/lib/Parser.js +182 -75
- package/lib/ParserHelpers.js +6 -3
- package/lib/ProgressPlugin.js +17 -2
- package/lib/RawModule.js +4 -2
- package/lib/RecordIdsPlugin.js +77 -9
- package/lib/RemovedPluginError.js +1 -3
- package/lib/RequestShortener.js +15 -6
- package/lib/RuleSet.js +51 -18
- package/lib/RuntimeTemplate.js +21 -10
- package/lib/SourceMapDevToolPlugin.js +4 -3
- package/lib/Stats.js +74 -31
- package/lib/Template.js +7 -4
- package/lib/TemplatedPathPlugin.js +6 -3
- package/lib/UmdMainTemplatePlugin.js +8 -3
- package/lib/UnsupportedFeatureWarning.js +22 -21
- package/lib/WarnCaseSensitiveModulesPlugin.js +2 -1
- package/lib/Watching.js +3 -2
- package/lib/WebpackOptionsApply.js +51 -23
- package/lib/WebpackOptionsDefaulter.js +15 -9
- package/lib/WebpackOptionsValidationError.js +63 -34
- package/lib/debug/ProfilingPlugin.js +11 -2
- package/lib/dependencies/AMDDefineDependencyParserPlugin.js +17 -8
- package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +8 -4
- package/lib/dependencies/HarmonyDetectionParserPlugin.js +4 -2
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +7 -3
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +6 -3
- package/lib/dependencies/ImportParserPlugin.js +42 -11
- package/lib/dependencies/LoaderPlugin.js +12 -7
- package/lib/dependencies/LocalModulesHelpers.js +13 -6
- package/lib/dependencies/RequireContextPlugin.js +4 -2
- package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +8 -4
- package/lib/dependencies/SystemPlugin.js +6 -6
- package/lib/dependencies/WebAssemblyExportImportedDependency.js +29 -0
- package/lib/dependencies/WebAssemblyImportDependency.js +1 -1
- package/lib/formatLocation.js +15 -7
- package/lib/node/NodeMainTemplateAsync.runtime.js +1 -1
- package/lib/node/NodeMainTemplatePlugin.js +6 -3
- package/lib/node/NodeSourcePlugin.js +9 -5
- package/lib/node/NodeWatchFileSystem.js +29 -12
- package/lib/node/ReadFileCompileWasmTemplatePlugin.js +11 -2
- package/lib/optimize/AggressiveSplittingPlugin.js +12 -6
- package/lib/optimize/ConcatenatedModule.js +19 -8
- package/lib/optimize/MergeDuplicateChunksPlugin.js +6 -3
- package/lib/optimize/ModuleConcatenationPlugin.js +23 -10
- package/lib/optimize/OccurrenceOrderPlugin.js +11 -4
- package/lib/optimize/RemoveParentModulesPlugin.js +17 -7
- package/lib/optimize/SideEffectsFlagPlugin.js +14 -3
- package/lib/optimize/SplitChunksPlugin.js +33 -20
- package/lib/performance/AssetsOverSizeLimitWarning.js +7 -7
- package/lib/performance/EntrypointsOverSizeLimitWarning.js +6 -7
- package/lib/performance/NoAsyncChunksWarning.js +5 -5
- package/lib/util/SortableSet.js +5 -2
- package/lib/util/StackedSetMap.js +12 -5
- package/lib/wasm/UnsupportedWebAssemblyFeatureError.js +1 -2
- package/lib/wasm/WasmFinalizeExportsPlugin.js +66 -0
- package/lib/wasm/WasmMainTemplatePlugin.js +87 -43
- package/lib/wasm/WebAssemblyGenerator.js +117 -74
- package/lib/wasm/WebAssemblyJavascriptGenerator.js +92 -49
- package/lib/wasm/WebAssemblyModulesPlugin.js +11 -1
- package/lib/wasm/WebAssemblyParser.js +96 -24
- package/lib/wasm/WebAssemblyUtils.js +17 -6
- package/lib/web/FetchCompileWasmTemplatePlugin.js +13 -1
- package/lib/web/JsonpChunkTemplatePlugin.js +7 -0
- package/lib/web/JsonpMainTemplate.runtime.js +2 -1
- package/lib/web/JsonpMainTemplatePlugin.js +68 -63
- package/lib/webpack.js +2 -1
- package/lib/webworker/WebWorkerMainTemplate.runtime.js +2 -1
- package/package.json +12 -11
- package/schemas/WebpackOptions.json +8 -0
@@ -13,6 +13,11 @@ const { shrinkPaddedLEB128 } = require("@webassemblyjs/wasm-opt");
|
|
13
13
|
const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
|
14
14
|
const { decode } = require("@webassemblyjs/wasm-parser");
|
15
15
|
const t = require("@webassemblyjs/ast");
|
16
|
+
const {
|
17
|
+
moduleContextFromModuleAST
|
18
|
+
} = require("@webassemblyjs/helper-module-context");
|
19
|
+
|
20
|
+
const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
|
16
21
|
|
17
22
|
/** @typedef {import("../Module")} Module */
|
18
23
|
/** @typedef {import("./WebAssemblyUtils").UsedWasmDependency} UsedWasmDependency */
|
@@ -43,20 +48,6 @@ function compose(...fns) {
|
|
43
48
|
}, value => value);
|
44
49
|
}
|
45
50
|
|
46
|
-
// Utility functions
|
47
|
-
|
48
|
-
/**
|
49
|
-
* @param {t.ModuleImport} n the import
|
50
|
-
* @returns {boolean} true, if a global was imported
|
51
|
-
*/
|
52
|
-
const isGlobalImport = n => n.descr.type === "GlobalType";
|
53
|
-
|
54
|
-
/**
|
55
|
-
* @param {t.ModuleImport} n the import
|
56
|
-
* @returns {boolean} true, if a func was imported
|
57
|
-
*/
|
58
|
-
const isFuncImport = n => n.descr.type === "FuncImportDescr";
|
59
|
-
|
60
51
|
// TODO replace with @callback
|
61
52
|
|
62
53
|
/**
|
@@ -73,24 +64,6 @@ const removeStartFunc = state => bin => {
|
|
73
64
|
});
|
74
65
|
};
|
75
66
|
|
76
|
-
/**
|
77
|
-
* Retrieve the start function
|
78
|
-
*
|
79
|
-
* @param {Object} ast - Module's AST
|
80
|
-
* @returns {t.Identifier | undefined} - node if any
|
81
|
-
*/
|
82
|
-
function getStartFuncIndex(ast) {
|
83
|
-
let startAtFuncIndex;
|
84
|
-
|
85
|
-
t.traverse(ast, {
|
86
|
-
Start({ node }) {
|
87
|
-
startAtFuncIndex = node.index;
|
88
|
-
}
|
89
|
-
});
|
90
|
-
|
91
|
-
return startAtFuncIndex;
|
92
|
-
}
|
93
|
-
|
94
67
|
/**
|
95
68
|
* Get imported globals
|
96
69
|
*
|
@@ -102,7 +75,7 @@ function getImportedGlobals(ast) {
|
|
102
75
|
|
103
76
|
t.traverse(ast, {
|
104
77
|
ModuleImport({ node }) {
|
105
|
-
if (
|
78
|
+
if (t.isGlobalType(node.descr) === true) {
|
106
79
|
importedGlobals.push(node);
|
107
80
|
}
|
108
81
|
}
|
@@ -116,7 +89,7 @@ function getCountImportedFunc(ast) {
|
|
116
89
|
|
117
90
|
t.traverse(ast, {
|
118
91
|
ModuleImport({ node }) {
|
119
|
-
if (
|
92
|
+
if (t.isFuncImportDescr(node.descr) === true) {
|
120
93
|
count++;
|
121
94
|
}
|
122
95
|
}
|
@@ -129,7 +102,7 @@ function getCountImportedFunc(ast) {
|
|
129
102
|
* Get next type index
|
130
103
|
*
|
131
104
|
* @param {Object} ast - Module's AST
|
132
|
-
* @returns {t.
|
105
|
+
* @returns {t.Index} - index
|
133
106
|
*/
|
134
107
|
function getNextTypeIndex(ast) {
|
135
108
|
const typeSectionMetadata = t.getSectionMetadata(ast, "type");
|
@@ -150,7 +123,7 @@ function getNextTypeIndex(ast) {
|
|
150
123
|
*
|
151
124
|
* @param {Object} ast - Module's AST
|
152
125
|
* @param {Number} countImportedFunc - number of imported funcs
|
153
|
-
* @returns {t.
|
126
|
+
* @returns {t.Index} - index
|
154
127
|
*/
|
155
128
|
function getNextFuncIndex(ast, countImportedFunc) {
|
156
129
|
const funcSectionMetadata = t.getSectionMetadata(ast, "func");
|
@@ -164,6 +137,27 @@ function getNextFuncIndex(ast, countImportedFunc) {
|
|
164
137
|
return t.indexLiteral(vectorOfSize + countImportedFunc);
|
165
138
|
}
|
166
139
|
|
140
|
+
/**
|
141
|
+
* Create a init instruction for a global
|
142
|
+
* @param {t.GlobalType} globalType the global type
|
143
|
+
* @returns {t.Instruction} init expression
|
144
|
+
*/
|
145
|
+
const createDefaultInitForGlobal = globalType => {
|
146
|
+
if (globalType.valtype[0] === "i") {
|
147
|
+
// create NumberLiteral global initializer
|
148
|
+
return t.objectInstruction("const", globalType.valtype, [
|
149
|
+
t.numberLiteralFromRaw(66)
|
150
|
+
]);
|
151
|
+
} else if (globalType.valtype[0] === "f") {
|
152
|
+
// create FloatLiteral global initializer
|
153
|
+
return t.objectInstruction("const", globalType.valtype, [
|
154
|
+
t.floatLiteral(66, false, false, "66")
|
155
|
+
]);
|
156
|
+
} else {
|
157
|
+
throw new Error("unknown type: " + globalType.valtype);
|
158
|
+
}
|
159
|
+
};
|
160
|
+
|
167
161
|
/**
|
168
162
|
* Rewrite the import globals:
|
169
163
|
* - removes the ModuleImport instruction
|
@@ -178,35 +172,51 @@ function getNextFuncIndex(ast, countImportedFunc) {
|
|
178
172
|
* @returns {ArrayBufferTransform} transform
|
179
173
|
*/
|
180
174
|
const rewriteImportedGlobals = state => bin => {
|
175
|
+
const additionalInitCode = state.additionalInitCode;
|
181
176
|
const newGlobals = [];
|
182
177
|
|
183
178
|
bin = editWithAST(state.ast, bin, {
|
184
179
|
ModuleImport(path) {
|
185
|
-
if (
|
180
|
+
if (t.isGlobalType(path.node.descr) === true) {
|
186
181
|
const globalType = path.node.descr;
|
187
182
|
|
188
183
|
globalType.mutability = "var";
|
189
184
|
|
190
|
-
|
191
|
-
|
192
|
-
if (globalType.valtype[0] === "i") {
|
193
|
-
// create NumberLiteral global initializer
|
194
|
-
init = t.objectInstruction("const", globalType.valtype, [
|
195
|
-
t.numberLiteralFromRaw(0)
|
196
|
-
]);
|
197
|
-
} else if (globalType.valtype[0] === "f") {
|
198
|
-
// create FloatLiteral global initializer
|
199
|
-
init = t.objectInstruction("const", globalType.valtype, [
|
200
|
-
t.floatLiteral(0, false, false, "0")
|
201
|
-
]);
|
202
|
-
} else {
|
203
|
-
throw new Error("unknown type: " + globalType.valtype);
|
204
|
-
}
|
185
|
+
const init = createDefaultInitForGlobal(globalType);
|
205
186
|
|
206
187
|
newGlobals.push(t.global(globalType, [init]));
|
207
188
|
|
208
189
|
path.remove();
|
209
190
|
}
|
191
|
+
},
|
192
|
+
|
193
|
+
// in order to preserve non-imported global's order we need to re-inject
|
194
|
+
// those as well
|
195
|
+
Global(path) {
|
196
|
+
const { node } = path;
|
197
|
+
const [init] = node.init;
|
198
|
+
|
199
|
+
if (init.id === "get_global") {
|
200
|
+
node.globalType.mutability = "var";
|
201
|
+
|
202
|
+
const initialGlobalidx = init.args[0];
|
203
|
+
|
204
|
+
node.init = [createDefaultInitForGlobal(node.globalType)];
|
205
|
+
|
206
|
+
additionalInitCode.push(
|
207
|
+
/**
|
208
|
+
* get_global in global initilizer only work for imported globals.
|
209
|
+
* They have the same indices than the init params, so use the
|
210
|
+
* same index.
|
211
|
+
*/
|
212
|
+
t.instruction("get_local", [initialGlobalidx]),
|
213
|
+
t.instruction("set_global", [t.indexLiteral(newGlobals.length)])
|
214
|
+
);
|
215
|
+
}
|
216
|
+
|
217
|
+
newGlobals.push(node);
|
218
|
+
|
219
|
+
path.remove();
|
210
220
|
}
|
211
221
|
});
|
212
222
|
|
@@ -218,18 +228,24 @@ const rewriteImportedGlobals = state => bin => {
|
|
218
228
|
* Rewrite the export names
|
219
229
|
* @param {Object} state state
|
220
230
|
* @param {Object} state.ast Module's ast
|
221
|
-
* @param {
|
231
|
+
* @param {Module} state.module Module
|
232
|
+
* @param {Set<string>} state.externalExports Module
|
222
233
|
* @returns {ArrayBufferTransform} transform
|
223
234
|
*/
|
224
|
-
const rewriteExportNames = ({ ast, module }) => bin => {
|
235
|
+
const rewriteExportNames = ({ ast, module, externalExports }) => bin => {
|
225
236
|
return editWithAST(ast, bin, {
|
226
237
|
ModuleExport(path) {
|
238
|
+
const isExternal = externalExports.has(path.node.name);
|
239
|
+
if (isExternal) {
|
240
|
+
path.remove();
|
241
|
+
return;
|
242
|
+
}
|
227
243
|
const usedName = module.isUsed(path.node.name);
|
228
|
-
if (usedName) {
|
229
|
-
path.node.name = usedName;
|
230
|
-
} else {
|
244
|
+
if (!usedName) {
|
231
245
|
path.remove();
|
246
|
+
return;
|
232
247
|
}
|
248
|
+
path.node.name = usedName;
|
233
249
|
}
|
234
250
|
});
|
235
251
|
};
|
@@ -247,10 +263,9 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
|
|
247
263
|
const result = usedDependencyMap.get(
|
248
264
|
path.node.module + ":" + path.node.name
|
249
265
|
);
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
path.node.module = WebAssemblyUtils.MANGLED_MODULE;
|
266
|
+
|
267
|
+
if (typeof result !== "undefined") {
|
268
|
+
path.node.module = result.module;
|
254
269
|
path.node.name = result.name;
|
255
270
|
}
|
256
271
|
}
|
@@ -265,17 +280,19 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
|
|
265
280
|
* @param {Object} state transformation state
|
266
281
|
* @param {Object} state.ast - Module's ast
|
267
282
|
* @param {t.Identifier} state.initFuncId identifier of the init function
|
268
|
-
* @param {t.
|
283
|
+
* @param {t.Index} state.startAtFuncOffset index of the start function
|
269
284
|
* @param {t.ModuleImport[]} state.importedGlobals list of imported globals
|
270
|
-
* @param {t.
|
271
|
-
* @param {t.
|
285
|
+
* @param {t.Instruction[]} state.additionalInitCode list of addition instructions for the init function
|
286
|
+
* @param {t.Index} state.nextFuncIndex index of the next function
|
287
|
+
* @param {t.Index} state.nextTypeIndex index of the next type
|
272
288
|
* @returns {ArrayBufferTransform} transform
|
273
289
|
*/
|
274
290
|
const addInitFunction = ({
|
275
291
|
ast,
|
276
292
|
initFuncId,
|
277
|
-
|
293
|
+
startAtFuncOffset,
|
278
294
|
importedGlobals,
|
295
|
+
additionalInitCode,
|
279
296
|
nextFuncIndex,
|
280
297
|
nextTypeIndex
|
281
298
|
}) => bin => {
|
@@ -296,8 +313,12 @@ const addInitFunction = ({
|
|
296
313
|
return [...acc, ...body];
|
297
314
|
}, []);
|
298
315
|
|
299
|
-
if (typeof
|
300
|
-
funcBody.push(t.callInstruction(
|
316
|
+
if (typeof startAtFuncOffset === "number") {
|
317
|
+
funcBody.push(t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset)));
|
318
|
+
}
|
319
|
+
|
320
|
+
for (const instr of additionalInitCode) {
|
321
|
+
funcBody.push(instr);
|
301
322
|
}
|
302
323
|
|
303
324
|
const funcResults = [];
|
@@ -324,12 +345,13 @@ const addInitFunction = ({
|
|
324
345
|
/**
|
325
346
|
* Extract mangle mappings from module
|
326
347
|
* @param {Module} module current module
|
348
|
+
* @param {boolean} mangle mangle imports
|
327
349
|
* @returns {Map<string, UsedWasmDependency>} mappings to mangled names
|
328
350
|
*/
|
329
|
-
const getUsedDependencyMap = module => {
|
351
|
+
const getUsedDependencyMap = (module, mangle) => {
|
330
352
|
/** @type {Map<string, UsedWasmDependency>} */
|
331
353
|
const map = new Map();
|
332
|
-
for (const usedDep of WebAssemblyUtils.getUsedDependencies(module)) {
|
354
|
+
for (const usedDep of WebAssemblyUtils.getUsedDependencies(module, mangle)) {
|
333
355
|
const dep = usedDep.dependency;
|
334
356
|
const request = dep.request;
|
335
357
|
const exportName = dep.name;
|
@@ -339,6 +361,11 @@ const getUsedDependencyMap = module => {
|
|
339
361
|
};
|
340
362
|
|
341
363
|
class WebAssemblyGenerator extends Generator {
|
364
|
+
constructor(options) {
|
365
|
+
super();
|
366
|
+
this.options = options;
|
367
|
+
}
|
368
|
+
|
342
369
|
generate(module) {
|
343
370
|
let bin = module.originalSource().source();
|
344
371
|
bin = preprocess(bin);
|
@@ -349,29 +376,44 @@ class WebAssemblyGenerator extends Generator {
|
|
349
376
|
: "__webpack_init__"
|
350
377
|
);
|
351
378
|
|
379
|
+
// parse it
|
352
380
|
const ast = decode(bin, {
|
353
381
|
ignoreDataSection: true,
|
354
382
|
ignoreCodeSection: true,
|
355
383
|
ignoreCustomNameSection: true
|
356
384
|
});
|
357
385
|
|
386
|
+
const moduleContext = moduleContextFromModuleAST(ast.body[0]);
|
387
|
+
|
358
388
|
const importedGlobals = getImportedGlobals(ast);
|
359
389
|
const countImportedFunc = getCountImportedFunc(ast);
|
360
|
-
const
|
390
|
+
const startAtFuncOffset = moduleContext.getStart();
|
361
391
|
const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
|
362
392
|
const nextTypeIndex = getNextTypeIndex(ast);
|
363
393
|
|
364
|
-
const usedDependencyMap = getUsedDependencyMap(
|
394
|
+
const usedDependencyMap = getUsedDependencyMap(
|
395
|
+
module,
|
396
|
+
this.options.mangleImports
|
397
|
+
);
|
398
|
+
const externalExports = new Set(
|
399
|
+
module.dependencies
|
400
|
+
.filter(d => d instanceof WebAssemblyExportImportedDependency)
|
401
|
+
.map(d => d.exportName)
|
402
|
+
);
|
403
|
+
|
404
|
+
/** @type {t.Instruction[]} */
|
405
|
+
const additionalInitCode = [];
|
365
406
|
|
366
407
|
const transform = compose(
|
367
408
|
rewriteExportNames({
|
368
409
|
ast,
|
369
|
-
module
|
410
|
+
module,
|
411
|
+
externalExports
|
370
412
|
}),
|
371
413
|
|
372
414
|
removeStartFunc({ ast }),
|
373
415
|
|
374
|
-
rewriteImportedGlobals({ ast }),
|
416
|
+
rewriteImportedGlobals({ ast, additionalInitCode }),
|
375
417
|
|
376
418
|
rewriteImports({
|
377
419
|
ast,
|
@@ -382,7 +424,8 @@ class WebAssemblyGenerator extends Generator {
|
|
382
424
|
ast,
|
383
425
|
initFuncId,
|
384
426
|
importedGlobals,
|
385
|
-
|
427
|
+
additionalInitCode,
|
428
|
+
startAtFuncOffset,
|
386
429
|
nextFuncIndex,
|
387
430
|
nextTypeIndex
|
388
431
|
})
|
@@ -8,34 +8,7 @@ const Generator = require("../Generator");
|
|
8
8
|
const Template = require("../Template");
|
9
9
|
const { RawSource } = require("webpack-sources");
|
10
10
|
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
|
11
|
-
|
12
|
-
function generateInitParams(module) {
|
13
|
-
const list = [];
|
14
|
-
|
15
|
-
for (const dep of module.dependencies) {
|
16
|
-
if (dep instanceof WebAssemblyImportDependency) {
|
17
|
-
if (dep.description.type === "GlobalType") {
|
18
|
-
const exportName = dep.name;
|
19
|
-
const usedName = dep.module && dep.module.isUsed(exportName);
|
20
|
-
|
21
|
-
if (dep.module === null) {
|
22
|
-
// Dependency was not found, an error will be thrown later
|
23
|
-
continue;
|
24
|
-
}
|
25
|
-
|
26
|
-
if (usedName !== false) {
|
27
|
-
list.push(
|
28
|
-
`__webpack_require__(${JSON.stringify(
|
29
|
-
dep.module.id
|
30
|
-
)})[${JSON.stringify(usedName)}]`
|
31
|
-
);
|
32
|
-
}
|
33
|
-
}
|
34
|
-
}
|
35
|
-
}
|
36
|
-
|
37
|
-
return list;
|
38
|
-
}
|
11
|
+
const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
|
39
12
|
|
40
13
|
class WebAssemblyJavascriptGenerator extends Generator {
|
41
14
|
generate(module, dependencyTemplates, runtimeTemplate) {
|
@@ -43,24 +16,88 @@ class WebAssemblyJavascriptGenerator extends Generator {
|
|
43
16
|
? Template.numberToIdentifer(module.usedExports.length)
|
44
17
|
: "__webpack_init__";
|
45
18
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
19
|
+
let needExportsCopy = false;
|
20
|
+
const importedModules = new Map();
|
21
|
+
const initParams = [];
|
22
|
+
let index = 0;
|
23
|
+
for (const dep of module.dependencies) {
|
24
|
+
if (dep.module) {
|
25
|
+
let importData = importedModules.get(dep.module);
|
26
|
+
if (importData === undefined) {
|
27
|
+
importedModules.set(
|
28
|
+
dep.module,
|
29
|
+
(importData = {
|
30
|
+
importVar: `m${index}`,
|
31
|
+
index,
|
32
|
+
request: dep.userRequest,
|
33
|
+
names: new Set(),
|
34
|
+
reexports: []
|
35
|
+
})
|
36
|
+
);
|
37
|
+
index++;
|
38
|
+
}
|
39
|
+
if (dep instanceof WebAssemblyImportDependency) {
|
40
|
+
importData.names.add(dep.name);
|
41
|
+
if (dep.description.type === "GlobalType") {
|
42
|
+
const exportName = dep.name;
|
43
|
+
const usedName = dep.module && dep.module.isUsed(exportName);
|
60
44
|
|
61
|
-
|
62
|
-
|
63
|
-
|
45
|
+
if (dep.module) {
|
46
|
+
if (usedName) {
|
47
|
+
initParams.push(
|
48
|
+
runtimeTemplate.exportFromImport({
|
49
|
+
module: dep.module,
|
50
|
+
request: dep.request,
|
51
|
+
importVar: importData.importVar,
|
52
|
+
originModule: module,
|
53
|
+
exportName: dep.name,
|
54
|
+
asiSafe: true,
|
55
|
+
isCall: false,
|
56
|
+
callContext: null
|
57
|
+
})
|
58
|
+
);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
if (dep instanceof WebAssemblyExportImportedDependency) {
|
64
|
+
importData.names.add(dep.name);
|
65
|
+
const usedName = module.isUsed(dep.exportName);
|
66
|
+
if (usedName) {
|
67
|
+
const defineStatement = Template.asString([
|
68
|
+
`${module.exportsArgument}[${JSON.stringify(
|
69
|
+
usedName
|
70
|
+
)}] = ${runtimeTemplate.exportFromImport({
|
71
|
+
module: dep.module,
|
72
|
+
request: dep.request,
|
73
|
+
importVar: importData.importVar,
|
74
|
+
originModule: module,
|
75
|
+
exportName: dep.name,
|
76
|
+
asiSafe: true,
|
77
|
+
isCall: false,
|
78
|
+
callContext: null
|
79
|
+
})};`
|
80
|
+
]);
|
81
|
+
importData.reexports.push(defineStatement);
|
82
|
+
needExportsCopy = true;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
87
|
+
const importsCode = Template.asString(
|
88
|
+
Array.from(
|
89
|
+
importedModules,
|
90
|
+
([module, { importVar, request, reexports }]) => {
|
91
|
+
const importStatement = runtimeTemplate.importStatement({
|
92
|
+
module,
|
93
|
+
request,
|
94
|
+
importVar,
|
95
|
+
originModule: module
|
96
|
+
});
|
97
|
+
return importStatement + reexports.join("\n");
|
98
|
+
}
|
99
|
+
)
|
100
|
+
);
|
64
101
|
|
65
102
|
// create source
|
66
103
|
const source = new RawSource(
|
@@ -69,18 +106,24 @@ class WebAssemblyJavascriptGenerator extends Generator {
|
|
69
106
|
"// Instantiate WebAssembly module",
|
70
107
|
"var wasmExports = __webpack_require__.w[module.i];",
|
71
108
|
|
109
|
+
!Array.isArray(module.usedExports)
|
110
|
+
? `__webpack_require__.r(${module.exportsArgument});`
|
111
|
+
: "",
|
112
|
+
|
72
113
|
// this must be before import for circular dependencies
|
73
114
|
"// export exports from WebAssembly module",
|
74
|
-
Array.isArray(module.usedExports)
|
115
|
+
Array.isArray(module.usedExports) && !needExportsCopy
|
75
116
|
? `${module.moduleArgument}.exports = wasmExports;`
|
76
117
|
: "for(var name in wasmExports) " +
|
77
118
|
`if(name != ${JSON.stringify(initIdentifer)}) ` +
|
78
119
|
`${module.exportsArgument}[name] = wasmExports[name];`,
|
79
120
|
"// exec imports from WebAssembly module (for esm order)",
|
80
|
-
|
81
|
-
|
121
|
+
importsCode,
|
122
|
+
"",
|
82
123
|
"// exec wasm module",
|
83
|
-
`wasmExports[${JSON.stringify(initIdentifer)}](${initParams
|
124
|
+
`wasmExports[${JSON.stringify(initIdentifer)}](${initParams.join(
|
125
|
+
", "
|
126
|
+
)})`
|
84
127
|
].join("\n")
|
85
128
|
);
|
86
129
|
return source;
|
@@ -9,8 +9,13 @@ const WebAssemblyParser = require("./WebAssemblyParser");
|
|
9
9
|
const WebAssemblyGenerator = require("./WebAssemblyGenerator");
|
10
10
|
const WebAssemblyJavascriptGenerator = require("./WebAssemblyJavascriptGenerator");
|
11
11
|
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
|
12
|
+
const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
|
12
13
|
|
13
14
|
class WebAssemblyModulesPlugin {
|
15
|
+
constructor(options) {
|
16
|
+
this.options = options;
|
17
|
+
}
|
18
|
+
|
14
19
|
apply(compiler) {
|
15
20
|
compiler.hooks.compilation.tap(
|
16
21
|
"WebAssemblyModulesPlugin",
|
@@ -20,6 +25,11 @@ class WebAssemblyModulesPlugin {
|
|
20
25
|
normalModuleFactory
|
21
26
|
);
|
22
27
|
|
28
|
+
compilation.dependencyFactories.set(
|
29
|
+
WebAssemblyExportImportedDependency,
|
30
|
+
normalModuleFactory
|
31
|
+
);
|
32
|
+
|
23
33
|
normalModuleFactory.hooks.createParser
|
24
34
|
.for("webassembly/experimental")
|
25
35
|
.tap("WebAssemblyModulesPlugin", () => {
|
@@ -31,7 +41,7 @@ class WebAssemblyModulesPlugin {
|
|
31
41
|
.tap("WebAssemblyModulesPlugin", () => {
|
32
42
|
return Generator.byType({
|
33
43
|
javascript: new WebAssemblyJavascriptGenerator(),
|
34
|
-
webassembly: new WebAssemblyGenerator()
|
44
|
+
webassembly: new WebAssemblyGenerator(this.options)
|
35
45
|
});
|
36
46
|
});
|
37
47
|
|