Skip to content

Commit fcfcd68

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): suppress warning for CommonJS templateUrl and styleUrl
Currently, when users use either absolute paths, or path mappings in JIT mode, we issue a warning for templateUrl and styleUrl. With this change we suppress those warning. Closes: #18057 (cherry picked from commit 2aedad9)
1 parent ea907d7 commit fcfcd68

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { Compiler, compilation } from 'webpack';
1414
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency');
1515
const AMDDefineDependency = require('webpack/lib/dependencies/AMDDefineDependency');
1616

17+
const STYLES_TEMPLATE_URL_REGEXP = /\.(html|svg|css|sass|less|styl|scss)$/;
18+
1719
// The below is extended because there are not in the typings
1820
interface WebpackModule extends compilation.Module {
1921
name?: string;
@@ -23,6 +25,10 @@ interface WebpackModule extends compilation.Module {
2325
userRequest?: string;
2426
}
2527

28+
interface CommonJsRequireDependencyType {
29+
request: string;
30+
}
31+
2632
export interface CommonJsUsageWarnPluginOptions {
2733
/** A list of CommonJS packages that are allowed to be used without a warning. */
2834
allowedDepedencies?: string[];
@@ -61,7 +67,7 @@ export class CommonJsUsageWarnPlugin {
6167
continue;
6268
}
6369

64-
if (this.hasCommonJsDependencies(dependencies)) {
70+
if (this.hasCommonJsDependencies(dependencies, true)) {
6571
// Dependency is CommonsJS or AMD.
6672

6773
// Check if it's parent issuer is also a CommonJS dependency.
@@ -97,8 +103,23 @@ export class CommonJsUsageWarnPlugin {
97103
});
98104
}
99105

100-
private hasCommonJsDependencies(dependencies: unknown[]): boolean {
101-
return dependencies.some(d => d instanceof CommonJsRequireDependency || d instanceof AMDDefineDependency);
106+
private hasCommonJsDependencies(dependencies: unknown[], checkForStylesAndTemplatesCJS = false): boolean {
107+
for (const dep of dependencies) {
108+
if (dep instanceof CommonJsRequireDependency) {
109+
if (checkForStylesAndTemplatesCJS && STYLES_TEMPLATE_URL_REGEXP.test((dep as CommonJsRequireDependencyType).request)) {
110+
// Skip in case it's a template or stylesheet
111+
continue;
112+
}
113+
114+
return true;
115+
}
116+
117+
if (dep instanceof AMDDefineDependency) {
118+
return true;
119+
}
120+
}
121+
122+
return false;
102123
}
103124

104125
private rawRequestToPackageName(rawRequest: string): string {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,24 @@ describe('Browser Builder commonjs warning', () => {
7777
expect(logs.join()).not.toContain('WARNING');
7878
await run.stop();
7979
});
80+
81+
it('should not show warning in JIT for templateUrl and styleUrl when using paths', async () => {
82+
host.replaceInFile('tsconfig.base.json', /"baseUrl": ".\/",/, `
83+
"baseUrl": "./",
84+
"paths": {
85+
"@app/*": [
86+
"src/app/*"
87+
]
88+
},
89+
`);
90+
91+
host.replaceInFile('src/app/app.module.ts', './app.component', '@app/app.component');
92+
93+
const run = await architect.scheduleTarget(targetSpec, { aot: false }, { logger });
94+
const output = await run.result;
95+
expect(output.success).toBe(true);
96+
97+
expect(logs.join()).not.toContain('WARNING');
98+
await run.stop();
99+
});
80100
});

0 commit comments

Comments
 (0)