Skip to content

Commit 4784155

Browse files
domskedgp1130
authored andcommitted
feat(@angular-devkit/build-angular): add wildcard option for allowedCommonJsDependencies
This commit adds the functionality to that when a wildcard `*` is provided to `allowedCommonJsDependencies` CJS/AMD warnings usages is skipped. Closes #25784
1 parent 7777bc5 commit 4784155

File tree

7 files changed

+63
-5
lines changed

7 files changed

+63
-5
lines changed

packages/angular_devkit/build_angular/src/builders/application/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@
406406
"enum": ["none", "anonymous", "use-credentials"]
407407
},
408408
"allowedCommonJsDependencies": {
409-
"description": "A list of CommonJS packages that are allowed to be used without a build time warning.",
409+
"description": "A list of CommonJS or AMD packages that are allowed to be used without a build time warning. Use `'*'` to allow all.",
410410
"type": "array",
411411
"items": {
412412
"type": "string"

packages/angular_devkit/build_angular/src/builders/application/tests/options/allowed-common-js-dependencies_spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
7777
);
7878
});
7979

80+
it('should not show warning when all dependencies are allowed by wildcard', async () => {
81+
// Add a Common JS dependency
82+
await harness.appendToFile(
83+
'src/app/app.component.ts',
84+
`
85+
import 'buffer';
86+
`,
87+
);
88+
89+
harness.useTarget('build', {
90+
...BASE_OPTIONS,
91+
allowedCommonJsDependencies: ['*'],
92+
optimization: true,
93+
});
94+
95+
const { result, logs } = await harness.executeOnce();
96+
97+
expect(result?.success).toBe(true);
98+
expect(logs).not.toContain(
99+
jasmine.objectContaining<logging.LogEntry>({
100+
message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
101+
}),
102+
);
103+
});
104+
80105
it('should not show warning when depending on zone.js', async () => {
81106
// Add a Common JS dependency
82107
await harness.appendToFile(

packages/angular_devkit/build_angular/src/builders/browser-esbuild/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@
429429
"enum": ["none", "anonymous", "use-credentials"]
430430
},
431431
"allowedCommonJsDependencies": {
432-
"description": "A list of CommonJS packages that are allowed to be used without a build time warning.",
432+
"description": "A list of CommonJS or AMD packages that are allowed to be used without a build time warning. Use `'*'` to allow all.",
433433
"type": "array",
434434
"items": {
435435
"type": "string"

packages/angular_devkit/build_angular/src/builders/browser/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@
417417
"enum": ["none", "anonymous", "use-credentials"]
418418
},
419419
"allowedCommonJsDependencies": {
420-
"description": "A list of CommonJS packages that are allowed to be used without a build time warning.",
420+
"description": "A list of CommonJS or AMD packages that are allowed to be used without a build time warning. Use `'*'` to allow all.",
421421
"type": "array",
422422
"items": {
423423
"type": "string"

packages/angular_devkit/build_angular/src/builders/browser/tests/options/allowed-common-js-dependencies_spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
7171
);
7272
});
7373

74+
it('should not show warning when all dependencies are allowed by wildcard', async () => {
75+
// Add a Common JS dependency
76+
await harness.appendToFile(
77+
'src/app/app.component.ts',
78+
`
79+
import 'bootstrap';
80+
import 'zone.js';
81+
`,
82+
);
83+
84+
harness.useTarget('build', {
85+
...BASE_OPTIONS,
86+
allowedCommonJsDependencies: ['*'],
87+
});
88+
89+
const { result, logs } = await harness.executeOnce();
90+
91+
expect(result?.success).toBe(true);
92+
expect(logs).not.toContain(
93+
jasmine.objectContaining<logging.LogEntry>({
94+
message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
95+
}),
96+
);
97+
});
98+
7499
it(`should not show warning when importing non global local data '@angular/common/locale/fr'`, async () => {
75100
await harness.appendToFile(
76101
'src/app/app.component.ts',

packages/angular_devkit/build_angular/src/tools/esbuild/commonjs-checker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { Metafile, PartialMessage } from 'esbuild';
1717
*
1818
* If any allowed dependencies are provided via the `allowedCommonJsDependencies`
1919
* parameter, both the direct import and any deep imports will be ignored and no
20-
* diagnostic will be generated.
20+
* diagnostic will be generated. Use `'*'` as entry to skip the check.
2121
*
2222
* If a module has been issued a diagnostic message, then all descendant modules
2323
* will not be checked. This prevents a potential massive amount of inactionable
@@ -34,6 +34,10 @@ export function checkCommonJSModules(
3434
const messages: PartialMessage[] = [];
3535
const allowedRequests = new Set(allowedCommonJsDependencies);
3636

37+
if (allowedRequests.has('*')) {
38+
return messages;
39+
}
40+
3741
// Ignore Angular locale definitions which are currently UMD
3842
allowedRequests.add('@angular/common/locales');
3943

packages/angular_devkit/build_angular/src/tools/webpack/plugins/common-js-usage-warn-plugin.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequ
1717
const CommonJsSelfReferenceDependency = require('webpack/lib/dependencies/CommonJsSelfReferenceDependency');
1818

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

@@ -30,6 +30,10 @@ export class CommonJsUsageWarnPlugin {
3030
}
3131

3232
apply(compiler: Compiler) {
33+
if (this.allowedDependencies.has('*')) {
34+
return;
35+
}
36+
3337
compiler.hooks.compilation.tap('CommonJsUsageWarnPlugin', (compilation) => {
3438
compilation.hooks.finishModules.tap('CommonJsUsageWarnPlugin', (modules) => {
3539
const mainEntry = compilation.entries.get('main');

0 commit comments

Comments
 (0)