1
1
import { remove , readJson , existsSync , stat , readFile } from 'fs-extra' ;
2
2
import { resolve , dirname , relative , join , parse } from 'path' ;
3
- import { optimize , LoaderTargetPlugin , JsonpTemplatePlugin } from 'webpack' ;
3
+ import { optimize , LoaderTargetPlugin } from 'webpack' ;
4
+ import JsonpTemplatePlugin from 'webpack/lib/web/JsonpTemplatePlugin'
4
5
import { ConcatSource } from 'webpack-sources' ;
5
6
import globby from 'globby' ;
6
7
import { defaults , values , uniq } from 'lodash' ;
@@ -9,7 +10,9 @@ import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
9
10
import FunctionModulePlugin from 'webpack/lib/FunctionModulePlugin' ;
10
11
import NodeSourcePlugin from 'webpack/lib/node/NodeSourcePlugin' ;
11
12
12
- const { CommonsChunkPlugin } = optimize ;
13
+ const pluginName = 'WXAppPlugin' ;
14
+
15
+ const { SplitChunksPlugin } = optimize ;
13
16
14
17
const deprecated = function deprecated ( obj , key , adapter , explain ) {
15
18
if ( deprecated . warned . has ( key ) ) {
@@ -93,38 +96,70 @@ export default class WXAppPlugin {
93
96
94
97
this . enforceTarget ( compiler ) ;
95
98
96
- compiler . plugin (
97
- ' run' ,
98
- this . try ( async compiler => {
99
- await this . run ( compiler ) ;
100
- } )
101
- ) ;
99
+ if ( compiler . hooks ) {
100
+ compiler . hooks . run . tapAsync ( pluginName ,
101
+ this . try ( async compilation => {
102
+ await this . run ( compiler ) ;
103
+ } )
104
+ ) ;
102
105
103
- compiler . plugin (
104
- 'watch-run' ,
105
- this . try ( async compiler => {
106
- await this . run ( compiler . compiler ) ;
107
- } )
108
- ) ;
106
+ compiler . hooks . watchRun . tapAsync ( pluginName ,
107
+ this . try ( async compiler => {
108
+ await this . run ( compiler ) ;
109
+ } )
110
+ ) ;
109
111
110
- compiler . plugin (
111
- 'emit' ,
112
- this . try ( async compilation => {
113
- if ( clear && isFirst ) {
114
- isFirst = false ;
115
- await this . clear ( compilation ) ;
116
- }
112
+ compiler . hooks . emit . tapAsync ( pluginName ,
113
+ this . try ( async compilation => {
114
+ if ( clear && isFirst ) {
115
+ isFirst = false ;
116
+ await this . clear ( compilation ) ;
117
+ }
117
118
118
- await this . toEmitTabBarIcons ( compilation ) ;
119
- } )
120
- ) ;
119
+ await this . toEmitTabBarIcons ( compilation ) ;
120
+ } )
121
+ ) ;
121
122
122
- compiler . plugin (
123
- 'after-emit' ,
124
- this . try ( async compilation => {
125
- await this . toAddTabBarIconsDependencies ( compilation ) ;
126
- } )
127
- ) ;
123
+ compiler . hooks . afterEmit . tapAsync ( pluginName ,
124
+ this . try ( async compilation => {
125
+ await this . toAddTabBarIconsDependencies ( compilation ) ;
126
+ } )
127
+ ) ;
128
+ }
129
+ else {
130
+ compiler . plugin (
131
+ 'run' ,
132
+ this . try ( async compiler => {
133
+ await this . run ( compiler ) ;
134
+ } )
135
+ ) ;
136
+
137
+ compiler . plugin (
138
+ 'watch-run' ,
139
+ this . try ( async compiler => {
140
+ await this . run ( compiler . compiler ) ;
141
+ } )
142
+ ) ;
143
+
144
+ compiler . plugin (
145
+ 'emit' ,
146
+ this . try ( async compilation => {
147
+ if ( clear && isFirst ) {
148
+ isFirst = false ;
149
+ await this . clear ( compilation ) ;
150
+ }
151
+
152
+ await this . toEmitTabBarIcons ( compilation ) ;
153
+ } )
154
+ ) ;
155
+
156
+ compiler . plugin (
157
+ 'after-emit' ,
158
+ this . try ( async compilation => {
159
+ await this . toAddTabBarIconsDependencies ( compilation ) ;
160
+ } )
161
+ ) ;
162
+ }
128
163
}
129
164
130
165
try = handler => async ( arg , callback ) => {
@@ -238,7 +273,7 @@ export default class WXAppPlugin {
238
273
toAddTabBarIconsDependencies ( compilation ) {
239
274
const { fileDependencies } = compilation ;
240
275
this . tabBarIcons . forEach ( iconPath => {
241
- if ( ! ~ fileDependencies . indexOf ( iconPath ) ) {
276
+ if ( ! ~ fileDependencies . has ( iconPath ) ) {
242
277
fileDependencies . push ( iconPath ) ;
243
278
}
244
279
} ) ;
@@ -319,16 +354,25 @@ export default class WXAppPlugin {
319
354
entryResources
320
355
} = this ;
321
356
322
- compiler . plugin ( 'compilation' , compilation => {
323
- compilation . plugin ( 'before-chunk-assets' , ( ) => {
324
- const assetsChunkIndex = compilation . chunks . findIndex (
325
- ( { name } ) => name === assetsChunkName
326
- ) ;
327
- if ( assetsChunkIndex > - 1 ) {
328
- compilation . chunks . splice ( assetsChunkIndex , 1 ) ;
329
- }
357
+ const beforeChunkAssetsHandler = compilation => ( ) => {
358
+ const assetsChunkIndex = compilation . chunks . findIndex (
359
+ ( { name } ) => name === assetsChunkName
360
+ ) ;
361
+ if ( assetsChunkIndex > - 1 ) {
362
+ compilation . chunks . splice ( assetsChunkIndex , 1 )
363
+ }
364
+ } ;
365
+
366
+ if ( compiler . hooks ) {
367
+ compiler . hooks . compilation . tap ( pluginName , compilation => {
368
+ compilation . hooks . beforeChunkAssets . tap ( pluginName , beforeChunkAssetsHandler ( compilation ) ) ;
330
369
} ) ;
331
- } ) ;
370
+ }
371
+ else {
372
+ compiler . plugin ( 'compilation' , compilation => {
373
+ compilation . plugin ( 'before-chunk-assets' , beforeChunkAssetsHandler ( compilation ) ) ;
374
+ } ) ;
375
+ }
332
376
333
377
const patterns = entryResources
334
378
. map ( resource => `${ resource } .*` )
@@ -368,25 +412,33 @@ export default class WXAppPlugin {
368
412
369
413
const scripts = entryResources . map ( ::this . getFullScriptPath ) ;
370
414
371
- compiler . apply (
372
- new CommonsChunkPlugin ( {
373
- name : stripExt ( commonModuleName ) ,
374
- minChunks : ( { resource } ) => {
375
- if ( resource ) {
376
- const regExp = this . getChunkResourceRegExp ( ) ;
377
- return regExp . test ( resource ) && scripts . indexOf ( resource ) < 0 ;
378
- }
379
- return false ;
415
+ const applyOpt = {
416
+ name : stripExt ( commonModuleName ) ,
417
+ minChunks : ( { resource } ) => {
418
+ if ( resource ) {
419
+ const regExp = this . getChunkResourceRegExp ( ) ;
420
+ return regExp . test ( resource ) && scripts . indexOf ( resource ) < 0 ;
380
421
}
381
- } )
422
+ return false ;
423
+ }
424
+ } ;
425
+
426
+ compiler . apply (
427
+ new SplitChunksPlugin ( applyOpt )
382
428
) ;
383
429
}
384
430
385
431
addScriptEntry ( compiler , entry , name ) {
386
- compiler . plugin ( 'make' , ( compilation , callback ) => {
387
- const dep = SingleEntryPlugin . createDependency ( entry , name ) ;
388
- compilation . addEntry ( this . base , dep , name , callback ) ;
389
- } ) ;
432
+ const makeHandler = ( compilation , callback ) => {
433
+ const dep = SingleEntryPlugin . createDependency ( entry , name )
434
+ compilation . addEntry ( this . base , dep , name , callback )
435
+ } ;
436
+ if ( compiler . hooks ) {
437
+ compiler . hooks . make . tapAsync ( pluginName , makeHandler ) ;
438
+ }
439
+ else {
440
+ compiler . plugin ( 'make' , makeHandler ) ;
441
+ }
390
442
}
391
443
392
444
compileScripts ( compiler ) {
@@ -405,45 +457,68 @@ export default class WXAppPlugin {
405
457
const commonChunkName = stripExt ( commonModuleName ) ;
406
458
const globalVar = target . name === 'Alipay' ? 'my' : 'wx' ;
407
459
408
- // inject chunk entries
409
- compilation . chunkTemplate . plugin ( 'render' , ( core , { name } ) => {
460
+ const renderHandler = ( core , { name } ) => {
410
461
if ( this . entryResources . indexOf ( name ) >= 0 ) {
411
- const relativePath = relative ( dirname ( name ) , `./${ commonModuleName } ` ) ;
412
- const posixPath = relativePath . replace ( / \\ / g, '/' ) ;
413
- const source = core . source ( ) ;
462
+ const relativePath = relative ( dirname ( name ) , `./${ commonModuleName } ` )
463
+ const posixPath = relativePath . replace ( / \\ / g, '/' )
464
+ const source = core . source ( )
414
465
415
466
// eslint-disable-next-line max-len
416
- const injectContent = `; function webpackJsonp() { require("./${ posixPath } "); ${ globalVar } .webpackJsonp.apply(null, arguments); }` ;
467
+ const injectContent = `; function webpackJsonp() { require("./${ posixPath } "); ${ globalVar } .webpackJsonp.apply(null, arguments); }`
417
468
418
469
if ( source . indexOf ( injectContent ) < 0 ) {
419
- const concatSource = new ConcatSource ( core ) ;
420
- concatSource . add ( injectContent ) ;
421
- return concatSource ;
470
+ const concatSource = new ConcatSource ( core )
471
+ concatSource . add ( injectContent )
472
+ return concatSource
422
473
}
423
474
}
424
- return core ;
425
- } ) ;
475
+ return core
476
+ } ;
426
477
427
- // replace `window` to `global` in common chunk
428
- compilation . mainTemplate . plugin ( 'bootstrap' , ( source , chunk ) => {
429
- const windowRegExp = new RegExp ( 'window' , 'g' ) ;
478
+ const bootstrapHandler = ( source , chunk ) => {
479
+ const windowRegExp = new RegExp ( 'window' , 'g' )
430
480
if ( chunk . name === commonChunkName ) {
431
- return source . replace ( windowRegExp , globalVar ) ;
481
+ return source . replace ( windowRegExp , globalVar )
432
482
}
433
- return source ;
434
- } ) ;
483
+ return source
484
+ } ;
435
485
436
- // override `require.ensure()`
437
- compilation . mainTemplate . plugin (
438
- 'require-ensure' ,
439
- ( ) => 'throw new Error("Not chunk loading available");'
440
- ) ;
486
+ // inject chunk entries
487
+ if ( compilation . chunkTemplate . hooks ) {
488
+ // inject chunk entries
489
+ compilation . chunkTemplate . hooks . render . tap ( pluginName , renderHandler ) ;
490
+
491
+ // replace `window` to `global` in common chunk
492
+ compilation . mainTemplate . hooks . bootstrap . tap ( pluginName , bootstrapHandler ) ;
493
+
494
+ // override `require.ensure()`
495
+ compilation . mainTemplate . hooks . requireEnsure . tap ( pluginName ,
496
+ ( ) => 'throw new Error("Not chunk loading available");'
497
+ ) ;
498
+ }
499
+ else {
500
+ compilation . chunkTemplate . plugin ( 'render' , renderHandler ) ;
501
+
502
+ // replace `window` to `global` in common chunk
503
+ compilation . mainTemplate . plugin ( 'bootstrap' , bootstrapHandler ) ;
504
+
505
+ // override `require.ensure()`
506
+ compilation . mainTemplate . plugin (
507
+ 'require-ensure' ,
508
+ ( ) => 'throw new Error("Not chunk loading available");'
509
+ ) ;
510
+ }
441
511
}
442
512
443
513
async run ( compiler ) {
444
514
this . base = this . getBase ( compiler ) ;
445
515
this . entryResources = await this . getEntryResource ( ) ;
446
- compiler . plugin ( 'compilation' , ::this . toModifyTemplate ) ;
516
+ if ( compiler . hooks ) {
517
+ compiler . hooks . compilation . tap ( pluginName , ::this . toModifyTemplate ) ;
518
+ }
519
+ else {
520
+ compiler . plugin ( 'compilation' , ::this . toModifyTemplate ) ;
521
+ }
447
522
this . compileScripts ( compiler ) ;
448
523
await this . compileAssets ( compiler ) ;
449
524
}
0 commit comments