1717import loaderUtils from 'loader-utils' ;
1818import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin' ;
1919import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin' ;
20- import FetchCompileWasmTemplatePlugin from 'webpack/lib/web/FetchCompileWasmTemplatePlugin' ;
2120import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin' ;
2221import WORKER_PLUGIN_SYMBOL from './symbol' ;
2322
23+ let FetchCompileWasmPlugin ;
24+ try {
25+ FetchCompileWasmPlugin = require ( 'webpack/lib/web/FetchCompileWasmPlugin' ) ; // Webpack 5
26+ } catch ( e ) { }
27+ FetchCompileWasmPlugin = FetchCompileWasmPlugin || require ( 'webpack/lib/web/FetchCompileWasmTemplatePlugin' ) ; // Webpack 4
28+
2429const NAME = 'WorkerPluginLoader' ;
2530let hasWarned = false ;
2631
@@ -65,7 +70,7 @@ export function pitch (request) {
6570 new NodeTargetPlugin ( ) . apply ( workerCompiler ) ;
6671 }
6772 ( new WebWorkerTemplatePlugin ( workerOptions ) ) . apply ( workerCompiler ) ;
68- ( new FetchCompileWasmTemplatePlugin ( {
73+ ( new FetchCompileWasmPlugin ( {
6974 mangleImports : compilerOptions . optimization . mangleWasmImports
7075 } ) ) . apply ( workerCompiler ) ;
7176 ( new SingleEntryPlugin ( this . context , request , options . name ) ) . apply ( workerCompiler ) ;
@@ -82,7 +87,7 @@ export function pitch (request) {
8287 if ( ! err && compilation . errors && compilation . errors . length ) {
8388 err = compilation . errors [ 0 ] ;
8489 }
85- const entry = entries && entries [ 0 ] && entries [ 0 ] . files [ 0 ] ;
90+ const entry = entries && entries [ 0 ] && entries [ 0 ] . files . values ( ) . next ( ) . value ; // compatible with Array (v4) and Set (v5) prototypes
8691 if ( ! err && ! entry ) err = Error ( `WorkerPlugin: no entry for ${ request } ` ) ;
8792 if ( err ) return cb ( err ) ;
8893 return cb ( null , `module.exports = __webpack_public_path__ + ${ JSON . stringify ( entry ) } ` ) ;
0 commit comments