Skip to content

Commit e09dc5c

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. (cherry picked from commit 0c2a862)
1 parent 7604e62 commit e09dc5c

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

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';
@@ -119,6 +112,8 @@ export class AngularWebpackPlugin {
119112
}
120113

121114
apply(compiler: Compiler): void {
115+
const { NormalModuleReplacementPlugin, util } = compiler.webpack;
116+
122117
// Setup file replacements with webpack
123118
for (const [key, value] of Object.entries(this.pluginOptions.fileReplacements)) {
124119
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 { normalizePath } from './ivy/paths';
1414

1515
interface CompilationOutput {
@@ -131,7 +131,8 @@ export class WebpackResourceLoader {
131131
},
132132
};
133133

134-
const context = this._parentCompilation.compiler.context;
134+
const { context, webpack } = this._parentCompilation.compiler;
135+
const { EntryPlugin, NormalModule, library, node, sources } = webpack;
135136
const childCompiler = this._parentCompilation.createChildCompiler(
136137
'angular-compiler:resource',
137138
outputOptions,
@@ -191,7 +192,7 @@ export class WebpackResourceLoader {
191192
let finalMap: string | undefined;
192193
childCompiler.hooks.compilation.tap('angular-compiler', (childCompilation) => {
193194
childCompilation.hooks.processAssets.tap(
194-
{ name: 'angular-compiler', stage: Compilation.PROCESS_ASSETS_STAGE_REPORT },
195+
{ name: 'angular-compiler', stage: webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT },
195196
() => {
196197
finalContent = childCompilation.assets[outputFilePath]?.source().toString();
197198
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)