Skip to content

Commit 1359095

Browse files
committed
fix(@angular-devkit/build-angular): downlevel and optimize locale data
Locale data is now guaranteed to be compatible with the ECMAScript level of the application bundles. The locale data is also optimized to remove comments and unnecessary whitespace. Fixes: angular#17497
1 parent d008cf2 commit 1359095

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

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

Lines changed: 28 additions & 11 deletions
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

@@ -753,7 +753,7 @@ async function inlineLocalesDirect(ast: ParseResult, options: InlineOptions) {
753753
let localeDataSource: Source | null = null;
754754
const localeDataPath = i18n.locales[locale] && i18n.locales[locale].dataPath;
755755
if (localeDataPath) {
756-
const localeDataContent = await loadLocaleData(localeDataPath, true);
756+
const localeDataContent = await loadLocaleData(localeDataPath, true, options.es5);
757757
localeDataSource = new OriginalSource(localeDataContent, path.basename(localeDataPath));
758758
}
759759

@@ -870,19 +870,36 @@ function findLocalizePositions(
870870
return positions;
871871
}
872872

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

877-
// NOTE: This can be removed once the locale data files are preprocessed in the framework
878-
if (optimize) {
879-
const result = await terserMangle(content, {
880-
compress: true,
881-
ecma: 5,
882-
});
877+
// Downlevel and optimize the data
878+
const transformResult = await transformAsync(content, {
879+
filename: path,
880+
// The types do not include the false option even though it is valid
881+
// tslint:disable-next-line: no-any
882+
inputSourceMap: false as any,
883+
babelrc: false,
884+
configFile: false,
885+
presets: [
886+
[
887+
require.resolve('@babel/preset-env'),
888+
{
889+
bugfixes: true,
890+
// IE 9 is the oldest support browser
891+
targets: es5 ? { ie: '9' } : { esmodules: true },
892+
},
893+
],
894+
],
895+
minified: allowMinify && optimize,
896+
compact: !shouldBeautify && optimize,
897+
comments: !optimize,
898+
});
883899

884-
return result.code;
900+
if (!transformResult || !transformResult.code) {
901+
throw new Error(`Unknown error occurred processing bundle for "${path}".`);
885902
}
886903

887-
return content;
904+
return transformResult.code;
888905
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ 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+
4448
// Execute Application E2E tests with dev server
4549
await ng('e2e', `--configuration=${lang}`, '--port=0');
4650

0 commit comments

Comments
 (0)