Skip to content

Commit

Permalink
fix(@ngtools/webpack): remove Webpack 5 deprecation warning in resour…
Browse files Browse the repository at this point in the history
…ce 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+.
  • Loading branch information
clydin authored and alan-agius4 committed Mar 17, 2021
1 parent d564567 commit 3504c43
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/ngtools/webpack/src/resource_loader.ts
Expand Up @@ -9,6 +9,7 @@ import * as vm from 'vm';
import { Compiler, compilation } from 'webpack';
import { RawSource } from 'webpack-sources';
import { normalizePath } from './ivy/paths';
import { isWebpackFiveOrHigher } from './webpack-version';

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

let finalContent: string | undefined;
let finalMap: string | undefined;
childCompiler.hooks.afterCompile.tap('angular-compiler', (childCompilation) => {
finalContent = childCompilation.assets[filePath]?.source().toString();
finalMap = childCompilation.assets[filePath + '.map']?.source().toString();
if (isWebpackFiveOrHigher()) {
childCompiler.hooks.compilation.tap('angular-compiler', (childCompilation) => {
childCompilation.hooks.processAssets.tap('angular-compiler', () => {
finalContent = childCompilation.assets[filePath]?.source().toString();
finalMap = childCompilation.assets[filePath + '.map']?.source().toString();

delete childCompilation.assets[filePath];
delete childCompilation.assets[filePath + '.map'];
});
});
} else {
childCompiler.hooks.afterCompile.tap('angular-compiler', (childCompilation) => {
finalContent = childCompilation.assets[filePath]?.source().toString();
finalMap = childCompilation.assets[filePath + '.map']?.source().toString();

delete childCompilation.assets[filePath];
delete childCompilation.assets[filePath + '.map'];
});
delete childCompilation.assets[filePath];
delete childCompilation.assets[filePath + '.map'];
});
}

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

0 comments on commit 3504c43

Please sign in to comment.