@@ -5,6 +5,15 @@ const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplate
5
5
const SingleEntryPlugin = require ( 'webpack/lib/SingleEntryPlugin' ) ;
6
6
const loaderUtils = require ( 'loader-utils' ) ;
7
7
8
+ const getWorker = ( file , content , query ) => {
9
+ const workerPublicPath = `__webpack_public_path__ + ${ JSON . stringify ( file ) } ` ;
10
+ if ( query . inline ) {
11
+ const createInlineWorkerPath = JSON . stringify ( `!!${ path . resolve ( 'createInlineWorker.js' ) } ` ) ;
12
+ return `require(${ createInlineWorkerPath } )(${ JSON . stringify ( content ) } , ${ workerPublicPath } )` ;
13
+ }
14
+ return `new Worker(${ workerPublicPath } )` ;
15
+ } ;
16
+
8
17
module . exports = function workerLoader ( ) { } ;
9
18
10
19
module . exports . pitch = function pitch ( request ) {
@@ -45,15 +54,8 @@ module.exports.pitch = function pitch(request) {
45
54
if ( err ) return callback ( err ) ;
46
55
if ( entries [ 0 ] ) {
47
56
const workerFile = entries [ 0 ] . files [ 0 ] ;
48
- let constructor = `new Worker(__webpack_public_path__ + ${ JSON . stringify ( workerFile ) } )` ;
49
- if ( query . inline ) {
50
- constructor = `require(${ JSON . stringify ( `!!${ path . join ( __dirname , 'createInlineWorker.js' ) } ` ) } )(${
51
- JSON . stringify ( compilation . assets [ workerFile ] . source ( ) )
52
- } , __webpack_public_path__ + ${
53
- JSON . stringify ( workerFile )
54
- } )`;
55
- }
56
- return callback ( null , `module.exports = function() {\n\treturn ${ constructor } ;\n};` ) ;
57
+ const workerFactory = getWorker ( workerFile , compilation . assets [ workerFile ] . source ( ) , query ) ;
58
+ return callback ( null , `module.exports = function() {\n\treturn ${ workerFactory } ;\n};` ) ;
57
59
}
58
60
return callback ( null , null ) ;
59
61
} ) ;
0 commit comments