Skip to content

Commit 5804fec

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/build-angular): remove Webpack 4 specific type casting
Webpack 5 contains improved types and exports that reduce the need to perform additional type casting throughout the internal Webpack plugins.
1 parent e0cb822 commit 5804fec

File tree

4 files changed

+46
-109
lines changed

4 files changed

+46
-109
lines changed

packages/angular_devkit/build_angular/src/webpack/plugins/analytics.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { analytics } from '@angular-devkit/core';
9-
import {
10-
Compilation,
11-
Compiler,
12-
Module,
13-
Stats,
14-
sources,
15-
} from 'webpack';
16-
17-
const NormalModule = require('webpack/lib/NormalModule');
18-
19-
interface NormalModule extends Module {
20-
_source?: sources.OriginalSource | null;
21-
resource?: string;
22-
}
9+
import { Compilation, Compiler, Module, NormalModule, Stats } from 'webpack';
2310

2411
const webpackAllErrorMessageRe = /^([^(]+)\(\d+,\d\): (.*)$/gm;
2512
const webpackTsErrorMessageRe = /^[^(]+\(\d+,\d\): error (TS\d+):/;
@@ -148,21 +135,26 @@ export class NgBuildAnalyticsPlugin {
148135
}
149136

150137
protected _checkTsNormalModule(module: NormalModule) {
151-
if (module._source) {
152-
// PLEASE REMEMBER:
153-
// We're dealing with ES5 _or_ ES2015 JavaScript at this point (we don't know for sure).
154-
155-
// Just count the ngOnInit occurences. Comments/Strings/calls occurences should be sparse
156-
// so we just consider them within the margin of error. We do break on word break though.
157-
this._stats.numberOfNgOnInit += countOccurrences(module._source.source().toString(), 'ngOnInit', true);
158-
159-
// Count the number of `Component({` strings (case sensitive), which happens in __decorate().
160-
this._stats.numberOfComponents += countOccurrences(module._source.source().toString(), 'Component({');
161-
// For Ivy we just count ɵcmp.
162-
this._stats.numberOfComponents += countOccurrences(module._source.source().toString(), '.ɵcmp', true);
163-
// for ascii_only true
164-
this._stats.numberOfComponents += countOccurrences(module._source.source().toString(), '.\u0275cmp', true);
138+
const originalSource = module.originalSource();
139+
if (!originalSource) {
140+
return;
165141
}
142+
143+
const originalContent = originalSource.source().toString();
144+
145+
// PLEASE REMEMBER:
146+
// We're dealing with ES5 _or_ ES2015 JavaScript at this point (we don't know for sure).
147+
148+
// Just count the ngOnInit occurences. Comments/Strings/calls occurences should be sparse
149+
// so we just consider them within the margin of error. We do break on word break though.
150+
this._stats.numberOfNgOnInit += countOccurrences(originalContent, 'ngOnInit', true);
151+
152+
// Count the number of `Component({` strings (case sensitive), which happens in __decorate().
153+
this._stats.numberOfComponents += countOccurrences(originalContent, 'Component({');
154+
// For Ivy we just count ɵcmp.
155+
this._stats.numberOfComponents += countOccurrences(originalContent, '.ɵcmp', true);
156+
// for ascii_only true
157+
this._stats.numberOfComponents += countOccurrences(originalContent, '.\u0275cmp', true);
166158
}
167159

168160
protected _collectErrors(stats: Stats) {
@@ -231,12 +223,11 @@ export class NgBuildAnalyticsPlugin {
231223
* Reports a succeed module.
232224
* @private
233225
*/
234-
protected _succeedModule(mod: Module) {
226+
protected _succeedModule(module: Module) {
235227
// Only report NormalModule instances.
236-
if (mod.constructor !== NormalModule) {
228+
if (!(module instanceof NormalModule)) {
237229
return;
238230
}
239-
const module = mod as {} as NormalModule;
240231

241232
// Only reports modules that are part of the user's project. We also don't do node_modules.
242233
// There is a chance that someone name a file path `hello_node_modules` or something and we

packages/angular_devkit/build_angular/src/webpack/plugins/any-component-style-budget-checker.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ export class AnyComponentStyleBudgetChecker {
2929
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
3030
const afterOptimizeChunkAssets = () => {
3131
// In AOT compilations component styles get processed in child compilations.
32-
// tslint:disable-next-line: no-any
33-
const parentCompilation = (compilation.compiler as any).parentCompilation;
34-
if (!parentCompilation) {
32+
if (!compilation.compiler.parentCompilation) {
3533
return;
3634
}
3735

packages/angular_devkit/build_angular/src/webpack/plugins/dedupe-module-resolve-plugin.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,17 @@ export interface DedupeModuleResolvePluginOptions {
2121

2222
// tslint:disable-next-line: no-any
2323
function getResourceData(resolveData: any): ResourceData {
24-
if (resolveData.createData) {
25-
// Webpack 5+
26-
const {
27-
descriptionFileData,
28-
relativePath,
29-
} = resolveData.createData.resourceResolveData;
30-
31-
return {
32-
packageName: descriptionFileData?.name,
33-
packageVersion: descriptionFileData?.version,
34-
relativePath,
35-
resource: resolveData.createData.resource,
36-
};
37-
} else {
38-
// Webpack 4
39-
const { resource, resourceResolveData } = resolveData;
40-
41-
return {
42-
packageName: resourceResolveData.descriptionFileData?.name,
43-
packageVersion: resourceResolveData.descriptionFileData?.version,
44-
relativePath: resourceResolveData.relativePath,
45-
resource: resource,
46-
};
47-
}
24+
const {
25+
descriptionFileData,
26+
relativePath,
27+
} = resolveData.createData.resourceResolveData;
28+
29+
return {
30+
packageName: descriptionFileData?.name,
31+
packageVersion: descriptionFileData?.version,
32+
relativePath,
33+
resource: resolveData.createData.resource,
34+
};
4835
}
4936

5037
/**
@@ -100,14 +87,9 @@ export class DedupeModuleResolvePlugin {
10087
}
10188

10289
// Alter current request with previously resolved module.
103-
// tslint:disable-next-line: no-any
104-
const createData = (result as any).createData;
105-
if (createData) {
106-
createData.resource = prevResource;
107-
createData.userRequest = prevResource;
108-
} else {
109-
result.request = prevRequest;
110-
}
90+
const createData = result.createData as { resource: string; userRequest: string };
91+
createData.resource = prevResource;
92+
createData.userRequest = prevRequest;
11193
});
11294
});
11395
}

packages/angular_devkit/build_angular/src/webpack/plugins/suppress-entry-chunks-webpack-plugin.ts

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export class SuppressExtractedTextChunksWebpackPlugin {
2121

2222
// Only chunks with a css asset should have JavaScript assets removed
2323
let hasCssFile = false;
24-
// chunk.files is an Array in Webpack 4 and a Set in Webpack 5
2524
for (const file of chunk.files) {
2625
if (file.endsWith('.css')) {
2726
hasCssFile = true;
@@ -35,53 +34,20 @@ export class SuppressExtractedTextChunksWebpackPlugin {
3534

3635
// Only chunks with all CSS entry dependencies should have JavaScript assets removed
3736
let cssOnly = false;
38-
// The any cast is used for default Webpack 4 type compatibility
39-
// tslint:disable-next-line: no-any
40-
const entryModules = (compilation as any).chunkGraph?.getChunkEntryModulesIterable(chunk);
41-
if (entryModules) {
42-
// Webpack 5
43-
for (const module of entryModules) {
44-
cssOnly = module.dependencies.every(
45-
(dependency: {}) => dependency.constructor.name === 'CssDependency',
46-
);
37+
const entryModules = compilation.chunkGraph.getChunkEntryModulesIterable(chunk);
38+
for (const module of entryModules) {
39+
cssOnly = module.dependencies.every(
40+
(dependency: {}) => dependency.constructor.name === 'CssDependency',
41+
);
4742

48-
if (!cssOnly) {
49-
break;
50-
}
51-
}
52-
} else {
53-
// Webpack 4
54-
for (const module of chunk.modulesIterable as Iterable<{ dependencies: {}[] }>) {
55-
cssOnly = module.dependencies.every((dependency) => {
56-
const name = dependency.constructor.name;
57-
58-
return (
59-
name === 'CssDependency' ||
60-
name === 'SingleEntryDependency' ||
61-
name === 'MultiEntryDependency' ||
62-
name === 'HarmonyCompatibilityDependency' ||
63-
name === 'HarmonyExportHeaderDependency' ||
64-
name === 'HarmonyInitDependency'
65-
);
66-
});
67-
68-
if (!cssOnly) {
69-
break;
70-
}
43+
if (!cssOnly) {
44+
break;
7145
}
7246
}
7347

7448
if (cssOnly) {
75-
if (Array.isArray(chunk.files)) {
76-
// Webpack 4
77-
(chunk.files as string[]) = chunk.files.filter((file) => file !== filename);
78-
delete compilation.assets[filename];
79-
} else {
80-
// Webpack 5
81-
// Casting is used for default Webpack 4 type compatibility
82-
((chunk.files as unknown) as Set<string>).delete(filename);
83-
((compilation as unknown) as { deleteAsset(file: string): void }).deleteAsset(filename);
84-
}
49+
chunk.files.delete(filename);
50+
compilation.deleteAsset(filename);
8551
}
8652
});
8753
});

0 commit comments

Comments
 (0)