Skip to content

feat(@angular-devkit/build-angular): add wildcard option for allowedCommonJsDependencies #26047

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
Oct 19, 2023
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 @@ -406,7 +406,7 @@
"enum": ["none", "anonymous", "use-credentials"]
},
"allowedCommonJsDependencies": {
"description": "A list of CommonJS packages that are allowed to be used without a build time warning.",
"description": "A list of CommonJS or AMD packages that are allowed to be used without a build time warning. Use `'*'` to allow all.",
"type": "array",
"items": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
);
});

it('should not show warning when all dependencies are allowed by wildcard', async () => {
// Add a Common JS dependency
await harness.appendToFile(
'src/app/app.component.ts',
`
import 'buffer';
`,
);

harness.useTarget('build', {
...BASE_OPTIONS,
allowedCommonJsDependencies: ['*'],
optimization: true,
});

const { result, logs } = await harness.executeOnce();

expect(result?.success).toBe(true);
expect(logs).not.toContain(
jasmine.objectContaining<logging.LogEntry>({
message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
}),
);
});

it('should not show warning when depending on zone.js', async () => {
// Add a Common JS dependency
await harness.appendToFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
"enum": ["none", "anonymous", "use-credentials"]
},
"allowedCommonJsDependencies": {
"description": "A list of CommonJS packages that are allowed to be used without a build time warning.",
"description": "A list of CommonJS or AMD packages that are allowed to be used without a build time warning. Use `'*'` to allow all.",
"type": "array",
"items": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@
"enum": ["none", "anonymous", "use-credentials"]
},
"allowedCommonJsDependencies": {
"description": "A list of CommonJS packages that are allowed to be used without a build time warning.",
"description": "A list of CommonJS or AMD packages that are allowed to be used without a build time warning. Use `'*'` to allow all.",
"type": "array",
"items": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
);
});

it('should not show warning when all dependencies are allowed by wildcard', async () => {
// Add a Common JS dependency
await harness.appendToFile(
'src/app/app.component.ts',
`
import 'bootstrap';
import 'zone.js';
`,
);

harness.useTarget('build', {
...BASE_OPTIONS,
allowedCommonJsDependencies: ['*'],
});

const { result, logs } = await harness.executeOnce();

expect(result?.success).toBe(true);
expect(logs).not.toContain(
jasmine.objectContaining<logging.LogEntry>({
message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
}),
);
});

it(`should not show warning when importing non global local data '@angular/common/locale/fr'`, async () => {
await harness.appendToFile(
'src/app/app.component.ts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { Metafile, PartialMessage } from 'esbuild';
*
* If any allowed dependencies are provided via the `allowedCommonJsDependencies`
* parameter, both the direct import and any deep imports will be ignored and no
* diagnostic will be generated.
* diagnostic will be generated. Use `'*'` as entry to skip the check.
*
* If a module has been issued a diagnostic message, then all descendant modules
* will not be checked. This prevents a potential massive amount of inactionable
Expand All @@ -34,6 +34,10 @@ export function checkCommonJSModules(
const messages: PartialMessage[] = [];
const allowedRequests = new Set(allowedCommonJsDependencies);

if (allowedRequests.has('*')) {
return messages;
}

// Ignore Angular locale definitions which are currently UMD
allowedRequests.add('@angular/common/locales');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequ
const CommonJsSelfReferenceDependency = require('webpack/lib/dependencies/CommonJsSelfReferenceDependency');

export interface CommonJsUsageWarnPluginOptions {
/** A list of CommonJS packages that are allowed to be used without a warning. */
/** A list of CommonJS or AMD packages that are allowed to be used without a warning. Use `'*'` to allow all. */
allowedDependencies?: string[];
}

Expand All @@ -30,6 +30,10 @@ export class CommonJsUsageWarnPlugin {
}

apply(compiler: Compiler) {
if (this.allowedDependencies.has('*')) {
return;
}

compiler.hooks.compilation.tap('CommonJsUsageWarnPlugin', (compilation) => {
compilation.hooks.finishModules.tap('CommonJsUsageWarnPlugin', (modules) => {
const mainEntry = compilation.entries.get('main');
Expand Down