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