@@ -4,6 +4,8 @@ import loaderUtils from 'loader-utils';
4
4
import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin' ;
5
5
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin' ;
6
6
import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin' ;
7
+ import { getEntryRuntime } from 'webpack/lib/util/runtime' ;
8
+ import { UsageState } from 'webpack' ;
7
9
8
10
export default function loader ( ) { }
9
11
@@ -38,7 +40,7 @@ loader.pitch = function(request) {
38
40
39
41
const cb = this . async ( ) ;
40
42
41
- const filename = loaderUtils . interpolateName ( this , `${ options . name || '[hash ]' } .worker.js` , {
43
+ const filename = loaderUtils . interpolateName ( this , `${ options . name || '[fullhash ]' } .worker.js` , {
42
44
context : options . context || this . rootContext || this . options . context ,
43
45
regExp : options . regExp
44
46
} ) ;
@@ -89,22 +91,37 @@ loader.pitch = function(request) {
89
91
90
92
compilationHook ( worker . compiler , ( compilation , data ) => {
91
93
if ( compilation . cache ) {
92
- if ( ! compilation . cache [ subCache ] ) compilation . cache [ subCache ] = { } ;
93
-
94
- compilation . cache = compilation . cache [ subCache ] ;
94
+ let cache ;
95
+ if ( compilation . cache instanceof Map ) {
96
+ cache = compilation . cache . get ( subCache ) ;
97
+ if ( ! cache ) {
98
+ cache = new Map ( ) ;
99
+ compilation . cache . set ( subCache , cache ) ;
100
+ }
101
+ }
102
+ else if ( ! compilation . cache [ subCache ] ) {
103
+ cache = compilation . cache [ subCache ] = { } ;
104
+ }
105
+
106
+ compilation . cache = cache ;
95
107
}
96
108
parseHook ( data , ( parser , options ) => {
97
109
exportDeclarationHook ( parser , expr => {
98
- let decl = expr . declaration || expr ,
99
- { compilation, current } = parser . state ,
100
- entry = compilation . entries [ 0 ] . resource ;
110
+ let decl = expr . declaration || expr ;
111
+ let { compilation, current } = parser . state ;
112
+
113
+ let entryModule =
114
+ compilation . entries instanceof Map
115
+ ? compilation . moduleGraph . getModule (
116
+ compilation . entries . get ( 'main' ) . dependencies [ 0 ]
117
+ )
118
+ : compilation . entries [ 0 ] ;
101
119
102
120
// only process entry exports
103
- if ( current . resource !== entry ) return ;
121
+ if ( current . resource !== entryModule . resource ) return ;
104
122
105
123
let key = current . nameForCondition ( ) ;
106
124
let exports = CACHE [ key ] || ( CACHE [ key ] = { } ) ;
107
-
108
125
if ( decl . id ) {
109
126
exports [ decl . id . name ] = true ;
110
127
}
@@ -116,6 +133,17 @@ loader.pitch = function(request) {
116
133
else {
117
134
console . warn ( '[workerize] unknown export declaration: ' , expr ) ;
118
135
}
136
+
137
+ if ( compilation . moduleGraph ) {
138
+ const runtime = getEntryRuntime ( compilation , 'main' ) ;
139
+ for ( const exportName of Object . keys ( exports ) ) {
140
+ const exportInfo = compilation . moduleGraph . getExportInfo ( entryModule , exportName ) ;
141
+ exportInfo . setUsed ( UsageState . Used , runtime ) ;
142
+ exportInfo . canMangleUse = false ;
143
+ exportInfo . canMangleProvide = false ;
144
+ }
145
+ compilation . moduleGraph . addExtraReason ( entryModule , 'used by workerize-loader' ) ;
146
+ }
119
147
} ) ;
120
148
} ) ;
121
149
} ) ;
@@ -124,9 +152,20 @@ loader.pitch = function(request) {
124
152
if ( err ) return cb ( err ) ;
125
153
126
154
if ( entries [ 0 ] ) {
127
- worker . file = entries [ 0 ] . files [ 0 ] ;
128
-
129
- let key = entries [ 0 ] . entryModule . nameForCondition ( ) ;
155
+ worker . file = Array . from ( entries [ 0 ] . files ) [ 0 ] ;
156
+ const entryModules =
157
+ compilation . chunkGraph &&
158
+ compilation . chunkGraph . getChunkEntryModulesIterable
159
+ ? Array . from (
160
+ compilation . chunkGraph . getChunkEntryModulesIterable ( entries [ 0 ] )
161
+ )
162
+ : null ;
163
+ const entryModule =
164
+ entryModules && entryModules . length > 0
165
+ ? entryModules [ 0 ]
166
+ : entries [ 0 ] . entryModule ;
167
+
168
+ let key = entryModule . nameForCondition ( ) ;
130
169
let contents = compilation . assets [ worker . file ] . source ( ) ;
131
170
let exports = Object . keys ( CACHE [ key ] || { } ) ;
132
171
0 commit comments