Skip to content

Commit

Permalink
feat(@angular-devkit/build-angular): add wildcard option for `allowed…
Browse files Browse the repository at this point in the history
…CommonJsDependencies`

This commit adds the functionality to that when a wildcard `*` is provided to `allowedCommonJsDependencies` CJS/AMD warnings usages is skipped.

Closes #25784
  • Loading branch information
domske authored and dgp1130 committed Oct 19, 2023
1 parent 7777bc5 commit 4784155
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
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

0 comments on commit 4784155

Please sign in to comment.