Skip to content

Commit 0c2a862

Browse files
clydinalan-agius4
authored andcommitted
fix(@ngtools/webpack): ensure plugin provided Webpack instance is used
Webpack 5 provides the Webpack instance as a property on the Webpack compiler which allows Webpack plugins to leverage the same Webpack instance that was used to initiate the build. The `AngularWebpackPlugin` now will only use the provided instance to ensure that differing Webpack instances and/or versions are not used.
1 parent bf11f2b commit 0c2a862

File tree

5 files changed

+11
-16
lines changed

5 files changed

+11
-16
lines changed

packages/ngtools/webpack/src/inline-data-loader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { Compilation, LoaderContext } from 'webpack';
9+
import type { Compilation, LoaderContext } from 'webpack';
1010

1111
export const InlineAngularResourceSymbol = Symbol();
1212

packages/ngtools/webpack/src/ivy/cache.ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import * as ts from 'typescript';
10-
import { normalizePath } from './paths';
1110

1211
export class SourceFileCache extends Map<string, ts.SourceFile> {
1312
private readonly angularDiagnostics = new Map<ts.SourceFile, ts.Diagnostic[]>();

packages/ngtools/webpack/src/ivy/plugin.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@ import { CompilerHost, CompilerOptions, readConfiguration } from '@angular/compi
1010
import { NgtscProgram } from '@angular/compiler-cli/src/ngtsc/program';
1111
import { createHash } from 'crypto';
1212
import * as ts from 'typescript';
13-
import {
14-
Compilation,
15-
Compiler,
16-
Module,
17-
NormalModule,
18-
NormalModuleReplacementPlugin,
19-
util,
20-
} from 'webpack';
13+
import type { Compilation, Compiler, Module, NormalModule } from 'webpack';
2114
import { NgccProcessor } from '../ngcc_processor';
2215
import { TypeScriptPathsPlugin } from '../paths-plugin';
2316
import { WebpackResourceLoader } from '../resource_loader';
@@ -124,6 +117,8 @@ export class AngularWebpackPlugin {
124117
}
125118

126119
apply(compiler: Compiler): void {
120+
const { NormalModuleReplacementPlugin, util } = compiler.webpack;
121+
127122
// Setup file replacements with webpack
128123
for (const [key, value] of Object.entries(this.pluginOptions.fileReplacements)) {
129124
new NormalModuleReplacementPlugin(

packages/ngtools/webpack/src/resource_loader.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { createHash } from 'crypto';
1010
import * as path from 'path';
1111
import * as vm from 'vm';
12-
import { Asset, Compilation, EntryPlugin, NormalModule, library, node, sources } from 'webpack';
12+
import type { Asset, Compilation } from 'webpack';
1313
import {
1414
CompilationWithInlineAngularResource,
1515
InlineAngularResourceSymbol,
@@ -142,7 +142,8 @@ export class WebpackResourceLoader {
142142
},
143143
};
144144

145-
const context = this._parentCompilation.compiler.context;
145+
const { context, webpack } = this._parentCompilation.compiler;
146+
const { EntryPlugin, NormalModule, library, node, sources } = webpack;
146147
const childCompiler = this._parentCompilation.createChildCompiler(
147148
'angular-compiler:resource',
148149
outputOptions,
@@ -204,7 +205,7 @@ export class WebpackResourceLoader {
204205
let finalMap: string | undefined;
205206
childCompiler.hooks.compilation.tap('angular-compiler', (childCompilation) => {
206207
childCompilation.hooks.processAssets.tap(
207-
{ name: 'angular-compiler', stage: Compilation.PROCESS_ASSETS_STAGE_REPORT },
208+
{ name: 'angular-compiler', stage: webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT },
208209
() => {
209210
finalContent = childCompilation.assets[outputFilePath]?.source().toString();
210211
finalMap = childCompilation.assets[outputFilePath + '.map']?.source().toString();

packages/ngtools/webpack/src/webpack-diagnostics.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { Compilation, WebpackError } from 'webpack';
9+
import type { Compilation } from 'webpack';
1010

1111
export function addWarning(compilation: Compilation, message: string): void {
12-
compilation.warnings.push(new WebpackError(message));
12+
compilation.warnings.push(new compilation.compiler.webpack.WebpackError(message));
1313
}
1414

1515
export function addError(compilation: Compilation, message: string): void {
16-
compilation.errors.push(new WebpackError(message));
16+
compilation.errors.push(new compilation.compiler.webpack.WebpackError(message));
1717
}

0 commit comments

Comments
 (0)