Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d74b7c9

Browse files
alan-agius4filipesilva
authored andcommittedJul 1, 2020
fix(@angular-devkit/build-angular): match allowed dependencies against the package name
With this change we add the functionality to also match an allowed dependency against a package name. The package name is retrieved from the rawRequest. Previously, users needed to add the request path which in some case might be a deep import. Ex: `zone.js/dist/zone-error`. With this change adding the package name example `zone.js` will suffice. Closes: #18058
1 parent f22efa1 commit d74b7c9

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed
 

‎packages/angular_devkit/build_angular/src/angular-cli-files/plugins/common-js-usage-warn-plugin.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ export class CommonJsUsageWarnPlugin {
3434
// Allow the below depedency for HMR
3535
// tslint:disable-next-line: max-line-length
3636
// https://github.com/angular/angular-cli/blob/1e258317b1f6ec1e957ee3559cc3b28ba602f3ba/packages/angular_devkit/build_angular/src/dev-server/index.ts#L605-L638
37-
private allowedDepedencies = [
38-
'webpack/hot/dev-server',
39-
];
37+
private allowedDepedencies = new Set<string>(['webpack/hot/dev-server']);
4038

4139
constructor(private options: CommonJsUsageWarnPluginOptions = {}) {
42-
if (this.options.allowedDepedencies) {
43-
this.allowedDepedencies.push(...this.options.allowedDepedencies);
44-
}
40+
this.options.allowedDepedencies?.forEach(d => this.allowedDepedencies.add(d));
4541
}
4642

4743
apply(compiler: Compiler) {
@@ -57,7 +53,10 @@ export class CommonJsUsageWarnPlugin {
5753
continue;
5854
}
5955

60-
if (this.allowedDepedencies?.includes(rawRequest)) {
56+
if (
57+
this.allowedDepedencies.has(rawRequest) ||
58+
this.allowedDepedencies.has(this.rawRequestToPackageName(rawRequest))
59+
) {
6160
// Skip as this module is allowed even if it's a CommonJS.
6261
continue;
6362
}
@@ -67,7 +66,8 @@ export class CommonJsUsageWarnPlugin {
6766

6867
// Check if it's parent issuer is also a CommonJS dependency.
6968
// In case it is skip as an warning will be show for the parent CommonJS dependency.
70-
if (this.hasCommonJsDependencies(issuer?.issuer?.dependencies ?? [])) {
69+
const parentDependencies = issuer?.issuer?.dependencies;
70+
if (parentDependencies && this.hasCommonJsDependencies(parentDependencies)) {
7171
continue;
7272
}
7373

@@ -100,4 +100,12 @@ export class CommonJsUsageWarnPlugin {
100100
return dependencies.some(d => d instanceof CommonJsRequireDependency || d instanceof AMDDefineDependency);
101101
}
102102

103+
private rawRequestToPackageName(rawRequest: string): string {
104+
return rawRequest.startsWith('@')
105+
// Scoped request ex: @angular/common/locale/en -> @angular/common
106+
? rawRequest.split('/', 2).join('/')
107+
// Non-scoped request ex: lodash/isEmpty -> lodash
108+
: rawRequest.split('/', 1)[0];
109+
}
110+
103111
}

‎packages/angular_devkit/build_angular/src/browser/specs/common-js-warning_spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ describe('Browser Builder commonjs warning', () => {
2121
architect = (await createArchitect(host.root())).architect;
2222

2323
// Add a Common JS dependency
24-
host.appendToFile('src/app/app.component.ts', `import 'bootstrap';`);
24+
host.appendToFile('src/app/app.component.ts', `
25+
import 'bootstrap';
26+
import 'zone.js/dist/zone-error';
27+
`);
2528

2629
// Create logger
2730
logger = new logging.Logger('');
@@ -45,6 +48,7 @@ describe('Browser Builder commonjs warning', () => {
4548
const overrides = {
4649
allowedCommonJsDependencies: [
4750
'bootstrap',
51+
'zone.js',
4852
],
4953
};
5054

0 commit comments

Comments
 (0)
Please sign in to comment.