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
@@ -36,7 +36,9 @@ const getRequests = chunk => {
|
|
36
36
|
|
37
37
|
const getModulesSize = modules => {
|
38
38
|
let sum = 0;
|
39
|
-
for (const m of modules)
|
39
|
+
for (const m of modules) {
|
40
|
+
sum += m.size();
|
41
|
+
}
|
40
42
|
return sum;
|
41
43
|
};
|
42
44
|
|
@@ -172,8 +174,9 @@ module.exports = class SplitChunksPlugin {
|
|
172
174
|
static normalizeCacheGroups({ cacheGroups, automaticNameDelimiter }) {
|
173
175
|
if (typeof cacheGroups === "function") {
|
174
176
|
// TODO webpack 5 remove this
|
175
|
-
if (cacheGroups.length !== 1)
|
177
|
+
if (cacheGroups.length !== 1) {
|
176
178
|
return module => cacheGroups(module, module.getChunks());
|
179
|
+
}
|
177
180
|
return cacheGroups;
|
178
181
|
}
|
179
182
|
if (cacheGroups && typeof cacheGroups === "object") {
|
@@ -192,12 +195,7 @@ module.exports = class SplitChunksPlugin {
|
|
192
195
|
if (result) {
|
193
196
|
if (results === undefined) results = [];
|
194
197
|
for (const r of Array.isArray(result) ? result : [result]) {
|
195
|
-
const result = Object.assign(
|
196
|
-
{
|
197
|
-
key
|
198
|
-
},
|
199
|
-
r
|
200
|
-
);
|
198
|
+
const result = Object.assign({ key }, r);
|
201
199
|
if (result.name) result.getName = () => result.name;
|
202
200
|
if (result.chunks) {
|
203
201
|
result.chunksFilter = SplitChunksPlugin.normalizeChunksFilter(
|
@@ -240,23 +238,34 @@ module.exports = class SplitChunksPlugin {
|
|
240
238
|
static checkTest(test, module) {
|
241
239
|
if (test === undefined) return true;
|
242
240
|
if (typeof test === "function") {
|
243
|
-
if (test.length !== 1)
|
241
|
+
if (test.length !== 1) {
|
242
|
+
return test(module, module.getChunks());
|
243
|
+
}
|
244
244
|
return test(module);
|
245
245
|
}
|
246
246
|
if (typeof test === "boolean") return test;
|
247
247
|
if (typeof test === "string") {
|
248
|
-
if (
|
248
|
+
if (
|
249
|
+
module.nameForCondition &&
|
250
|
+
module.nameForCondition().startsWith(test)
|
251
|
+
) {
|
249
252
|
return true;
|
253
|
+
}
|
250
254
|
for (const chunk of module.chunksIterable) {
|
251
|
-
if (chunk.name && chunk.name.startsWith(test))
|
255
|
+
if (chunk.name && chunk.name.startsWith(test)) {
|
256
|
+
return true;
|
257
|
+
}
|
252
258
|
}
|
253
259
|
return false;
|
254
260
|
}
|
255
261
|
if (test instanceof RegExp) {
|
256
|
-
if (module.nameForCondition && test.test(module.nameForCondition()))
|
262
|
+
if (module.nameForCondition && test.test(module.nameForCondition())) {
|
257
263
|
return true;
|
264
|
+
}
|
258
265
|
for (const chunk of module.chunksIterable) {
|
259
|
-
if (chunk.name && test.test(chunk.name))
|
266
|
+
if (chunk.name && test.test(chunk.name)) {
|
267
|
+
return true;
|
268
|
+
}
|
260
269
|
}
|
261
270
|
return false;
|
262
271
|
}
|
@@ -447,8 +456,9 @@ module.exports = class SplitChunksPlugin {
|
|
447
456
|
for (const module of compilation.modules) {
|
448
457
|
// Get cache group
|
449
458
|
let cacheGroups = this.options.getCacheGroups(module);
|
450
|
-
if (!Array.isArray(cacheGroups) || cacheGroups.length === 0)
|
459
|
+
if (!Array.isArray(cacheGroups) || cacheGroups.length === 0) {
|
451
460
|
continue;
|
461
|
+
}
|
452
462
|
|
453
463
|
// Prepare some values
|
454
464
|
const chunksKey = getKey(module.chunksIterable);
|
@@ -555,18 +565,20 @@ module.exports = class SplitChunksPlugin {
|
|
555
565
|
if (pair[1] === item.modules.size) {
|
556
566
|
const chunk = pair[0];
|
557
567
|
if (chunk.hasEntryModule()) continue;
|
558
|
-
if (!newChunk || !newChunk.name)
|
559
|
-
|
568
|
+
if (!newChunk || !newChunk.name) {
|
569
|
+
newChunk = chunk;
|
570
|
+
} else if (
|
560
571
|
chunk.name &&
|
561
572
|
chunk.name.length < newChunk.name.length
|
562
|
-
)
|
573
|
+
) {
|
563
574
|
newChunk = chunk;
|
564
|
-
else if (
|
575
|
+
} else if (
|
565
576
|
chunk.name &&
|
566
577
|
chunk.name.length === newChunk.name.length &&
|
567
578
|
chunk.name < newChunk.name
|
568
|
-
)
|
579
|
+
) {
|
569
580
|
newChunk = chunk;
|
581
|
+
}
|
570
582
|
chunkName = undefined;
|
571
583
|
isReused = true;
|
572
584
|
}
|
@@ -683,8 +695,9 @@ module.exports = class SplitChunksPlugin {
|
|
683
695
|
}
|
684
696
|
if (info.modules.size !== oldSize) {
|
685
697
|
info.size = getModulesSize(info.modules);
|
686
|
-
if (info.size < info.cacheGroup.minSize)
|
698
|
+
if (info.size < info.cacheGroup.minSize) {
|
687
699
|
chunksInfoMap.delete(key);
|
700
|
+
}
|
688
701
|
}
|
689
702
|
}
|
690
703
|
}
|
@@ -9,21 +9,21 @@ const SizeFormatHelpers = require("../SizeFormatHelpers");
|
|
9
9
|
|
10
10
|
module.exports = class AssetsOverSizeLimitWarning extends WebpackError {
|
11
11
|
constructor(assetsOverSizeLimit, assetLimit) {
|
12
|
-
|
13
|
-
|
14
|
-
this.name = "AssetsOverSizeLimitWarning";
|
15
|
-
this.assets = assetsOverSizeLimit;
|
16
|
-
const assetLists = this.assets
|
12
|
+
const assetLists = assetsOverSizeLimit
|
17
13
|
.map(
|
18
14
|
asset =>
|
19
15
|
`\n ${asset.name} (${SizeFormatHelpers.formatSize(asset.size)})`
|
20
16
|
)
|
21
17
|
.join("");
|
22
|
-
|
18
|
+
|
19
|
+
super(`asset size limit: The following asset(s) exceed the recommended size limit (${SizeFormatHelpers.formatSize(
|
23
20
|
assetLimit
|
24
21
|
)}).
|
25
22
|
This can impact web performance.
|
26
|
-
Assets: ${assetLists}
|
23
|
+
Assets: ${assetLists}`);
|
24
|
+
|
25
|
+
this.name = "AssetsOverSizeLimitWarning";
|
26
|
+
this.assets = assetsOverSizeLimit;
|
27
27
|
|
28
28
|
Error.captureStackTrace(this, this.constructor);
|
29
29
|
}
|
@@ -9,11 +9,7 @@ const SizeFormatHelpers = require("../SizeFormatHelpers");
|
|
9
9
|
|
10
10
|
module.exports = class EntrypointsOverSizeLimitWarning extends WebpackError {
|
11
11
|
constructor(entrypoints, entrypointLimit) {
|
12
|
-
|
13
|
-
|
14
|
-
this.name = "EntrypointsOverSizeLimitWarning";
|
15
|
-
this.entrypoints = entrypoints;
|
16
|
-
const entrypointList = this.entrypoints
|
12
|
+
const entrypointList = entrypoints
|
17
13
|
.map(
|
18
14
|
entrypoint =>
|
19
15
|
`\n ${entrypoint.name} (${SizeFormatHelpers.formatSize(
|
@@ -21,10 +17,13 @@ module.exports = class EntrypointsOverSizeLimitWarning extends WebpackError {
|
|
21
17
|
)})\n${entrypoint.files.map(asset => ` ${asset}`).join("\n")}`
|
22
18
|
)
|
23
19
|
.join("");
|
24
|
-
|
20
|
+
super(`entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (${SizeFormatHelpers.formatSize(
|
25
21
|
entrypointLimit
|
26
22
|
)}). This can impact web performance.
|
27
|
-
Entrypoints:${entrypointList}\n
|
23
|
+
Entrypoints:${entrypointList}\n`);
|
24
|
+
|
25
|
+
this.name = "EntrypointsOverSizeLimitWarning";
|
26
|
+
this.entrypoints = entrypoints;
|
28
27
|
|
29
28
|
Error.captureStackTrace(this, this.constructor);
|
30
29
|
}
|
@@ -8,13 +8,13 @@ const WebpackError = require("../WebpackError");
|
|
8
8
|
|
9
9
|
module.exports = class NoAsyncChunksWarning extends WebpackError {
|
10
10
|
constructor() {
|
11
|
-
super(
|
11
|
+
super(
|
12
|
+
"webpack performance recommendations: \n" +
|
13
|
+
"You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.\n" +
|
14
|
+
"For more info visit https://webpack.js.org/guides/code-splitting/"
|
15
|
+
);
|
12
16
|
|
13
17
|
this.name = "NoAsyncChunksWarning";
|
14
|
-
this.message =
|
15
|
-
"webpack performance recommendations: \n" +
|
16
|
-
"You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.\n" +
|
17
|
-
"For more info visit https://webpack.js.org/guides/code-splitting/";
|
18
18
|
|
19
19
|
Error.captureStackTrace(this, this.constructor);
|
20
20
|
}
|
package/lib/util/SortableSet.js
CHANGED
@@ -94,12 +94,15 @@ class SortableSet extends Set {
|
|
94
94
|
}
|
95
95
|
|
96
96
|
_invalidateCache() {
|
97
|
-
if (this._cache !== undefined)
|
97
|
+
if (this._cache !== undefined) {
|
98
|
+
this._cache.clear();
|
99
|
+
}
|
98
100
|
}
|
99
101
|
|
100
102
|
_invalidateOrderedCache() {
|
101
|
-
if (this._cacheOrderIndependent !== undefined)
|
103
|
+
if (this._cacheOrderIndependent !== undefined) {
|
102
104
|
this._cacheOrderIndependent.clear();
|
105
|
+
}
|
103
106
|
}
|
104
107
|
}
|
105
108
|
|
@@ -25,8 +25,11 @@ class StackedSetMap {
|
|
25
25
|
}
|
26
26
|
|
27
27
|
delete(item) {
|
28
|
-
if (this.stack.length > 1)
|
29
|
-
|
28
|
+
if (this.stack.length > 1) {
|
29
|
+
this.map.set(item, TOMBSTONE);
|
30
|
+
} else {
|
31
|
+
this.map.delete(item);
|
32
|
+
}
|
30
33
|
}
|
31
34
|
|
32
35
|
has(item) {
|
@@ -47,10 +50,11 @@ class StackedSetMap {
|
|
47
50
|
|
48
51
|
get(item) {
|
49
52
|
const topValue = this.map.get(item);
|
50
|
-
if (topValue !== undefined)
|
53
|
+
if (topValue !== undefined) {
|
51
54
|
return topValue === TOMBSTONE || topValue === UNDEFINED_MARKER
|
52
55
|
? undefined
|
53
56
|
: topValue;
|
57
|
+
}
|
54
58
|
if (this.stack.length > 1) {
|
55
59
|
for (var i = this.stack.length - 2; i >= 0; i--) {
|
56
60
|
const value = this.stack[i].get(item);
|
@@ -71,8 +75,11 @@ class StackedSetMap {
|
|
71
75
|
this.map = new Map();
|
72
76
|
for (const data of this.stack) {
|
73
77
|
for (const pair of data) {
|
74
|
-
if (pair[1] === TOMBSTONE)
|
75
|
-
|
78
|
+
if (pair[1] === TOMBSTONE) {
|
79
|
+
this.map.delete(pair[0]);
|
80
|
+
} else {
|
81
|
+
this.map.set(pair[0], pair[1]);
|
82
|
+
}
|
76
83
|
}
|
77
84
|
}
|
78
85
|
this.stack = [this.map];
|
@@ -8,9 +8,8 @@ const WebpackError = require("../WebpackError");
|
|
8
8
|
module.exports = class UnsupportedWebAssemblyFeatureError extends WebpackError {
|
9
9
|
/** @param {string} message Error message */
|
10
10
|
constructor(message) {
|
11
|
-
super();
|
11
|
+
super(message);
|
12
12
|
this.name = "UnsupportedWebAssemblyFeatureError";
|
13
|
-
this.message = message;
|
14
13
|
this.hideStack = true;
|
15
14
|
|
16
15
|
Error.captureStackTrace(this, this.constructor);
|
@@ -0,0 +1,66 @@
|
|
1
|
+
/*
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
+
Author Tobias Koppers @sokra
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
const UnsupportedWebAssemblyFeatureError = require("../wasm/UnsupportedWebAssemblyFeatureError");
|
8
|
+
|
9
|
+
class WasmFinalizeExportsPlugin {
|
10
|
+
apply(compiler) {
|
11
|
+
compiler.hooks.compilation.tap("WasmFinalizeExportsPlugin", compilation => {
|
12
|
+
compilation.hooks.finishModules.tap(
|
13
|
+
"WasmFinalizeExportsPlugin",
|
14
|
+
modules => {
|
15
|
+
for (const module of modules) {
|
16
|
+
// 1. if a WebAssembly module
|
17
|
+
if (module.type.startsWith("webassembly") === true) {
|
18
|
+
const jsIncompatibleExports =
|
19
|
+
module.buildMeta.jsIncompatibleExports;
|
20
|
+
|
21
|
+
if (jsIncompatibleExports === undefined) {
|
22
|
+
continue;
|
23
|
+
}
|
24
|
+
|
25
|
+
for (const reason of module.reasons) {
|
26
|
+
// 2. is referenced by a non-WebAssembly module
|
27
|
+
if (reason.module.type.startsWith("webassembly") === false) {
|
28
|
+
const ref = reason.dependency.getReference();
|
29
|
+
|
30
|
+
const importedNames = ref.importedNames;
|
31
|
+
|
32
|
+
if (Array.isArray(importedNames)) {
|
33
|
+
importedNames.forEach(name => {
|
34
|
+
// 3. and uses a func with an incompatible JS signature
|
35
|
+
if (
|
36
|
+
Object.prototype.hasOwnProperty.call(
|
37
|
+
jsIncompatibleExports,
|
38
|
+
name
|
39
|
+
)
|
40
|
+
) {
|
41
|
+
// 4. error
|
42
|
+
/** @type {any} */
|
43
|
+
const error = new UnsupportedWebAssemblyFeatureError(
|
44
|
+
`Export "${name}" with ${
|
45
|
+
jsIncompatibleExports[name]
|
46
|
+
} can only be used for direct wasm to wasm dependencies`
|
47
|
+
);
|
48
|
+
error.module = module;
|
49
|
+
error.origin = reason.module;
|
50
|
+
error.originLoc = reason.dependency.loc;
|
51
|
+
error.dependencies = [reason.dependency];
|
52
|
+
compilation.errors.push(error);
|
53
|
+
}
|
54
|
+
});
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}
|
61
|
+
);
|
62
|
+
});
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
module.exports = WasmFinalizeExportsPlugin;
|
@@ -27,12 +27,16 @@ function getAllWasmModules(chunk) {
|
|
27
27
|
/**
|
28
28
|
* generates the import object function for a module
|
29
29
|
* @param {Module} module the module
|
30
|
+
* @param {boolean} mangle mangle imports
|
30
31
|
* @returns {string} source code
|
31
32
|
*/
|
32
|
-
function generateImportObject(module) {
|
33
|
+
function generateImportObject(module, mangle) {
|
33
34
|
const waitForInstances = new Map();
|
34
35
|
const properties = [];
|
35
|
-
const usedWasmDependencies = WebAssemblyUtils.getUsedDependencies(
|
36
|
+
const usedWasmDependencies = WebAssemblyUtils.getUsedDependencies(
|
37
|
+
module,
|
38
|
+
mangle
|
39
|
+
);
|
36
40
|
for (const usedDep of usedWasmDependencies) {
|
37
41
|
const dep = usedDep.dependency;
|
38
42
|
const importedModule = dep.module;
|
@@ -41,15 +45,17 @@ function generateImportObject(module) {
|
|
41
45
|
const description = dep.description;
|
42
46
|
const direct = dep.onlyDirectImport;
|
43
47
|
|
44
|
-
const
|
48
|
+
const module = usedDep.module;
|
49
|
+
const name = usedDep.name;
|
45
50
|
|
46
51
|
if (direct) {
|
47
52
|
const instanceVar = `m${waitForInstances.size}`;
|
48
53
|
waitForInstances.set(instanceVar, importedModule.id);
|
49
|
-
properties.push(
|
50
|
-
|
51
|
-
|
52
|
-
|
54
|
+
properties.push({
|
55
|
+
module,
|
56
|
+
name,
|
57
|
+
value: `${instanceVar}[${JSON.stringify(usedName)}]`
|
58
|
+
});
|
53
59
|
} else {
|
54
60
|
const params = description.signature.params.map(
|
55
61
|
(param, k) => "p" + k + param.valtype
|
@@ -58,18 +64,53 @@ function generateImportObject(module) {
|
|
58
64
|
const mod = `installedModules[${JSON.stringify(importedModule.id)}]`;
|
59
65
|
const func = `${mod}.exports[${JSON.stringify(usedName)}]`;
|
60
66
|
|
61
|
-
properties.push(
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
`function(${params}) {`,
|
67
|
+
properties.push({
|
68
|
+
module,
|
69
|
+
name,
|
70
|
+
value: Template.asString([
|
71
|
+
(importedModule.type.startsWith("webassembly")
|
72
|
+
? `${mod} ? ${func} : `
|
73
|
+
: "") + `function(${params}) {`,
|
68
74
|
Template.indent([`return ${func}(${params});`]),
|
69
75
|
"}"
|
70
76
|
])
|
71
|
-
);
|
77
|
+
});
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
let importObject;
|
82
|
+
if (mangle) {
|
83
|
+
importObject = [
|
84
|
+
"return {",
|
85
|
+
Template.indent([
|
86
|
+
properties.map(p => `${JSON.stringify(p.name)}: ${p.value}`).join(",\n")
|
87
|
+
]),
|
88
|
+
"};"
|
89
|
+
];
|
90
|
+
} else {
|
91
|
+
const propertiesByModule = new Map();
|
92
|
+
for (const p of properties) {
|
93
|
+
let list = propertiesByModule.get(p.module);
|
94
|
+
if (list === undefined) {
|
95
|
+
propertiesByModule.set(p.module, (list = []));
|
96
|
+
}
|
97
|
+
list.push(p);
|
72
98
|
}
|
99
|
+
importObject = [
|
100
|
+
"return {",
|
101
|
+
Template.indent([
|
102
|
+
Array.from(propertiesByModule, ([module, list]) => {
|
103
|
+
return Template.asString([
|
104
|
+
`${JSON.stringify(module)}: {`,
|
105
|
+
Template.indent([
|
106
|
+
list.map(p => `${JSON.stringify(p.name)}: ${p.value}`).join(",\n")
|
107
|
+
]),
|
108
|
+
"}"
|
109
|
+
]);
|
110
|
+
}).join(",\n")
|
111
|
+
]),
|
112
|
+
"};"
|
113
|
+
];
|
73
114
|
}
|
74
115
|
|
75
116
|
if (waitForInstances.size === 1) {
|
@@ -80,11 +121,7 @@ function generateImportObject(module) {
|
|
80
121
|
`${JSON.stringify(module.id)}: function() {`,
|
81
122
|
Template.indent([
|
82
123
|
`return promiseResolve().then(function() { return ${promise}; }).then(function(${variable}) {`,
|
83
|
-
Template.indent(
|
84
|
-
"return {",
|
85
|
-
Template.indent([properties.join(",\n")]),
|
86
|
-
"};"
|
87
|
-
]),
|
124
|
+
Template.indent(importObject),
|
88
125
|
"});"
|
89
126
|
]),
|
90
127
|
"},"
|
@@ -102,12 +139,7 @@ function generateImportObject(module) {
|
|
102
139
|
`${JSON.stringify(module.id)}: function() {`,
|
103
140
|
Template.indent([
|
104
141
|
`return promiseResolve().then(function() { return Promise.all([${promises}]); }).then(function(array) {`,
|
105
|
-
Template.indent([
|
106
|
-
`var ${variables};`,
|
107
|
-
"return {",
|
108
|
-
Template.indent([properties.join(",\n")]),
|
109
|
-
"};"
|
110
|
-
]),
|
142
|
+
Template.indent([`var ${variables};`, ...importObject]),
|
111
143
|
"});"
|
112
144
|
]),
|
113
145
|
"},"
|
@@ -115,20 +147,17 @@ function generateImportObject(module) {
|
|
115
147
|
} else {
|
116
148
|
return Template.asString([
|
117
149
|
`${JSON.stringify(module.id)}: function() {`,
|
118
|
-
Template.indent(
|
119
|
-
"return {",
|
120
|
-
Template.indent([properties.join(",\n")]),
|
121
|
-
"};"
|
122
|
-
]),
|
150
|
+
Template.indent(importObject),
|
123
151
|
"},"
|
124
152
|
]);
|
125
153
|
}
|
126
154
|
}
|
127
155
|
|
128
156
|
class WasmMainTemplatePlugin {
|
129
|
-
constructor(generateLoadBinaryCode, supportsStreaming) {
|
157
|
+
constructor({ generateLoadBinaryCode, supportsStreaming, mangleImports }) {
|
130
158
|
this.generateLoadBinaryCode = generateLoadBinaryCode;
|
131
159
|
this.supportsStreaming = supportsStreaming;
|
160
|
+
this.mangleImports = mangleImports;
|
132
161
|
}
|
133
162
|
apply(mainTemplate) {
|
134
163
|
mainTemplate.hooks.localVars.tap(
|
@@ -136,7 +165,9 @@ class WasmMainTemplatePlugin {
|
|
136
165
|
(source, chunk) => {
|
137
166
|
const wasmModules = getAllWasmModules(chunk);
|
138
167
|
if (wasmModules.length === 0) return source;
|
139
|
-
const importObjects = wasmModules.map(
|
168
|
+
const importObjects = wasmModules.map(module => {
|
169
|
+
return generateImportObject(module, this.mangleImports);
|
170
|
+
});
|
140
171
|
return Template.asString([
|
141
172
|
source,
|
142
173
|
"",
|
@@ -179,10 +210,11 @@ class WasmMainTemplatePlugin {
|
|
179
210
|
hashWithLength(length) {
|
180
211
|
const shortChunkHashMap = Object.create(null);
|
181
212
|
for (const wasmModuleId of Object.keys(chunkModuleMaps.hash)) {
|
182
|
-
if (typeof chunkModuleMaps.hash[wasmModuleId] === "string")
|
213
|
+
if (typeof chunkModuleMaps.hash[wasmModuleId] === "string") {
|
183
214
|
shortChunkHashMap[wasmModuleId] = chunkModuleMaps.hash[
|
184
215
|
wasmModuleId
|
185
216
|
].substr(0, length);
|
217
|
+
}
|
186
218
|
}
|
187
219
|
return `" + ${JSON.stringify(
|
188
220
|
shortChunkHashMap
|
@@ -191,6 +223,12 @@ class WasmMainTemplatePlugin {
|
|
191
223
|
}
|
192
224
|
}
|
193
225
|
);
|
226
|
+
const createImportObject = content =>
|
227
|
+
this.mangleImports
|
228
|
+
? `{ ${JSON.stringify(
|
229
|
+
WebAssemblyUtils.MANGLED_MODULE
|
230
|
+
)}: ${content} }`
|
231
|
+
: content;
|
194
232
|
return Template.asString([
|
195
233
|
source,
|
196
234
|
"",
|
@@ -218,15 +256,17 @@ class WasmMainTemplatePlugin {
|
|
218
256
|
Template.indent([
|
219
257
|
"promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) {",
|
220
258
|
Template.indent([
|
221
|
-
|
222
|
-
|
259
|
+
`return WebAssembly.instantiate(items[0], ${createImportObject(
|
260
|
+
"items[1]"
|
261
|
+
)});`
|
223
262
|
]),
|
224
263
|
"});"
|
225
264
|
]),
|
226
265
|
"} else if(typeof WebAssembly.instantiateStreaming === 'function') {",
|
227
266
|
Template.indent([
|
228
|
-
|
229
|
-
|
267
|
+
`promise = WebAssembly.instantiateStreaming(req, ${createImportObject(
|
268
|
+
"importObject"
|
269
|
+
)});`
|
230
270
|
])
|
231
271
|
])
|
232
272
|
: Template.asString([
|
@@ -240,8 +280,9 @@ class WasmMainTemplatePlugin {
|
|
240
280
|
]),
|
241
281
|
"]).then(function(items) {",
|
242
282
|
Template.indent([
|
243
|
-
|
244
|
-
|
283
|
+
`return WebAssembly.instantiate(items[0], ${createImportObject(
|
284
|
+
"items[1]"
|
285
|
+
)});`
|
245
286
|
]),
|
246
287
|
"});"
|
247
288
|
])
|
@@ -251,8 +292,9 @@ class WasmMainTemplatePlugin {
|
|
251
292
|
"var bytesPromise = req.then(function(x) { return x.arrayBuffer(); });",
|
252
293
|
"promise = bytesPromise.then(function(bytes) {",
|
253
294
|
Template.indent([
|
254
|
-
|
255
|
-
|
295
|
+
`return WebAssembly.instantiate(bytes, ${createImportObject(
|
296
|
+
"importObject"
|
297
|
+
)});`
|
256
298
|
]),
|
257
299
|
"});"
|
258
300
|
]),
|
@@ -274,8 +316,9 @@ class WasmMainTemplatePlugin {
|
|
274
316
|
mainTemplate.hooks.requireExtensions.tap(
|
275
317
|
"WasmMainTemplatePlugin",
|
276
318
|
(source, chunk) => {
|
277
|
-
if (!chunk.hasModuleInGraph(m => m.type.startsWith("webassembly")))
|
319
|
+
if (!chunk.hasModuleInGraph(m => m.type.startsWith("webassembly"))) {
|
278
320
|
return source;
|
321
|
+
}
|
279
322
|
return Template.asString([
|
280
323
|
source,
|
281
324
|
"",
|
@@ -288,6 +331,7 @@ class WasmMainTemplatePlugin {
|
|
288
331
|
hash.update("WasmMainTemplatePlugin");
|
289
332
|
hash.update("1");
|
290
333
|
hash.update(`${mainTemplate.outputOptions.webassemblyModuleFilename}`);
|
334
|
+
hash.update(`${this.mangleImports}`);
|
291
335
|
});
|
292
336
|
mainTemplate.hooks.hashForChunk.tap(
|
293
337
|
"WasmMainTemplatePlugin",
|