Skip to content

Commit a5fb971

Browse files
committed
test(@ngtools/webpack): add unit test for register_locate_data transformer
1 parent 5952889 commit a5fb971

File tree

4 files changed

+90
-41
lines changed

4 files changed

+90
-41
lines changed

packages/@ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ export class AngularCompilerPlugin implements Tapable {
236236
if (options.hasOwnProperty('i18nFormat')) {
237237
this._angularCompilerOptions.i18nInFormat = options.i18nFormat;
238238
}
239-
if (options.hasOwnProperty('locale')) {
240-
this._angularCompilerOptions.i18nInLocale = options.locale;
239+
if (options.hasOwnProperty('locale') && options.locale) {
240+
this._angularCompilerOptions.i18nInLocale = this._validateLocale(options.locale);
241241
}
242242
if (options.hasOwnProperty('missingTranslation')) {
243243
this._angularCompilerOptions.i18nInMissingTranslations =
@@ -657,7 +657,7 @@ export class AngularCompilerPlugin implements Tapable {
657657
transformOps.push(...replaceBootstrap(sf, this.entryModule));
658658
}
659659

660-
// if we have a locale, auto import the locale data file
660+
// If we have a locale, auto import the locale data file.
661661
if (this._angularCompilerOptions.i18nInLocale) {
662662
transformOps.push(...registerLocaleData(
663663
sf,
@@ -824,4 +824,41 @@ export class AngularCompilerPlugin implements Tapable {
824824
timeEnd('AngularCompilerPlugin._emit');
825825
return { program, emitResult, diagnostics: allDiagnostics };
826826
}
827+
828+
private _validateLocale(locale: string) {
829+
// Get the path of the common module.
830+
const commonPath = path.dirname(require.resolve('@angular/common/package.json'));
831+
// Check if the locale file exists
832+
if (!fs.existsSync(path.resolve(commonPath, 'locales', `${locale}.js`))) {
833+
// Check for an alternative locale (if the locale id was badly formatted).
834+
const locales = fs.readdirSync(path.resolve(commonPath, 'locales'))
835+
.filter(file => file.endsWith('.js'))
836+
.map(file => file.replace('.js', ''));
837+
838+
let newLocale;
839+
const normalizedLocale = locale.toLowerCase().replace(/_/g, '-');
840+
for (const l of locales) {
841+
if (l.toLowerCase() === normalizedLocale) {
842+
newLocale = l;
843+
break;
844+
}
845+
}
846+
847+
if (newLocale) {
848+
locale = newLocale;
849+
} else {
850+
// Check for a parent locale
851+
const parentLocale = normalizedLocale.split('-')[0];
852+
if (locales.indexOf(parentLocale) !== -1) {
853+
locale = parentLocale;
854+
} else {
855+
throw new Error(
856+
`Unable to load the locale data file "@angular/common/locales/${locale}", ` +
857+
`please check that "${locale}" is a valid locale id.`);
858+
}
859+
}
860+
}
861+
862+
return locale;
863+
}
827864
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as ts from 'typescript';
2+
import { oneLine, stripIndent } from 'common-tags';
3+
import { transformTypescript } from './ast_helpers';
4+
import { registerLocaleData } from './register_locale_data';
5+
6+
describe('@ngtools/webpack transformers', () => {
7+
describe('register_locale_data', () => {
8+
it('should add locale imports', () => {
9+
const input = stripIndent`
10+
import { enableProdMode } from '@angular/core';
11+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
12+
13+
import { AppModule } from './app/app.module';
14+
import { environment } from './environments/environment';
15+
16+
if (environment.production) {
17+
enableProdMode();
18+
}
19+
20+
platformBrowserDynamic().bootstrapModule(AppModule);
21+
`;
22+
const output = stripIndent`
23+
import __locale_fr__ from "@angular/common/locales/fr";
24+
import { registerLocaleData } from "@angular/common";
25+
registerLocaleData(__locale_fr__);
26+
27+
import { enableProdMode } from '@angular/core';
28+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
29+
30+
import { AppModule } from './app/app.module';
31+
import { environment } from './environments/environment';
32+
33+
if (environment.production) {
34+
enableProdMode();
35+
}
36+
37+
platformBrowserDynamic().bootstrapModule(AppModule);
38+
`;
39+
40+
const transformOpsCb = (sourceFile: ts.SourceFile) =>
41+
registerLocaleData(sourceFile, { path: '/app.module', className: 'AppModule' }, 'fr');
42+
const result = transformTypescript(input, transformOpsCb);
43+
44+
expect(oneLine`${result}`).toEqual(oneLine`${output}`);
45+
});
46+
});
47+
});

packages/@ngtools/webpack/src/transformers/register_locale_data.ts

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as path from 'path';
2-
import * as fs from 'fs';
31
import * as ts from 'typescript';
42

53
import { findAstNodes, getFirstNode } from './ast_helpers';
@@ -45,39 +43,6 @@ export function registerLocaleData(
4543
return;
4644
}
4745

48-
// get the path of the common module
49-
const commonPath = path.dirname(require.resolve('@angular/common/package.json'));
50-
// check if the locale file exists
51-
if (!fs.existsSync(path.resolve(commonPath, 'locales', `${locale}.js`))) {
52-
// check for an alternative locale (if the locale id was badly formatted)
53-
const locales = fs.readdirSync(path.resolve(commonPath, 'locales'))
54-
.filter(file => file.endsWith('.js'))
55-
.map(file => file.replace('.js', ''));
56-
57-
let newLocale;
58-
const normalizedLocale = locale.toLowerCase().replace(/_/g, '-');
59-
for (const l of locales) {
60-
if (l.toLowerCase() === normalizedLocale) {
61-
newLocale = l;
62-
break;
63-
}
64-
}
65-
66-
if (newLocale) {
67-
locale = newLocale;
68-
} else {
69-
// check for a parent locale
70-
const parentLocale = normalizedLocale.split('-')[0];
71-
if (locales.indexOf(parentLocale) !== -1) {
72-
locale = parentLocale;
73-
} else {
74-
throw new Error(
75-
`Unable to load the locale data file "@angular/common/locales/${locale}", ` +
76-
`please check that "${locale}" is a valid locale id.`);
77-
}
78-
}
79-
}
80-
8146
// Create the import node for the locale.
8247
const localeIdentifier = ts.createIdentifier(`__locale_${locale.replace(/-/g, '')}__`);
8348
const localeImportClause = ts.createImportClause(localeIdentifier, undefined);
@@ -92,7 +57,7 @@ export function registerLocaleData(
9257

9358
// Create the import node for the registerLocaleData function.
9459
const regIdentifier = ts.createIdentifier(`registerLocaleData`);
95-
const regImportSpecifier = ts.createImportSpecifier(regIdentifier, regIdentifier);
60+
const regImportSpecifier = ts.createImportSpecifier(undefined, regIdentifier);
9661
const regNamedImport = ts.createNamedImports([regImportSpecifier]);
9762
const regImportClause = ts.createImportClause(undefined, regNamedImport);
9863
const regNewImport = ts.createImportDeclaration(undefined, undefined, regImportClause,

tests/e2e/tests/build/aot/angular-compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ export default function () {
4242

4343
// tests for register_locale_data transformer
4444
.then(() => rimraf('dist'))
45-
.then(() => ng('build', '--aot', '--locale=fr'))
45+
.then(() => ng('build', '--locale=fr'))
4646
.then(() => expectFileToMatch('dist/main.bundle.js',
4747
/registerLocaleData/))
4848
.then(() => expectFileToMatch('dist/main.bundle.js',
4949
/angular_common_locales_fr/))
5050
.then(() => rimraf('dist'))
5151
.then(() => expectToFail(() =>
52-
ng('build', '--aot', '--locale=no-locale')))
52+
ng('build', '--locale=no-locale')))
5353

5454
// Cleanup
5555
.then(() => {

0 commit comments

Comments
 (0)