Skip to content

Commit 850a0ae

Browse files
valorkinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): fixes deprecation warning for MainTemplate.hooks.assetPath in webpack 5
1 parent c5ddca9 commit 850a0ae

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,26 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
141141
extraPlugins.push({
142142
apply(compiler) {
143143
compiler.hooks.compilation.tap('build-angular', compilation => {
144-
// Webpack typings do not contain MainTemplate assetPath hook
145-
// The webpack.Compilation assetPath hook is a noop in 4.x so the template must be used
146-
// tslint:disable-next-line: no-any
147-
(compilation.mainTemplate.hooks as any).assetPath.tap(
148-
'build-angular',
149-
(
150-
filename: string | ((data: { chunk: compilation.Chunk }) => string),
151-
data: { chunk: compilation.Chunk },
152-
) => {
153-
const assetName = typeof filename === 'function' ? filename(data) : filename;
154-
const isMap = assetName && assetName.endsWith('.map');
155-
156-
return data.chunk && data.chunk.name === 'polyfills-es5'
157-
? `polyfills-es5${hashFormat.chunk}.js${isMap ? '.map' : ''}`
158-
: assetName;
159-
},
160-
);
144+
const assetPath = (
145+
filename: string | ((data: { chunk: compilation.Chunk }) => string),
146+
data: { chunk: compilation.Chunk },
147+
) => {
148+
const assetName = typeof filename === 'function' ? filename(data) : filename;
149+
const isMap = assetName?.endsWith('.map');
150+
151+
return data.chunk?.name === 'polyfills-es5'
152+
? `polyfills-es5${hashFormat.chunk}.js${isMap ? '.map' : ''}`
153+
: assetName;
154+
};
155+
156+
if (isWebpackFiveOrHigher()) {
157+
compilation.hooks.assetPath.tap('remove-hash-plugin', assetPath);
158+
} else {
159+
const mainTemplate = compilation.mainTemplate as typeof compilation.mainTemplate & {
160+
hooks: typeof compilation['hooks'];
161+
};
162+
mainTemplate.hooks.assetPath.tap('build-angular', assetPath);
163+
}
161164
});
162165
},
163166
});

packages/angular_devkit/build_angular/src/webpack/plugins/remove-hash-plugin.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { Compiler } from 'webpack';
9+
import { isWebpackFiveOrHigher } from '../../utils/webpack-version';
910
import { HashFormat } from '../utils/helpers';
1011

1112

@@ -20,25 +21,28 @@ export class RemoveHashPlugin {
2021

2122
apply(compiler: Compiler): void {
2223
compiler.hooks.compilation.tap('remove-hash-plugin', compilation => {
23-
const mainTemplate = compilation.mainTemplate as typeof compilation.mainTemplate & {
24-
hooks: typeof compilation['hooks'];
24+
const assetPath = (path: string, data: { chunk?: { name: string } }) => {
25+
const chunkName = data.chunk?.name;
26+
const { chunkNames, hashFormat } = this.options;
27+
28+
if (chunkName && chunkNames?.includes(chunkName)) {
29+
// Replace hash formats with empty strings.
30+
return path
31+
.replace(hashFormat.chunk, '')
32+
.replace(hashFormat.extract, '');
33+
}
34+
35+
return path;
2536
};
2637

27-
mainTemplate.hooks.assetPath.tap('remove-hash-plugin',
28-
(path: string, data: { chunk?: { name: string } }) => {
29-
const chunkName = data.chunk && data.chunk.name;
30-
const { chunkNames, hashFormat } = this.options;
31-
32-
if (chunkName && chunkNames.includes(chunkName)) {
33-
// Replace hash formats with empty strings.
34-
return path
35-
.replace(hashFormat.chunk, '')
36-
.replace(hashFormat.extract, '');
37-
}
38-
39-
return path;
40-
},
41-
);
38+
if (isWebpackFiveOrHigher()) {
39+
compilation.hooks.assetPath.tap('remove-hash-plugin', assetPath);
40+
} else {
41+
const mainTemplate = compilation.mainTemplate as typeof compilation.mainTemplate & {
42+
hooks: typeof compilation['hooks'];
43+
};
44+
mainTemplate.hooks.assetPath.tap('remove-hash-plugin', assetPath);
45+
}
4246
});
4347
}
4448
}

0 commit comments

Comments
 (0)