Skip to content

Commit 909b10a

Browse files
committed
fix(@angular-devkit/build-angular): don't warn on transitive CommonJS dependencies in AOT mode
At the moment in AOT mode if a CommonJS dependency has transitive CommonJS dependency we are issue warning for both. With this change we align the behaviour with JIT mode, where we issue warnings only for direct CommonJS dependencies or ES dependencies which have CommonJS dependencies. Closes #18526 (cherry picked from commit bbe83ae)
1 parent f481ba4 commit 909b10a

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ const STYLES_TEMPLATE_URL_REGEXP = /\.(html|svg|css|sass|less|styl|scss)$/;
2020
interface WebpackModule extends compilation.Module {
2121
name?: string;
2222
rawRequest?: string;
23-
dependencies: unknown[];
23+
dependencies: WebpackModule[];
2424
issuer: WebpackModule | null;
25+
module: WebpackModule | null;
2526
userRequest?: string;
26-
}
27-
28-
interface CommonJsRequireDependencyType {
2927
request: string;
3028
}
3129

@@ -58,22 +56,22 @@ export class CommonJsUsageWarnPlugin {
5856
this.allowedDepedencies.has(this.rawRequestToPackageName(rawRequest)) ||
5957
rawRequest.startsWith('@angular/common/locales/')
6058
) {
61-
/**
62-
* Skip when:
63-
* - module is absolute or relative.
64-
* - module is allowed even if it's a CommonJS.
65-
* - module is a locale imported from '@angular/common'.
66-
*/
59+
/**
60+
* Skip when:
61+
* - module is absolute or relative.
62+
* - module is allowed even if it's a CommonJS.
63+
* - module is a locale imported from '@angular/common'.
64+
*/
6765
continue;
6866
}
6967

70-
if (this.hasCommonJsDependencies(dependencies, true)) {
68+
if (this.hasCommonJsDependencies(dependencies)) {
7169
// Dependency is CommonsJS or AMD.
7270

7371
// Check if it's parent issuer is also a CommonJS dependency.
7472
// In case it is skip as an warning will be show for the parent CommonJS dependency.
7573
const parentDependencies = issuer?.issuer?.dependencies;
76-
if (parentDependencies && this.hasCommonJsDependencies(parentDependencies)) {
74+
if (parentDependencies && this.hasCommonJsDependencies(parentDependencies, true)) {
7775
continue;
7876
}
7977

@@ -103,10 +101,10 @@ export class CommonJsUsageWarnPlugin {
103101
});
104102
}
105103

106-
private hasCommonJsDependencies(dependencies: unknown[], checkForStylesAndTemplatesCJS = false): boolean {
104+
private hasCommonJsDependencies(dependencies: WebpackModule[], checkParentModules = false): boolean {
107105
for (const dep of dependencies) {
108106
if (dep instanceof CommonJsRequireDependency) {
109-
if (checkForStylesAndTemplatesCJS && STYLES_TEMPLATE_URL_REGEXP.test((dep as CommonJsRequireDependencyType).request)) {
107+
if (STYLES_TEMPLATE_URL_REGEXP.test(dep.request)) {
110108
// Skip in case it's a template or stylesheet
111109
continue;
112110
}
@@ -117,6 +115,10 @@ export class CommonJsUsageWarnPlugin {
117115
if (dep instanceof AMDDefineDependency) {
118116
return true;
119117
}
118+
119+
if (checkParentModules && dep.module && this.hasCommonJsDependencies(dep.module.dependencies)) {
120+
return true;
121+
}
120122
}
121123

122124
return false;

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

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,43 @@ describe('Browser Builder commonjs warning', () => {
2828

2929
afterEach(async () => host.restore().toPromise());
3030

31-
it('should show warning when depending on a Common JS bundle', async () => {
32-
// Add a Common JS dependency
33-
host.appendToFile('src/app/app.component.ts', `
34-
import 'bootstrap';
35-
`);
36-
37-
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
38-
const output = await run.result;
39-
expect(output.success).toBe(true);
40-
const logMsg = logs.join();
41-
expect(logMsg).toMatch(/WARNING in.+app\.component\.ts depends on 'bootstrap'\. CommonJS or AMD dependencies/);
42-
expect(logMsg).not.toContain('jquery', 'Should not warn on transitive CommonJS packages which parent is also CommonJS.');
43-
await run.stop();
44-
});
31+
for (const aot of [true, false]) {
32+
it(`should not show warning for styles import in ${aot ? 'AOT' : 'JIT'} Mode`, async () => {
33+
// Add a Common JS dependency
34+
host.appendToFile('src/app/app.component.ts', `
35+
import '../../test.css';
36+
`);
37+
38+
host.writeMultipleFiles({
39+
'./test.css': `
40+
body {
41+
color: red;
42+
};
43+
`,
44+
});
45+
46+
const run = await architect.scheduleTarget(targetSpec, { aot }, { logger });
47+
const output = await run.result;
48+
expect(output.success).toBe(true);
49+
expect(logs.join()).not.toContain('WARNING');
50+
await run.stop();
51+
});
52+
53+
it(`should show warning when depending on a Common JS bundle in ${aot ? 'AOT' : 'JIT'} Mode`, async () => {
54+
// Add a Common JS dependency
55+
host.appendToFile('src/app/app.component.ts', `
56+
import 'bootstrap';
57+
`);
58+
59+
const run = await architect.scheduleTarget(targetSpec, { aot }, { logger });
60+
const output = await run.result;
61+
expect(output.success).toBe(true);
62+
const logMsg = logs.join();
63+
expect(logMsg).toMatch(/WARNING in.+app\.component\.ts depends on 'bootstrap'\. CommonJS or AMD dependencies/);
64+
expect(logMsg).not.toContain('jquery', 'Should not warn on transitive CommonJS packages which parent is also CommonJS.');
65+
await run.stop();
66+
});
67+
}
4568

4669
it('should not show warning when depending on a Common JS bundle which is allowed', async () => {
4770
// Add a Common JS dependency

0 commit comments

Comments
 (0)