@@ -8,14 +8,17 @@ const bannedPluginNames = [
88
99module . exports = function workerLoaderPlugin ( config = null ) {
1010 const sourcemap = ( config && config . sourcemap ) || false ;
11+ const loadPath = config && config . hasOwnProperty ( 'loadPath' ) ? config . loadPath : '' ;
12+ let inline = config && config . hasOwnProperty ( 'inline' ) ? config . inline : true ;
1113
1214 const idMap = new Map ( ) ;
1315 const exclude = new Map ( ) ;
1416 let projectOptions = null ;
1517 let basePath = null ;
18+ let configuredFileName = null ;
1619
1720 return {
18- name : 'worker-loader' ,
21+ name : 'web- worker-loader' ,
1922
2023 options ( options ) {
2124 if ( ! projectOptions ) {
@@ -37,7 +40,7 @@ module.exports = function workerLoaderPlugin(config = null) {
3740
3841 resolveId ( importee , importer ) {
3942 if ( importee === 'rollup-plugin-web-worker-loader-helper' ) {
40- return path . resolve ( __dirname , 'createWorkerFactory .js' ) ;
43+ return path . resolve ( __dirname , 'WorkerLoaderHelper .js' ) ;
4144 } else if ( importee . indexOf ( 'web-worker:' ) === 0 ) {
4245 const name = importee . split ( ':' ) [ 1 ] ;
4346 const folder = path . dirname ( importer ) ;
@@ -46,9 +49,15 @@ module.exports = function workerLoaderPlugin(config = null) {
4649
4750 const target = require . resolve ( name , { paths } ) ;
4851 if ( target && ! idMap . has ( importee ) ) {
49- idMap . set ( target , Object . assign ( { } , projectOptions , {
52+ const inputOptions = Object . assign ( { } , projectOptions , {
5053 input : target ,
51- } ) ) ;
54+ } ) ;
55+
56+ idMap . set ( target , {
57+ workerID : `web-worker-${ idMap . size } .js` ,
58+ chunk : null ,
59+ inputOptions,
60+ } ) ;
5261
5362 return target ;
5463 }
@@ -59,7 +68,31 @@ module.exports = function workerLoaderPlugin(config = null) {
5968 load ( id ) {
6069 return new Promise ( ( resolve , reject ) => {
6170 if ( idMap . has ( id ) && ! exclude . has ( id ) ) {
62- const inputOptions = idMap . get ( id ) ;
71+ if ( ! inline ) {
72+ /* inline requires rollup version 1.9.2 or higher */
73+ const version = this . meta . rollupVersion . split ( '.' ) ;
74+ if ( version . length !== 3 ) {
75+ this . warn ( 'Unknown rollup version' ) ;
76+ inline = true ;
77+ } else {
78+ const major = parseInt ( version [ 0 ] , 10 ) ;
79+ const minor = parseInt ( version [ 1 ] , 10 ) ;
80+ const patch = parseInt ( version [ 2 ] , 10 ) ;
81+ if (
82+ isNaN ( major ) ||
83+ isNaN ( minor ) ||
84+ isNaN ( patch ) ||
85+ major < 1 ||
86+ minor < 9 ||
87+ patch < 2
88+ ) {
89+ this . warn ( `Rollup version 1.9.2 or higher is required for emitting a worker file (current version:${ this . meta . rollupVersion } ). See https://github.com/rollup/rollup/issues/2801` ) ;
90+ inline = true ;
91+ }
92+ }
93+ }
94+
95+ const { inputOptions, workerID} = idMap . get ( id ) ;
6396 exclude . set ( id , true ) ;
6497 rollup . rollup ( inputOptions ) . then ( bundle => {
6598 exclude . delete ( id ) ;
@@ -79,12 +112,20 @@ module.exports = function workerLoaderPlugin(config = null) {
79112 this . addWatchFile ( dep ) ;
80113 }
81114
82- let source = utils . extractSource ( chunk . code , chunk . exports ) ;
83115 let map = null ;
84- if ( sourcemap ) {
85- map = utils . fixMapSources ( chunk , basePath ) ;
116+ let source ;
117+ if ( inline ) {
118+ source = utils . extractSource ( chunk . code , chunk . exports ) ;
119+ map = null ;
120+ if ( sourcemap ) {
121+ map = utils . fixMapSources ( chunk , basePath ) ;
122+ }
123+ } else {
124+ source = path . join ( loadPath , workerID ) ;
125+ chunk . fileName = workerID ;
126+ idMap . get ( id ) . chunk = chunk ;
86127 }
87- resolve ( { code : utils . buildWorkerCode ( source , map ) } ) ;
128+ resolve ( { code : utils . buildWorkerCode ( source , map , inline ) } ) ;
88129 } else {
89130 resolve ( null ) ;
90131 }
@@ -101,10 +142,34 @@ module.exports = function workerLoaderPlugin(config = null) {
101142
102143 transform ( code , id ) {
103144 if ( idMap . has ( id ) && ! exclude . has ( id ) ) {
104- const inputOptions = idMap . get ( id ) ;
145+ const { inputOptions} = idMap . get ( id ) ;
105146 return { code, map : `{"version":3,"file":"${ path . basename ( inputOptions . input ) } ","sources":[],"sourcesContent":[],"names":[],"mappings":""}` } ;
106147 }
107148 return null ;
108149 } ,
150+
151+ outputOptions ( options ) {
152+ if ( ! inline && options . file && ! options . dir ) {
153+ configuredFileName = path . basename ( options . file ) ;
154+ return Object . assign ( { } , options , {
155+ file : null ,
156+ dir : path . dirname ( options . file ) ,
157+ } ) ;
158+ }
159+ return null ;
160+ } ,
161+
162+ generateBundle ( options , bundle , isWrite ) {
163+ if ( ! inline && isWrite ) {
164+ if ( configuredFileName && Object . keys ( bundle ) . length === 1 ) {
165+ bundle [ Object . keys ( bundle ) [ 0 ] ] . fileName = configuredFileName ;
166+ }
167+ for ( const worker of idMap ) {
168+ if ( worker [ 1 ] . chunk && ! bundle [ worker [ 1 ] . workerID ] ) {
169+ bundle [ worker [ 1 ] . workerID ] = worker [ 1 ] . chunk ;
170+ }
171+ }
172+ }
173+ } ,
109174 } ;
110175} ;
0 commit comments