1717import util from 'util' ;
1818import { gzip } from 'zlib' ;
1919import { promises as fs } from 'fs' ;
20- import * as webpack from 'webpack' ;
20+ import * as defaultWebpack from 'webpack' ;
2121import { SourceMapSource , RawSource } from 'webpack-sources' ;
2222import { rollup } from 'rollup' ;
2323import commonjsPlugin from '@rollup/plugin-commonjs' ;
@@ -75,7 +75,8 @@ export default class OptimizePlugin {
7575 /**
7676 * @param {Partial<DEFAULT_OPTIONS>? } [options]
7777 */
78- constructor ( options ) {
78+ constructor ( options , webpack = defaultWebpack ) {
79+ this . webpack = webpack ;
7980 this . options = Object . assign ( { } , options || { } ) ;
8081 for ( const i in DEFAULT_OPTIONS ) {
8182 if ( this . options [ i ] == null ) this . options [ i ] = DEFAULT_OPTIONS [ i ] ;
@@ -104,14 +105,18 @@ export default class OptimizePlugin {
104105 }
105106
106107 isWebpack4 ( ) {
107- return webpack . version [ 0 ] === '4' ;
108+ return this . webpack . version [ 0 ] === '4' ;
109+ }
110+
111+ isWebpack5 ( ) {
112+ return this . webpack . version [ 0 ] === '5' ;
108113 }
109114
110115 serializeOptions ( ) {
111116 return this . _serialized || ( this . _serialized = JSON . stringify ( this . options ) ) ;
112117 }
113118
114- async optimize ( compiler , compilation , chunks ) {
119+ async optimize ( compiler , compilation , chunkFiles ) {
115120 const cwd = compiler . context ;
116121 const { timings, start, end } = createPerformanceTimings ( ) ;
117122
@@ -124,10 +129,6 @@ export default class OptimizePlugin {
124129 } ;
125130
126131 const processing = new WeakMap ( ) ;
127- const chunkFiles = Array . from ( chunks ) . reduce (
128- ( acc , chunk ) => acc . concat ( Array . from ( chunk . files || [ ] ) ) ,
129- [ ]
130- ) ;
131132 const chunkAssets = Array . from ( compilation . additionalChunkAssets || [ ] ) ;
132133 const files = [ ...chunkFiles , ...chunkAssets ] ;
133134
@@ -150,7 +151,7 @@ export default class OptimizePlugin {
150151 const original = { file, source, map, options } ;
151152 // @ts -ignore-next
152153 const result = this . workerPool . enqueue ( original ) ;
153- pending = result . then ( this . buildResultSources . bind ( this , original ) ) ;
154+ pending = result . then ( this . buildResultSources . bind ( this , original ) ) . catch ( console . error ) ;
154155 processing . set ( asset , pending ) ;
155156
156157 const t = ` └ ${ file } ` ;
@@ -449,14 +450,33 @@ export default class OptimizePlugin {
449450 compilation . chunkTemplate . hooks . hashForChunk . tap ( NAME , updateWithHash . bind ( null , null ) ) ;
450451 } else {
451452 // @ts -ignore
452- webpack . javascript . JavascriptModulesPlugin . getCompilationHooks ( compilation ) . chunkHash . tap ( NAME , updateWithHash ) ;
453+ this . webpack . javascript . JavascriptModulesPlugin . getCompilationHooks ( compilation ) . chunkHash . tap ( NAME , updateWithHash ) ;
453454 }
454455 }
455456
456457 apply ( compiler ) {
457458 compiler . hooks . compilation . tap ( NAME , compilation => {
458459 this . updateChunkHash ( compilation ) ;
459- compilation . hooks . optimizeChunkAssets . tapPromise ( NAME , this . optimize . bind ( this , compiler , compilation ) ) ;
460+
461+ if ( this . isWebpack5 ( ) ) {
462+ compilation . hooks . processAssets . tapPromise ( {
463+ name : NAME ,
464+ stage : this . webpack . Compilation . PROCESS_ASSETS_STAGE_OPTIMIZE
465+ } , ( assets ) => {
466+ const chunkFiles = Object . keys ( assets ) ;
467+
468+ return this . optimize ( compiler , compilation , chunkFiles ) ;
469+ } ) ;
470+ } else {
471+ compilation . hooks . optimizeChunkAssets . tapPromise ( NAME , ( chunks ) => {
472+ const chunkFiles = Array . from ( chunks ) . reduce (
473+ ( acc , chunk ) => acc . concat ( Array . from ( chunk . files || [ ] ) ) ,
474+ [ ]
475+ ) ;
476+
477+ return this . optimize ( compiler , compilation , chunkFiles ) ;
478+ } ) ;
479+ }
460480 } ) ;
461481 }
462482}
0 commit comments