@@ -64,6 +64,25 @@ loader.pitch = function(request) {
6464 ( new NodeTargetPlugin ( ) ) . apply ( worker . compiler ) ;
6565 }
6666
67+ // webpack >= v4 supports webassembly
68+ let wasmPluginPath = null ;
69+ try {
70+ wasmPluginPath = require . resolve (
71+ 'webpack/lib/web/FetchCompileWasmTemplatePlugin'
72+ ) ;
73+ }
74+ catch ( _err ) {
75+ // webpack <= v3, skipping
76+ }
77+
78+ if ( wasmPluginPath ) {
79+ // eslint-disable-next-line global-require
80+ const FetchCompileWasmTemplatePlugin = require ( wasmPluginPath ) ;
81+ new FetchCompileWasmTemplatePlugin ( {
82+ mangleImports : this . _compiler . options . optimization . mangleWasmImports
83+ } ) . apply ( worker . compiler ) ;
84+ }
85+
6786 ( new SingleEntryPlugin ( this . context , `!!${ path . resolve ( __dirname , 'rpc-worker-loader.js' ) } !${ request } ` , 'main' ) ) . apply ( worker . compiler ) ;
6887
6988 const subCache = `subcache ${ __dirname } ${ request } ` ;
@@ -83,7 +102,8 @@ loader.pitch = function(request) {
83102 // only process entry exports
84103 if ( current . resource !== entry ) return ;
85104
86- let exports = compilation . __workerizeExports || ( compilation . __workerizeExports = { } ) ;
105+ let key = current . nameForCondition ( ) ;
106+ let exports = CACHE [ key ] || ( CACHE [ key ] = { } ) ;
87107
88108 if ( decl . id ) {
89109 exports [ decl . id . name ] = true ;
@@ -106,8 +126,9 @@ loader.pitch = function(request) {
106126 if ( entries [ 0 ] ) {
107127 worker . file = entries [ 0 ] . files [ 0 ] ;
108128
129+ let key = entries [ 0 ] . entryModule . nameForCondition ( ) ;
109130 let contents = compilation . assets [ worker . file ] . source ( ) ;
110- let exports = Object . keys ( CACHE [ worker . file ] = compilation . __workerizeExports || CACHE [ worker . file ] || { } ) ;
131+ let exports = Object . keys ( CACHE [ key ] || { } ) ;
111132
112133 // console.log('Workerized exports: ', exports.join(', '));
113134
@@ -125,11 +146,16 @@ loader.pitch = function(request) {
125146 delete this . _compilation . assets [ worker . file ] ;
126147 }
127148
149+ let workerUrl = worker . url ;
150+ if ( options . import ) {
151+ workerUrl = `"data:,importScripts('"+location.origin+${ workerUrl } +"')"` ;
152+ }
153+
128154 return cb ( null , `
129155 var addMethods = require(${ loaderUtils . stringifyRequest ( this , path . resolve ( __dirname , 'rpc-wrapper.js' ) ) } )
130156 var methods = ${ JSON . stringify ( exports ) }
131157 module.exports = function() {
132- var w = new Worker(${ worker . url } , { name: ${ JSON . stringify ( filename ) } })
158+ var w = new Worker(${ workerUrl } , { name: ${ JSON . stringify ( filename ) } })
133159 addMethods(w, methods)
134160 ${ options . ready ? 'w.ready = new Promise(function(r) { w.addEventListener("ready", function(){ r(w) }) })' : '' }
135161 return w
0 commit comments