Skip to content

Commit b626308

Browse files
clydinfilipesilva
authored andcommittedMay 18, 2020
fix(@angular-devkit/build-angular): downlevel and optimize locale data
Locale data is now transformed to be compatible with the ECMAScript level of the application bundles. The locale data is also optimized to remove comments and unnecessary whitespace. Fixes: #17497 and Fixes: #17735
1 parent 595d7c9 commit b626308

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed
 

‎packages/angular_devkit/build_angular/src/utils/process-bundle.ts

+28-11
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ export async function inlineLocales(options: InlineOptions) {
634634
// If locale data is provided, load it and prepend to file
635635
const localeDataPath = i18n.locales[locale]?.dataPath;
636636
if (localeDataPath) {
637-
localeDataContent = await loadLocaleData(localeDataPath, true);
637+
localeDataContent = await loadLocaleData(localeDataPath, true, options.es5);
638638
}
639639
}
640640

@@ -748,7 +748,7 @@ async function inlineLocalesDirect(ast: ParseResult, options: InlineOptions) {
748748
let localeDataSource: Source | null = null;
749749
const localeDataPath = i18n.locales[locale] && i18n.locales[locale].dataPath;
750750
if (localeDataPath) {
751-
const localeDataContent = await loadLocaleData(localeDataPath, true);
751+
const localeDataContent = await loadLocaleData(localeDataPath, true, options.es5);
752752
localeDataSource = new OriginalSource(localeDataContent, path.basename(localeDataPath));
753753
}
754754

@@ -854,19 +854,36 @@ function findLocalizePositions(
854854
return positions;
855855
}
856856

857-
async function loadLocaleData(path: string, optimize: boolean): Promise<string> {
857+
async function loadLocaleData(path: string, optimize: boolean, es5: boolean): Promise<string> {
858858
// The path is validated during option processing before the build starts
859859
const content = fs.readFileSync(path, 'utf8');
860860

861-
// NOTE: This can be removed once the locale data files are preprocessed in the framework
862-
if (optimize) {
863-
const result = await terserMangle(content, {
864-
compress: true,
865-
ecma: 5,
866-
});
861+
// Downlevel and optimize the data
862+
const transformResult = await transformAsync(content, {
863+
filename: path,
864+
// The types do not include the false option even though it is valid
865+
// tslint:disable-next-line: no-any
866+
inputSourceMap: false as any,
867+
babelrc: false,
868+
configFile: false,
869+
presets: [
870+
[
871+
require.resolve('@babel/preset-env'),
872+
{
873+
bugfixes: true,
874+
// IE 9 is the oldest supported browser
875+
targets: es5 ? { ie: '9' } : { esmodules: true },
876+
},
877+
],
878+
],
879+
minified: allowMinify && optimize,
880+
compact: !shouldBeautify && optimize,
881+
comments: !optimize,
882+
});
867883

868-
return result.code;
884+
if (!transformResult || !transformResult.code) {
885+
throw new Error(`Unknown error occurred processing bundle for "${path}".`);
869886
}
870887

871-
return content;
888+
return transformResult.code;
872889
}

‎tests/legacy-cli/e2e/tests/i18n/ivy-localize-dl-xliff2.ts

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ export async function executeTest() {
4141
await expectFileToMatch(`${outputPath}/vendor-es5.js`, '.ng.common.locales');
4242
await expectFileToMatch(`${outputPath}/vendor-es2015.js`, '.ng.common.locales');
4343

44+
// Verify the locale data is browser compatible
45+
await expectToFail(() => expectFileToMatch(`${outputPath}/vendor-es5.js`, /\bconst\b/));
46+
await expectFileToMatch(`${outputPath}/vendor-es2015.js`, /\bconst\b/);
47+
48+
// Verify locale data comments are removed in production
49+
await expectToFail(() =>
50+
expectFileToMatch(`${outputPath}/vendor-es5.js`, '// See angular/tools/gulp-tasks/cldr/extract.js'),
51+
);
52+
await expectToFail(() =>
53+
expectFileToMatch(`${outputPath}/vendor-es2015.js`, '// See angular/tools/gulp-tasks/cldr/extract.js'),
54+
);
55+
4456
// Execute Application E2E tests with dev server
4557
await ng('e2e', `--configuration=${lang}`, '--port=0');
4658

0 commit comments

Comments
 (0)
Please sign in to comment.