@@ -8,6 +8,28 @@ import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlug
88export default function loader ( ) { }
99
1010const 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+ }
1133
1234loader . pitch = function ( request ) {
1335 this . cacheable ( false ) ;
@@ -30,26 +52,25 @@ loader.pitch = function(request) {
3052 } ;
3153
3254 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 ) ;
3557
3658 if ( this . target !== 'webworker' && this . target !== 'web' ) {
37- worker . compiler . apply ( new NodeTargetPlugin ( ) ) ;
59+ ( new NodeTargetPlugin ( ) ) . apply ( worker . compiler ) ;
3860 }
3961
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 ) ;
4163
4264 const subCache = `subcache ${ __dirname } ${ request } ` ;
4365
44- worker . compiler . plugin ( 'compilation' , ( compilation , data ) => {
66+ compilationHook ( worker . compiler , ( compilation , data ) => {
4567 if ( compilation . cache ) {
4668 if ( ! compilation . cache [ subCache ] ) compilation . cache [ subCache ] = { } ;
4769
4870 compilation . cache = compilation . cache [ subCache ] ;
4971 }
50-
51- data . normalModuleFactory . plugin ( 'parser' , ( parser , options ) => {
52- parser . plugin ( 'export declaration' , expr => {
72+ parseHook ( data , ( parser , options ) => {
73+ exportDeclarationHook ( parser , expr => {
5374 let decl = expr . declaration || expr ,
5475 { compilation, current } = parser . state ,
5576 entry = compilation . entries [ 0 ] . resource ;
0 commit comments