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 WORKER_PLUGIN_SYMBOL from './symbol' ;
2221
22+ let FetchCompileWasmPlugin ;
23+ try {
24+ FetchCompileWasmPlugin = require ( 'webpack/lib/web/FetchCompileWasmPlugin' ) ; // Webpack 5
25+ } catch ( e ) { }
26+ FetchCompileWasmPlugin = FetchCompileWasmPlugin || require ( 'webpack/lib/web/FetchCompileWasmTemplatePlugin' ) ; // Webpack 4
27+
2328const NAME = 'WorkerPluginLoader' ;
2429let hasWarned = false ;
2530
@@ -59,7 +64,7 @@ export function pitch (request) {
5964 const workerCompiler = this . _compilation . createChildCompiler ( NAME , workerOptions , plugins ) ;
6065 workerCompiler . context = this . _compiler . context ;
6166 ( new WebWorkerTemplatePlugin ( ) ) . apply ( workerCompiler ) ;
62- ( new FetchCompileWasmTemplatePlugin ( {
67+ ( new FetchCompileWasmPlugin ( {
6368 mangleImports : compilerOptions . optimization . mangleWasmImports
6469 } ) ) . apply ( workerCompiler ) ;
6570 ( new SingleEntryPlugin ( this . context , request , options . name ) ) . apply ( workerCompiler ) ;
@@ -76,7 +81,7 @@ export function pitch (request) {
7681 if ( ! err && compilation . errors && compilation . errors . length ) {
7782 err = compilation . errors [ 0 ] ;
7883 }
79- const entry = entries && entries [ 0 ] && entries [ 0 ] . files [ 0 ] ;
84+ const entry = entries && entries [ 0 ] && entries [ 0 ] . files . values ( ) . next ( ) . value ; // compatible with Array (v4) and Set (v5) prototypes
8085 if ( ! err && ! entry ) err = Error ( `WorkerPlugin: no entry for ${ request } ` ) ;
8186 if ( err ) return cb ( err ) ;
8287 return cb ( null , `${ options . esModule ? 'export default' : 'module.exports =' } __webpack_public_path__ + ${ JSON . stringify ( entry ) } ` ) ;
0 commit comments