55"use strict" ;
66
77const Generator = require ( "../Generator" ) ;
8+ const Template = require ( "../Template" ) ;
89const { RawSource } = require ( "webpack-sources" ) ;
910
1011const { editWithAST, addWithAST } = require ( "@webassemblyjs/wasm-edit" ) ;
1112const { decode } = require ( "@webassemblyjs/wasm-parser" ) ;
1213const t = require ( "@webassemblyjs/ast" ) ;
1314
1415function compose ( ...fns ) {
15- return fns . reverse ( ) . reduce ( ( prevFn , nextFn ) => {
16+ return fns . reduce ( ( prevFn , nextFn ) => {
1617 return value => nextFn ( prevFn ( value ) ) ;
1718 } , value => value ) ;
1819}
@@ -31,8 +32,6 @@ const isGlobalImport = n => n.descr.type === "GlobalType";
3132 */
3233const isFuncImport = n => n . descr . type === "FuncImportDescr" ;
3334
34- const initFuncId = t . identifier ( "__webpack_init__" ) ;
35-
3635// TODO replace with @callback
3736/**
3837 * @typedef {(ArrayBuffer) => ArrayBuffer } ArrayBufferTransform
@@ -181,13 +180,36 @@ const rewriteImportedGlobals = state => bin => {
181180 return addWithAST ( state . ast , bin , newGlobals ) ;
182181} ;
183182
183+ /**
184+ * Rewrite the export names
185+ * @param {Object } state state
186+ * @param {Object } state.ast Module's ast
187+ * @param {Object } state.module Module
188+ * @returns {ArrayBufferTransform } transform
189+ */
190+ const rewriteExportNames = ( { ast, module } ) => bin => {
191+ return editWithAST ( ast , bin , {
192+ ModuleExport ( path ) {
193+ const usedName = module . isUsed ( path . node . name ) ;
194+ if ( usedName ) {
195+ path . node . name = usedName ;
196+ // TODO remove this when fixed in @webassemblyjs
197+ path . node . descr . id = t . numberLiteral ( + path . node . descr . id . raw ) ;
198+ } else {
199+ path . remove ( ) ;
200+ }
201+ }
202+ } ) ;
203+ } ;
204+
184205/**
185206 * Add an init function.
186207 *
187208 * The init function fills the globals given input arguments.
188209 *
189210 * @param {Object } state transformation state
190211 * @param {Object } state.ast - Module's ast
212+ * @param {t.Identifier } state.initFuncId identifier of the init function
191213 * @param {t.IndexLiteral } state.startAtFuncIndex index of the start function
192214 * @param {t.ModuleImport[] } state.importedGlobals list of imported globals
193215 * @param {t.IndexLiteral } state.nextFuncIndex index of the next function
@@ -196,6 +218,7 @@ const rewriteImportedGlobals = state => bin => {
196218 */
197219const addInitFunction = ( {
198220 ast,
221+ initFuncId,
199222 startAtFuncIndex,
200223 importedGlobals,
201224 nextFuncIndex,
@@ -246,6 +269,12 @@ class WebAssemblyGenerator extends Generator {
246269 generate ( module ) {
247270 const bin = module . originalSource ( ) . source ( ) ;
248271
272+ const initFuncId = t . identifier (
273+ Array . isArray ( module . usedExports )
274+ ? Template . numberToIdentifer ( module . usedExports . length )
275+ : "__webpack_init__"
276+ ) ;
277+
249278 const ast = decode ( bin , {
250279 ignoreDataSection : true ,
251280 ignoreCodeSection : true
@@ -258,12 +287,18 @@ class WebAssemblyGenerator extends Generator {
258287 const nextTypeIndex = getNextTypeIndex ( ast ) ;
259288
260289 const transform = compose (
290+ rewriteExportNames ( {
291+ ast,
292+ module
293+ } ) ,
294+
261295 removeStartFunc ( { ast } ) ,
262296
263297 rewriteImportedGlobals ( { ast } ) ,
264298
265299 addInitFunction ( {
266300 ast,
301+ initFuncId,
267302 importedGlobals,
268303 startAtFuncIndex,
269304 nextFuncIndex,
0 commit comments