Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 68ed9ab

Browse files
alan-agius4filipesilva
authored andcommittedJul 1, 2020
fix(@angular-devkit/build-angular): show warning when using non global locale data
When using the `localize` option directly importing locale data from `@angular/common` is not needed because the Angular CLI will automatically include locale data. When not using the `localize` option, most likely users meant to import the global variant of the local data. See: https://angular.io/guide/i18n#import-global-variants-of-the-locale-data
1 parent d74b7c9 commit 68ed9ab

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed
 

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,16 @@ export class CommonJsUsageWarnPlugin {
8181
// And if the issuer request is not from 'webpack-dev-server', as 'webpack-dev-server'
8282
// will require CommonJS libraries for live reloading such as 'sockjs-node'.
8383
if (mainIssuer?.name === 'main' && !issuer?.userRequest?.includes('webpack-dev-server')) {
84-
const warning = `${issuer?.userRequest} depends on ${rawRequest}. CommonJS or AMD dependencies can cause optimization bailouts.\n` +
85-
'For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies';
84+
let warning = `${issuer?.userRequest} depends on '${rawRequest}'.`;
85+
86+
if (rawRequest.startsWith('@angular/common/locales')) {
87+
warning += `\nWhen using the 'localize' option this import is not needed. ` +
88+
`Did you mean to import '${rawRequest.replace(/locales(\/extra)?\//, 'locales/global/')}'?\n` +
89+
'For more info see: https://angular.io/guide/i18n#import-global-variants-of-the-locale-data';
90+
} else {
91+
warning += ' CommonJS or AMD dependencies can cause optimization bailouts.\n' +
92+
'For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies';
93+
}
8694

8795
// Avoid showing the same warning multiple times when in 'watch' mode.
8896
if (!this.shownWarnings.has(warning)) {

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

+28-7
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ describe('Browser Builder commonjs warning', () => {
2020
await host.initialize().toPromise();
2121
architect = (await createArchitect(host.root())).architect;
2222

23-
// Add a Common JS dependency
24-
host.appendToFile('src/app/app.component.ts', `
25-
import 'bootstrap';
26-
import 'zone.js/dist/zone-error';
27-
`);
28-
2923
// Create logger
3024
logger = new logging.Logger('');
3125
logs = [];
@@ -35,16 +29,27 @@ describe('Browser Builder commonjs warning', () => {
3529
afterEach(async () => host.restore().toPromise());
3630

3731
it('should show warning when depending on a Common JS bundle', async () => {
32+
// Add a Common JS dependency
33+
host.appendToFile('src/app/app.component.ts', `
34+
import 'bootstrap';
35+
`);
36+
3837
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
3938
const output = await run.result;
4039
expect(output.success).toBe(true);
4140
const logMsg = logs.join();
42-
expect(logMsg).toMatch(/WARNING in.+app\.component\.ts depends on bootstrap\. CommonJS or AMD dependencies/);
41+
expect(logMsg).toMatch(/WARNING in.+app\.component\.ts depends on 'bootstrap'\. CommonJS or AMD dependencies/);
4342
expect(logMsg).not.toContain('jquery', 'Should not warn on transitive CommonJS packages which parent is also CommonJS.');
4443
await run.stop();
4544
});
4645

4746
it('should not show warning when depending on a Common JS bundle which is allowed', async () => {
47+
// Add a Common JS dependency
48+
host.appendToFile('src/app/app.component.ts', `
49+
import 'bootstrap';
50+
import 'zone.js/dist/zone-error';
51+
`);
52+
4853
const overrides = {
4954
allowedCommonJsDependencies: [
5055
'bootstrap',
@@ -58,4 +63,20 @@ describe('Browser Builder commonjs warning', () => {
5863
expect(logs.join()).not.toContain('WARNING');
5964
await run.stop();
6065
});
66+
67+
it(`should show warning when importing non global '@angular/common/locale' data`, async () => {
68+
// Add a Common JS dependency
69+
host.appendToFile('src/app/app.component.ts', `
70+
import '@angular/common/locales/fr';
71+
`);
72+
73+
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
74+
const output = await run.result;
75+
expect(output.success).toBe(true);
76+
77+
const logMsg = logs.join();
78+
expect(logMsg).toMatch(/WARNING in.+app\.component\.ts depends on '@angular\/common\/locales\/fr'/);
79+
expect(logMsg).toContain(`Did you mean to import '@angular/common/locales/global/fr'`);
80+
await run.stop();
81+
});
6182
});

0 commit comments

Comments
 (0)
Please sign in to comment.