Skip to content

fix(@angular-devkit/build-angular): suppress warning for CommonJS templateUrl and styleUrl #18259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { Compiler, compilation } from 'webpack';
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency');
const AMDDefineDependency = require('webpack/lib/dependencies/AMDDefineDependency');

const STYLES_TEMPLATE_URL_REGEXP = /\.(html|svg|css|sass|less|styl|scss)$/;

// The below is extended because there are not in the typings
interface WebpackModule extends compilation.Module {
name?: string;
Expand All @@ -23,6 +25,10 @@ interface WebpackModule extends compilation.Module {
userRequest?: string;
}

interface CommonJsRequireDependencyType {
request: string;
}

export interface CommonJsUsageWarnPluginOptions {
/** A list of CommonJS packages that are allowed to be used without a warning. */
allowedDepedencies?: string[];
Expand Down Expand Up @@ -61,7 +67,7 @@ export class CommonJsUsageWarnPlugin {
continue;
}

if (this.hasCommonJsDependencies(dependencies)) {
if (this.hasCommonJsDependencies(dependencies, true)) {
// Dependency is CommonsJS or AMD.

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

private hasCommonJsDependencies(dependencies: unknown[]): boolean {
return dependencies.some(d => d instanceof CommonJsRequireDependency || d instanceof AMDDefineDependency);
private hasCommonJsDependencies(dependencies: unknown[], checkForStylesAndTemplatesCJS = false): boolean {
for (const dep of dependencies) {
if (dep instanceof CommonJsRequireDependency) {
if (checkForStylesAndTemplatesCJS && STYLES_TEMPLATE_URL_REGEXP.test((dep as CommonJsRequireDependencyType).request)) {
// Skip in case it's a template or stylesheet
continue;
}

return true;
}

if (dep instanceof AMDDefineDependency) {
return true;
}
}

return false;
}

private rawRequestToPackageName(rawRequest: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,24 @@ describe('Browser Builder commonjs warning', () => {
expect(logs.join()).not.toContain('WARNING');
await run.stop();
});

it('should not show warning in JIT for templateUrl and styleUrl when using paths', async () => {
host.replaceInFile('tsconfig.base.json', /"baseUrl": ".\/",/, `
"baseUrl": "./",
"paths": {
"@app/*": [
"src/app/*"
]
},
`);

host.replaceInFile('src/app/app.module.ts', './app.component', '@app/app.component');

const run = await architect.scheduleTarget(targetSpec, { aot: false }, { logger });
const output = await run.result;
expect(output.success).toBe(true);

expect(logs.join()).not.toContain('WARNING');
await run.stop();
});
});