1
- import { stringifyRequest } from 'loader-utils' ;
2
-
3
1
import { workerGenerator , sourceMappingURLRegex } from './utils' ;
4
2
5
3
export default function runAsChild (
@@ -8,78 +6,73 @@ export default function runAsChild(
8
6
options ,
9
7
callback
10
8
) {
11
- // eslint-disable-next-line import/no-unresolved, global-require
12
- const getLazyHashedEtag = require ( 'webpack/lib/cache/getLazyHashedEtag' ) ;
13
-
14
9
workerContext . compiler . runAsChild ( ( error , entries , compilation ) => {
15
10
if ( error ) {
16
11
return callback ( error ) ;
17
12
}
18
13
19
14
if ( entries [ 0 ] ) {
20
15
const [ workerFilename ] = [ ...entries [ 0 ] . files ] ;
21
- const requestIdent = stringifyRequest (
22
- { context : loaderContext . rootContext } ,
23
- workerContext . request
16
+ const cache = workerContext . compiler . getCache ( 'worker-loader' ) ;
17
+ const cacheIdent = workerFilename ;
18
+ const cacheETag = cache . getLazyHashedEtag (
19
+ compilation . assets [ workerFilename ]
24
20
) ;
25
- const cacheIdent = `${ workerContext . compiler . compilerPath } /worker-loader|${ requestIdent } ` ;
26
- const cacheETag = getLazyHashedEtag ( compilation . assets [ workerFilename ] ) ;
27
21
28
- // TODO not working, need fix on webpack@5 side
29
- return workerContext . compiler . cache . get (
30
- cacheIdent ,
31
- cacheETag ,
32
- ( getCacheError , content ) => {
33
- if ( getCacheError ) {
34
- return callback ( getCacheError ) ;
35
- }
36
-
37
- if ( options . inline === 'no-fallback' ) {
38
- // eslint-disable-next-line no-underscore-dangle, no-param-reassign
39
- delete loaderContext . _compilation . assets [ workerFilename ] ;
22
+ return cache . get ( cacheIdent , cacheETag , ( getCacheError , content ) => {
23
+ if ( getCacheError ) {
24
+ return callback ( getCacheError ) ;
25
+ }
40
26
41
- // TODO improve this, we should store generated source maps files for file in `assetInfo`
42
- // eslint-disable-next-line no-underscore-dangle
43
- if ( loaderContext . _compilation . assets [ `${ workerFilename } .map` ] ) {
44
- // eslint-disable-next-line no-underscore-dangle, no-param-reassign
45
- delete loaderContext . _compilation . assets [ `${ workerFilename } .map` ] ;
46
- }
47
- }
27
+ if ( options . inline === 'no-fallback' ) {
28
+ // eslint-disable-next-line no-underscore-dangle, no-param-reassign
29
+ delete loaderContext . _compilation . assets [ workerFilename ] ;
48
30
49
- if ( content ) {
50
- return callback ( null , content ) ;
31
+ // TODO improve this, we should store generated source maps files for file in `assetInfo`
32
+ // eslint-disable-next-line no-underscore-dangle
33
+ if ( loaderContext . _compilation . assets [ `${ workerFilename } .map` ] ) {
34
+ // eslint-disable-next-line no-underscore-dangle, no-param-reassign
35
+ delete loaderContext . _compilation . assets [ `${ workerFilename } .map` ] ;
51
36
}
37
+ }
52
38
53
- let workerSource = compilation . assets [ workerFilename ] . source ( ) ;
39
+ if ( content ) {
40
+ return callback ( null , content ) ;
41
+ }
54
42
55
- if ( options . inline === 'no-fallback' ) {
56
- // Remove `/* sourceMappingURL=url */` comment
57
- workerSource = workerSource . replace ( sourceMappingURLRegex , '' ) ;
58
- }
43
+ let workerSource = compilation . assets [ workerFilename ] . source ( ) ;
59
44
60
- const workerCode = workerGenerator (
61
- loaderContext ,
62
- workerFilename ,
63
- workerSource ,
64
- options
65
- ) ;
45
+ if ( options . inline === 'no-fallback' ) {
46
+ // Remove `/* sourceMappingURL=url */` comment
47
+ workerSource = workerSource . replace ( sourceMappingURLRegex , '' ) ;
48
+ }
66
49
67
- return workerContext . compiler . cache . store (
68
- cacheIdent ,
69
- cacheETag ,
70
- workerCode ,
71
- ( storeCacheError ) => {
72
- if ( storeCacheError ) {
73
- return callback ( storeCacheError ) ;
74
- }
50
+ const workerCode = workerGenerator (
51
+ loaderContext ,
52
+ workerFilename ,
53
+ workerSource ,
54
+ options
55
+ ) ;
75
56
76
- return callback ( null , workerCode ) ;
57
+ return cache . store (
58
+ cacheIdent ,
59
+ cacheETag ,
60
+ workerCode ,
61
+ ( storeCacheError ) => {
62
+ if ( storeCacheError ) {
63
+ return callback ( storeCacheError ) ;
77
64
}
78
- ) ;
79
- }
80
- ) ;
65
+
66
+ return callback ( null , workerCode ) ;
67
+ }
68
+ ) ;
69
+ } ) ;
81
70
}
82
71
83
- return callback ( null , null ) ;
72
+ return callback (
73
+ new Error (
74
+ `Failed to compile web worker "${ workerContext . request } " request`
75
+ )
76
+ ) ;
84
77
} ) ;
85
78
}
0 commit comments