Skip to content

Commit c78a460

Browse files
valorkinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): fixes optimizeChunkAssets is deprecated in webpack 5
1 parent 850a0ae commit c78a460

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

packages/angular_devkit/build_angular/src/webpack/plugins/optimize-css-webpack-plugin.ts

+35-21
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,49 @@
88
import * as cssNano from 'cssnano';
99
import { ProcessOptions, Result } from 'postcss';
1010
import { Compiler, compilation } from 'webpack';
11-
import { RawSource, SourceMapSource } from 'webpack-sources';
11+
import { RawSource, Source, SourceMapSource } from 'webpack-sources';
1212
import { addWarning } from '../../utils/webpack-diagnostics';
13+
import { isWebpackFiveOrHigher } from '../../utils/webpack-version';
1314

1415
export interface OptimizeCssWebpackPluginOptions {
1516
sourceMap: boolean;
1617
test: (file: string) => boolean;
1718
}
1819

20+
const PLUGIN_NAME = 'optimize-css-webpack-plugin';
21+
1922
function hook(
2023
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>,
2525
) {
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+
}
3054
});
3155
}
3256

@@ -42,18 +66,8 @@ export class OptimizeCssWebpackPlugin {
4266
}
4367

4468
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];
5771

5872
const actions = files
5973
.filter(file => this._options.test(file))

0 commit comments

Comments
 (0)