8
8
import * as cssNano from 'cssnano' ;
9
9
import { ProcessOptions , Result } from 'postcss' ;
10
10
import { Compiler , compilation } from 'webpack' ;
11
- import { RawSource , SourceMapSource } from 'webpack-sources' ;
11
+ import { RawSource , Source , SourceMapSource } from 'webpack-sources' ;
12
12
import { addWarning } from '../../utils/webpack-diagnostics' ;
13
+ import { isWebpackFiveOrHigher } from '../../utils/webpack-version' ;
13
14
14
15
export interface OptimizeCssWebpackPluginOptions {
15
16
sourceMap : boolean ;
16
17
test : ( file : string ) => boolean ;
17
18
}
18
19
20
+ const PLUGIN_NAME = 'optimize-css-webpack-plugin' ;
21
+
19
22
function hook (
20
23
compiler : Compiler ,
21
- action : (
22
- compilation : compilation . Compilation ,
23
- chunks : Iterable < compilation . Chunk > ,
24
- ) => Promise < void > ,
24
+ action : ( compilation : compilation . Compilation , assetsURI : string [ ] ) => Promise < void > ,
25
25
) {
26
- compiler . hooks . compilation . tap ( 'optimize-css-webpack-plugin' , ( compilation ) => {
27
- compilation . hooks . optimizeChunkAssets . tapPromise ( 'optimize-css-webpack-plugin' , ( chunks ) =>
28
- action ( compilation , chunks ) ,
29
- ) ;
26
+ compiler . hooks . compilation . tap ( PLUGIN_NAME , ( compilation ) => {
27
+ if ( isWebpackFiveOrHigher ( ) ) {
28
+ // webpack 5 migration "guide"
29
+ // https://github.com/webpack/webpack/blob/07fc554bef5930f8577f91c91a8b81791fc29746/lib/Compilation.js#L527-L532
30
+ // TODO_WEBPACK_5 const stage = Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE;
31
+ const stage = 100 ;
32
+ // tslint:disable-next-line: no-any
33
+ ( compilation . hooks as any )
34
+ . processAssets . tapPromise ( { name : PLUGIN_NAME , stage} , ( assets : Record < string , Source > ) => {
35
+ return action ( compilation , Object . keys ( assets ) ) ;
36
+ } ) ;
37
+ } else {
38
+ compilation . hooks . optimizeChunkAssets
39
+ . tapPromise ( PLUGIN_NAME , ( chunks : compilation . Chunk [ ] ) => {
40
+ const files : string [ ] = [ ] ;
41
+ for ( const chunk of chunks ) {
42
+ if ( ! chunk . files ) {
43
+ continue ;
44
+ }
45
+
46
+ for ( const file of chunk . files ) {
47
+ files . push ( file ) ;
48
+ }
49
+ }
50
+
51
+ return action ( compilation , files ) ;
52
+ } ) ;
53
+ }
30
54
} ) ;
31
55
}
32
56
@@ -42,18 +66,8 @@ export class OptimizeCssWebpackPlugin {
42
66
}
43
67
44
68
apply ( compiler : Compiler ) : void {
45
- hook ( compiler , ( compilation : compilation . Compilation , chunks : Iterable < compilation . Chunk > ) => {
46
- const files : string [ ] = [ ...compilation . additionalChunkAssets ] ;
47
-
48
- for ( const chunk of chunks ) {
49
- if ( ! chunk . files ) {
50
- continue ;
51
- }
52
-
53
- for ( const file of chunk . files ) {
54
- files . push ( file ) ;
55
- }
56
- }
69
+ hook ( compiler , ( compilation : compilation . Compilation , assetsURI : string [ ] ) => {
70
+ const files = [ ...compilation . additionalChunkAssets , ...assetsURI ] ;
57
71
58
72
const actions = files
59
73
. filter ( file => this . _options . test ( file ) )
0 commit comments