Skip to content

Commit 895fa62

Browse files
clydinvikerman
authored andcommitted
feat(@angular-devkit/build-angular): support deprecated i18n options with new configuration
1 parent bc831e8 commit 895fa62

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
normalizeSourceMaps,
5252
} from '../utils';
5353
import { copyAssets } from '../utils/copy-assets';
54-
import { I18nOptions, createI18nOptions } from '../utils/i18n-options';
54+
import { I18nOptions, createI18nOptions, mergeDeprecatedI18nOptions } from '../utils/i18n-options';
5555
import { createTranslationLoader } from '../utils/load-translations';
5656
import {
5757
InlineOptions,
@@ -163,9 +163,21 @@ async function initialize(
163163
throw new Error('The builder requires a target.');
164164
}
165165

166+
const tsConfig = readTsconfig(options.tsConfig, context.workspaceRoot);
167+
const usingIvy = tsConfig.options.enableIvy !== false;
166168
const metadata = await context.getProjectMetadata(context.target);
167169
const i18n = createI18nOptions(metadata, options.localize);
168170

171+
// Until 11.0, support deprecated i18n options when not using new localize option
172+
// i18nFormat is automatically calculated
173+
if (options.localize === undefined && usingIvy) {
174+
mergeDeprecatedI18nOptions(i18n, options.i18nLocale, options.i18nFile);
175+
} else if (options.localize !== undefined && !usingIvy) {
176+
options.localize = undefined;
177+
178+
context.logger.warn(`Option 'localize' is not supported with View Engine.`);
179+
}
180+
169181
if (i18n.inlineLocales.size > 0) {
170182
// Load locales
171183
const loader = await createTranslationLoader();
@@ -283,8 +295,14 @@ export function buildWebpackBrowser(
283295

284296
return { success };
285297
} else if (success) {
286-
if (!fs.existsSync(baseOutputPath)) {
287-
fs.mkdirSync(baseOutputPath, { recursive: true });
298+
const outputPaths =
299+
i18n.shouldInline && !i18n.flatOutput
300+
? [...i18n.inlineLocales].map(l => path.join(baseOutputPath, l))
301+
: [baseOutputPath];
302+
for (const outputPath of outputPaths) {
303+
if (!fs.existsSync(outputPath)) {
304+
fs.mkdirSync(outputPath, { recursive: true });
305+
}
288306
}
289307

290308
let noModuleFiles: EmittedFiles[] | undefined;
@@ -475,13 +493,6 @@ export function buildWebpackBrowser(
475493

476494
let hasErrors = false;
477495
try {
478-
for (const locale of i18n.inlineLocales) {
479-
const localeOutputPath = path.join(baseOutputPath, locale);
480-
if (!fs.existsSync(localeOutputPath)) {
481-
fs.mkdirSync(localeOutputPath, { recursive: true });
482-
}
483-
}
484-
485496
for await (const result of executor.inlineAll(inlineActions)) {
486497
if (options.verbose) {
487498
context.logger.info(
@@ -499,9 +510,6 @@ export function buildWebpackBrowser(
499510
}
500511

501512
// Copy any non-processed files into the output locations
502-
const outputPaths = [...i18n.inlineLocales].map(l =>
503-
path.join(baseOutputPath, l),
504-
);
505513
await copyAssets(
506514
[
507515
{
@@ -510,7 +518,7 @@ export function buildWebpackBrowser(
510518
input: webpackStats.outputPath!,
511519
output: '',
512520
ignore: [...processedFiles].map(f =>
513-
// tslint:disable-next-line: no-non-null-assertion
521+
// tslint:disable-next-line: no-non-null-assertion
514522
path.relative(webpackStats.outputPath!, f),
515523
),
516524
},
@@ -546,9 +554,6 @@ export function buildWebpackBrowser(
546554

547555
// Copy assets
548556
if (options.assets) {
549-
const outputPaths = i18n.shouldInline
550-
? [...i18n.inlineLocales].map(l => path.join(baseOutputPath, l))
551-
: [baseOutputPath];
552557
try {
553558
await copyAssets(
554559
normalizeAssetPatterns(
@@ -640,10 +645,6 @@ export function buildWebpackBrowser(
640645
}
641646

642647
if (options.index) {
643-
const outputPaths = i18n.shouldInline
644-
? [...i18n.inlineLocales].map(l => path.join(baseOutputPath, l))
645-
: [baseOutputPath];
646-
647648
for (const outputPath of outputPaths) {
648649
try {
649650
await generateIndex(

packages/angular_devkit/build_angular/src/utils/i18n-options.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface I18nOptions {
1111
inlineLocales: Set<string>;
1212
sourceLocale: string;
1313
locales: Record<string, { file: string; format?: string; translation?: unknown }>;
14+
flatOutput?: boolean;
1415
readonly shouldInline: boolean;
1516
}
1617

@@ -80,3 +81,21 @@ export function createI18nOptions(
8081

8182
return i18n;
8283
}
84+
85+
export function mergeDeprecatedI18nOptions(i18n: I18nOptions, i18nLocale: string | undefined, i18nFile: string | undefined): I18nOptions {
86+
if (i18nFile !== undefined && i18nLocale === undefined) {
87+
throw new Error(`Option 'i18nFile' cannot be used without the 'i18nLocale' option.`);
88+
}
89+
90+
if (i18nLocale !== undefined) {
91+
i18n.inlineLocales.clear();
92+
i18n.inlineLocales.add(i18nLocale);
93+
94+
if (i18nFile !== undefined) {
95+
i18n.locales[i18nLocale] = { file: i18nFile };
96+
i18n.flatOutput = true;
97+
}
98+
}
99+
100+
return i18n;
101+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,16 @@ export async function inlineLocales(options: InlineOptions) {
464464
if (!i18n || i18n.inlineLocales.size === 0) {
465465
return { file: options.filename, diagnostics: [], count: 0 };
466466
}
467+
if (i18n.flatOutput && i18n.inlineLocales.size > 1) {
468+
throw new Error('Flat output is only supported when inlining one locale.');
469+
}
467470

468471
if (!options.code.includes(localizeName)) {
469472
for (const locale of i18n.inlineLocales) {
470-
fs.writeFileSync(path.join(options.outputPath, locale, options.filename), options.code);
473+
fs.writeFileSync(
474+
path.join(options.outputPath, i18n.flatOutput ? '' : locale, options.filename),
475+
options.code,
476+
);
471477
}
472478

473479
return { file: options.filename, diagnostics: [], count: 0 };
@@ -506,7 +512,10 @@ export async function inlineLocales(options: InlineOptions) {
506512
}
507513

508514
const output = content.toString();
509-
fs.writeFileSync(path.join(options.outputPath, locale, options.filename), output);
515+
fs.writeFileSync(
516+
path.join(options.outputPath, i18n.flatOutput ? '' : locale, options.filename),
517+
output,
518+
);
510519
}
511520

512521
return { file: options.filename, diagnostics: diagnostics.messages, count: positions.length };

0 commit comments

Comments
 (0)