11import { remove , readJson , existsSync , stat , readFile } from 'fs-extra' ;
22import { 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'
45import { ConcatSource } from 'webpack-sources' ;
56import globby from 'globby' ;
67import { defaults , values , uniq } from 'lodash' ;
@@ -9,7 +10,9 @@ import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
910import FunctionModulePlugin from 'webpack/lib/FunctionModulePlugin' ;
1011import NodeSourcePlugin from 'webpack/lib/node/NodeSourcePlugin' ;
1112
12- const { CommonsChunkPlugin } = optimize ;
13+ const pluginName = 'WXAppPlugin' ;
14+
15+ const { SplitChunksPlugin } = optimize ;
1316
1417const deprecated = function deprecated ( obj , key , adapter , explain ) {
1518 if ( deprecated . warned . has ( key ) ) {
@@ -93,38 +96,70 @@ export default class WXAppPlugin {
9396
9497 this . enforceTarget ( compiler ) ;
9598
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+ ) ;
102105
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+ ) ;
109111
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+ }
117118
118- await this . toEmitTabBarIcons ( compilation ) ;
119- } )
120- ) ;
119+ await this . toEmitTabBarIcons ( compilation ) ;
120+ } )
121+ ) ;
121122
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+ }
128163 }
129164
130165 try = handler => async ( arg , callback ) => {
@@ -238,7 +273,7 @@ export default class WXAppPlugin {
238273 toAddTabBarIconsDependencies ( compilation ) {
239274 const { fileDependencies } = compilation ;
240275 this . tabBarIcons . forEach ( iconPath => {
241- if ( ! ~ fileDependencies . indexOf ( iconPath ) ) {
276+ if ( ! ~ fileDependencies . has ( iconPath ) ) {
242277 fileDependencies . push ( iconPath ) ;
243278 }
244279 } ) ;
@@ -319,16 +354,25 @@ export default class WXAppPlugin {
319354 entryResources
320355 } = this ;
321356
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 ) ) ;
330369 } ) ;
331- } ) ;
370+ }
371+ else {
372+ compiler . plugin ( 'compilation' , compilation => {
373+ compilation . plugin ( 'before-chunk-assets' , beforeChunkAssetsHandler ( compilation ) ) ;
374+ } ) ;
375+ }
332376
333377 const patterns = entryResources
334378 . map ( resource => `${ resource } .*` )
@@ -368,25 +412,33 @@ export default class WXAppPlugin {
368412
369413 const scripts = entryResources . map ( ::this . getFullScriptPath ) ;
370414
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 ;
380421 }
381- } )
422+ return false ;
423+ }
424+ } ;
425+
426+ compiler . apply (
427+ new SplitChunksPlugin ( applyOpt )
382428 ) ;
383429 }
384430
385431 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+ }
390442 }
391443
392444 compileScripts ( compiler ) {
@@ -405,45 +457,68 @@ export default class WXAppPlugin {
405457 const commonChunkName = stripExt ( commonModuleName ) ;
406458 const globalVar = target . name === 'Alipay' ? 'my' : 'wx' ;
407459
408- // inject chunk entries
409- compilation . chunkTemplate . plugin ( 'render' , ( core , { name } ) => {
460+ const renderHandler = ( core , { name } ) => {
410461 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 ( )
414465
415466 // 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); }`
417468
418469 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
422473 }
423474 }
424- return core ;
425- } ) ;
475+ return core
476+ } ;
426477
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' )
430480 if ( chunk . name === commonChunkName ) {
431- return source . replace ( windowRegExp , globalVar ) ;
481+ return source . replace ( windowRegExp , globalVar )
432482 }
433- return source ;
434- } ) ;
483+ return source
484+ } ;
435485
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+ }
441511 }
442512
443513 async run ( compiler ) {
444514 this . base = this . getBase ( compiler ) ;
445515 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+ }
447522 this . compileScripts ( compiler ) ;
448523 await this . compileAssets ( compiler ) ;
449524 }
0 commit comments