@@ -8,6 +8,28 @@ import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlug
8
8
export default function loader ( ) { }
9
9
10
10
const CACHE = { } ;
11
+ const tapName = 'workerize-loader' ;
12
+
13
+ function compilationHook ( compiler , handler ) {
14
+ if ( compiler . hooks ) {
15
+ return compiler . hooks . compilation . tap ( tapName , handler ) ;
16
+ }
17
+ return compiler . plugin ( 'compilation' , handler ) ;
18
+ }
19
+
20
+ function parseHook ( data , handler ) {
21
+ if ( data . normalModuleFactory . hooks ) {
22
+ return data . normalModuleFactory . hooks . parser . for ( 'javascript/auto' ) . tap ( tapName , handler ) ;
23
+ }
24
+ return data . normalModuleFactory . plugin ( 'parser' , handler ) ;
25
+ }
26
+
27
+ function exportDeclarationHook ( parser , handler ) {
28
+ if ( parser . hooks ) {
29
+ return parser . hooks . exportDeclaration . tap ( tapName , handler ) ;
30
+ }
31
+ return parser . plugin ( 'export declaration' , handler ) ;
32
+ }
11
33
12
34
loader . pitch = function ( request ) {
13
35
this . cacheable ( false ) ;
@@ -30,26 +52,25 @@ loader.pitch = function(request) {
30
52
} ;
31
53
32
54
worker . compiler = this . _compilation . createChildCompiler ( 'worker' , worker . options ) ;
33
-
34
- worker . compiler . apply ( new WebWorkerTemplatePlugin ( worker . options ) ) ;
55
+
56
+ ( new WebWorkerTemplatePlugin ( worker . options ) ) . apply ( worker . compiler ) ;
35
57
36
58
if ( this . target !== 'webworker' && this . target !== 'web' ) {
37
- worker . compiler . apply ( new NodeTargetPlugin ( ) ) ;
59
+ ( new NodeTargetPlugin ( ) ) . apply ( worker . compiler ) ;
38
60
}
39
61
40
- worker . compiler . apply ( new SingleEntryPlugin ( this . context , `!!${ path . resolve ( __dirname , 'rpc-worker-loader.js' ) } !${ request } ` , 'main' ) ) ;
62
+ ( new SingleEntryPlugin ( this . context , `!!${ path . resolve ( __dirname , 'rpc-worker-loader.js' ) } !${ request } ` , 'main' ) ) . apply ( worker . compiler ) ;
41
63
42
64
const subCache = `subcache ${ __dirname } ${ request } ` ;
43
65
44
- worker . compiler . plugin ( 'compilation' , ( compilation , data ) => {
66
+ compilationHook ( worker . compiler , ( compilation , data ) => {
45
67
if ( compilation . cache ) {
46
68
if ( ! compilation . cache [ subCache ] ) compilation . cache [ subCache ] = { } ;
47
69
48
70
compilation . cache = compilation . cache [ subCache ] ;
49
71
}
50
-
51
- data . normalModuleFactory . plugin ( 'parser' , ( parser , options ) => {
52
- parser . plugin ( 'export declaration' , expr => {
72
+ parseHook ( data , ( parser , options ) => {
73
+ exportDeclarationHook ( parser , expr => {
53
74
let decl = expr . declaration || expr ,
54
75
{ compilation, current } = parser . state ,
55
76
entry = compilation . entries [ 0 ] . resource ;
0 commit comments