Skip to content

Commit 3504c43

Browse files
clydinalan-agius4
authored andcommitted
fix(@ngtools/webpack): remove Webpack 5 deprecation warning in resource loader
This change adds support for using the Webpack `processAssets` hook to handle the resource loader child compilation's assets. This new hook is the recommended way to process assets in Webpack 5+.
1 parent d564567 commit 3504c43

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/ngtools/webpack/src/resource_loader.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as vm from 'vm';
99
import { Compiler, compilation } from 'webpack';
1010
import { RawSource } from 'webpack-sources';
1111
import { normalizePath } from './ivy/paths';
12+
import { isWebpackFiveOrHigher } from './webpack-version';
1213

1314
const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
1415
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
@@ -132,13 +133,25 @@ export class WebpackResourceLoader {
132133

133134
let finalContent: string | undefined;
134135
let finalMap: string | undefined;
135-
childCompiler.hooks.afterCompile.tap('angular-compiler', (childCompilation) => {
136-
finalContent = childCompilation.assets[filePath]?.source().toString();
137-
finalMap = childCompilation.assets[filePath + '.map']?.source().toString();
136+
if (isWebpackFiveOrHigher()) {
137+
childCompiler.hooks.compilation.tap('angular-compiler', (childCompilation) => {
138+
childCompilation.hooks.processAssets.tap('angular-compiler', () => {
139+
finalContent = childCompilation.assets[filePath]?.source().toString();
140+
finalMap = childCompilation.assets[filePath + '.map']?.source().toString();
141+
142+
delete childCompilation.assets[filePath];
143+
delete childCompilation.assets[filePath + '.map'];
144+
});
145+
});
146+
} else {
147+
childCompiler.hooks.afterCompile.tap('angular-compiler', (childCompilation) => {
148+
finalContent = childCompilation.assets[filePath]?.source().toString();
149+
finalMap = childCompilation.assets[filePath + '.map']?.source().toString();
138150

139-
delete childCompilation.assets[filePath];
140-
delete childCompilation.assets[filePath + '.map'];
141-
});
151+
delete childCompilation.assets[filePath];
152+
delete childCompilation.assets[filePath + '.map'];
153+
});
154+
}
142155

143156
return new Promise<CompilationOutput>((resolve, reject) => {
144157
childCompiler.runAsChild((error, _, childCompilation) => {

0 commit comments

Comments
 (0)