@@ -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 ( module ) ;
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 propertyName = usedDep . name ;
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
- `${ JSON . stringify ( propertyName ) } : ${ instanceVar } ` +
51
- `[${ JSON . stringify ( usedName ) } ]`
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,20 +64,55 @@ 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
- Template . asString ( [
63
- ` ${ JSON . stringify ( propertyName ) } : ` +
64
- ( importedModule . type . startsWith ( "webassembly" )
65
- ? ` ${ mod } ? ${ func } : `
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
+ } ) ;
72
78
}
73
79
}
74
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 ) ;
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
+ ] ;
114
+ }
115
+
75
116
if ( waitForInstances . size === 1 ) {
76
117
const moduleId = Array . from ( waitForInstances . values ( ) ) [ 0 ] ;
77
118
const promise = `installedWasmModules[${ JSON . stringify ( moduleId ) } ]` ;
@@ -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,41 +139,35 @@ 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
"},"
114
146
] ) ;
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 (
135
164
"WasmMainTemplatePlugin" ,
136
165
( source , chunk ) => {
137
166
const wasmModules = getAllWasmModules ( chunk ) ;
138
167
if ( wasmModules . length === 0 ) return source ;
139
- const importObjects = wasmModules . map ( generateImportObject ) ;
168
+ const importObjects = wasmModules . map ( module => {
169
+ return generateImportObject ( module , this . mangleImports ) ;
170
+ } ) ;
140
171
return Template . asString ( [
141
172
source ,
142
173
"" ,
@@ -192,6 +223,12 @@ class WasmMainTemplatePlugin {
192
223
}
193
224
}
194
225
) ;
226
+ const createImportObject = content =>
227
+ this . mangleImports
228
+ ? `{ ${ JSON . stringify (
229
+ WebAssemblyUtils . MANGLED_MODULE
230
+ ) } : ${ content } }`
231
+ : content ;
195
232
return Template . asString ( [
196
233
source ,
197
234
"" ,
@@ -219,15 +256,17 @@ class WasmMainTemplatePlugin {
219
256
Template . indent ( [
220
257
"promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) {" ,
221
258
Template . indent ( [
222
- "return WebAssembly.instantiate(items[0], " +
223
- `{ ${ WebAssemblyUtils . MANGLED_MODULE } : items[1] });`
259
+ `return WebAssembly.instantiate(items[0], ${ createImportObject (
260
+ "items[1]"
261
+ ) } );`
224
262
] ) ,
225
263
"});"
226
264
] ) ,
227
265
"} else if(typeof WebAssembly.instantiateStreaming === 'function') {" ,
228
266
Template . indent ( [
229
- "promise = WebAssembly.instantiateStreaming(req, " +
230
- `{ ${ WebAssemblyUtils . MANGLED_MODULE } : importObject });`
267
+ `promise = WebAssembly.instantiateStreaming(req, ${ createImportObject (
268
+ "importObject"
269
+ ) } );`
231
270
] )
232
271
] )
233
272
: Template . asString ( [
@@ -241,8 +280,9 @@ class WasmMainTemplatePlugin {
241
280
] ) ,
242
281
"]).then(function(items) {" ,
243
282
Template . indent ( [
244
- "return WebAssembly.instantiate(items[0], " +
245
- `{ ${ WebAssemblyUtils . MANGLED_MODULE } : items[1] });`
283
+ `return WebAssembly.instantiate(items[0], ${ createImportObject (
284
+ "items[1]"
285
+ ) } );`
246
286
] ) ,
247
287
"});"
248
288
] )
@@ -252,8 +292,9 @@ class WasmMainTemplatePlugin {
252
292
"var bytesPromise = req.then(function(x) { return x.arrayBuffer(); });" ,
253
293
"promise = bytesPromise.then(function(bytes) {" ,
254
294
Template . indent ( [
255
- "return WebAssembly.instantiate(bytes, " +
256
- `{ ${ WebAssemblyUtils . MANGLED_MODULE } : importObject });`
295
+ `return WebAssembly.instantiate(bytes, ${ createImportObject (
296
+ "importObject"
297
+ ) } );`
257
298
] ) ,
258
299
"});"
259
300
] ) ,
@@ -290,6 +331,7 @@ class WasmMainTemplatePlugin {
290
331
hash . update ( "WasmMainTemplatePlugin" ) ;
291
332
hash . update ( "1" ) ;
292
333
hash . update ( `${ mainTemplate . outputOptions . webassemblyModuleFilename } ` ) ;
334
+ hash . update ( `${ this . mangleImports } ` ) ;
293
335
} ) ;
294
336
mainTemplate . hooks . hashForChunk . tap (
295
337
"WasmMainTemplatePlugin" ,
0 commit comments