@@ -38,7 +38,7 @@ loader.pitch = function(request) {
3838
3939 const cb = this . async ( ) ;
4040
41- const filename = loaderUtils . interpolateName ( this , `${ options . name || '[hash ]' } .worker.js` , {
41+ const filename = loaderUtils . interpolateName ( this , `${ options . name || '[fullhash ]' } .worker.js` , {
4242 context : options . context || this . rootContext || this . options . context ,
4343 regExp : options . regExp
4444 } ) ;
@@ -89,22 +89,37 @@ loader.pitch = function(request) {
8989
9090 compilationHook ( worker . compiler , ( compilation , data ) => {
9191 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 ;
95105 }
96106 parseHook ( data , ( parser , options ) => {
97107 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 ] ;
101117
102118 // only process entry exports
103- if ( current . resource !== entry ) return ;
119+ if ( current . resource !== entryModule . resource ) return ;
104120
105121 let key = current . nameForCondition ( ) ;
106122 let exports = CACHE [ key ] || ( CACHE [ key ] = { } ) ;
107-
108123 if ( decl . id ) {
109124 exports [ decl . id . name ] = true ;
110125 }
@@ -116,6 +131,20 @@ loader.pitch = function(request) {
116131 else {
117132 console . warn ( '[workerize] unknown export declaration: ' , expr ) ;
118133 }
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+ }
119148 } ) ;
120149 } ) ;
121150 } ) ;
@@ -124,9 +153,20 @@ loader.pitch = function(request) {
124153 if ( err ) return cb ( err ) ;
125154
126155 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 ( ) ;
130170 let contents = compilation . assets [ worker . file ] . source ( ) ;
131171 let exports = Object . keys ( CACHE [ key ] || { } ) ;
132172
0 commit comments