@@ -8,14 +8,17 @@ const bannedPluginNames = [
8
8
9
9
module . exports = function workerLoaderPlugin ( config = null ) {
10
10
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 ;
11
13
12
14
const idMap = new Map ( ) ;
13
15
const exclude = new Map ( ) ;
14
16
let projectOptions = null ;
15
17
let basePath = null ;
18
+ let configuredFileName = null ;
16
19
17
20
return {
18
- name : 'worker-loader' ,
21
+ name : 'web- worker-loader' ,
19
22
20
23
options ( options ) {
21
24
if ( ! projectOptions ) {
@@ -37,7 +40,7 @@ module.exports = function workerLoaderPlugin(config = null) {
37
40
38
41
resolveId ( importee , importer ) {
39
42
if ( importee === 'rollup-plugin-web-worker-loader-helper' ) {
40
- return path . resolve ( __dirname , 'createWorkerFactory .js' ) ;
43
+ return path . resolve ( __dirname , 'WorkerLoaderHelper .js' ) ;
41
44
} else if ( importee . indexOf ( 'web-worker:' ) === 0 ) {
42
45
const name = importee . split ( ':' ) [ 1 ] ;
43
46
const folder = path . dirname ( importer ) ;
@@ -46,9 +49,15 @@ module.exports = function workerLoaderPlugin(config = null) {
46
49
47
50
const target = require . resolve ( name , { paths } ) ;
48
51
if ( target && ! idMap . has ( importee ) ) {
49
- idMap . set ( target , Object . assign ( { } , projectOptions , {
52
+ const inputOptions = Object . assign ( { } , projectOptions , {
50
53
input : target ,
51
- } ) ) ;
54
+ } ) ;
55
+
56
+ idMap . set ( target , {
57
+ workerID : `web-worker-${ idMap . size } .js` ,
58
+ chunk : null ,
59
+ inputOptions,
60
+ } ) ;
52
61
53
62
return target ;
54
63
}
@@ -59,7 +68,31 @@ module.exports = function workerLoaderPlugin(config = null) {
59
68
load ( id ) {
60
69
return new Promise ( ( resolve , reject ) => {
61
70
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 ) ;
63
96
exclude . set ( id , true ) ;
64
97
rollup . rollup ( inputOptions ) . then ( bundle => {
65
98
exclude . delete ( id ) ;
@@ -79,12 +112,20 @@ module.exports = function workerLoaderPlugin(config = null) {
79
112
this . addWatchFile ( dep ) ;
80
113
}
81
114
82
- let source = utils . extractSource ( chunk . code , chunk . exports ) ;
83
115
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 ;
86
127
}
87
- resolve ( { code : utils . buildWorkerCode ( source , map ) } ) ;
128
+ resolve ( { code : utils . buildWorkerCode ( source , map , inline ) } ) ;
88
129
} else {
89
130
resolve ( null ) ;
90
131
}
@@ -101,10 +142,34 @@ module.exports = function workerLoaderPlugin(config = null) {
101
142
102
143
transform ( code , id ) {
103
144
if ( idMap . has ( id ) && ! exclude . has ( id ) ) {
104
- const inputOptions = idMap . get ( id ) ;
145
+ const { inputOptions} = idMap . get ( id ) ;
105
146
return { code, map : `{"version":3,"file":"${ path . basename ( inputOptions . input ) } ","sources":[],"sourcesContent":[],"names":[],"mappings":""}` } ;
106
147
}
107
148
return null ;
108
149
} ,
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
+ } ,
109
174
} ;
110
175
} ;
0 commit comments