Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): ensure HTML lang attribute is set…
Browse files Browse the repository at this point in the history
… when localizing
  • Loading branch information
clydin authored and mgechev committed Dec 2, 2019
1 parent 5ec4a54 commit 406d792
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 19 deletions.
21 changes: 12 additions & 9 deletions packages/angular_devkit/build_angular/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export function buildWebpackBrowser(
const host = new NodeJsSyncHost();
const root = normalize(context.workspaceRoot);
const baseOutputPath = path.resolve(context.workspaceRoot, options.outputPath);
let outputPaths: undefined | string[];
let outputPaths: undefined | Map<string, string>;

// Check Angular version.
assertCompatibleAngularVersion(context.workspaceRoot, context.logger);
Expand Down Expand Up @@ -304,7 +304,7 @@ export function buildWebpackBrowser(
emittedFiles,
i18n,
baseOutputPath,
outputPaths,
Array.from(outputPaths.values()),
scriptsEntryPointName,
// tslint:disable-next-line: no-non-null-assertion
webpackStats.outputPath!,
Expand Down Expand Up @@ -529,7 +529,7 @@ export function buildWebpackBrowser(
),
},
],
outputPaths,
Array.from(outputPaths.values()),
'',
);
} catch (err) {
Expand Down Expand Up @@ -561,7 +561,7 @@ export function buildWebpackBrowser(
normalize(projectRoot),
projectSourceRoot === undefined ? undefined : normalize(projectSourceRoot),
),
outputPaths,
Array.from(outputPaths.values()),
context.workspaceRoot,
);
} catch (err) {
Expand Down Expand Up @@ -646,7 +646,7 @@ export function buildWebpackBrowser(
emittedFiles,
i18n,
baseOutputPath,
outputPaths,
Array.from(outputPaths.values()),
scriptsEntryPointName,
// tslint:disable-next-line: no-non-null-assertion
webpackStats.outputPath!,
Expand All @@ -660,7 +660,7 @@ export function buildWebpackBrowser(
}

if (options.index) {
for (const outputPath of outputPaths) {
for (const [locale, outputPath] of outputPaths.entries()) {
try {
await generateIndex(
outputPath,
Expand All @@ -670,6 +670,8 @@ export function buildWebpackBrowser(
noModuleFiles,
moduleFiles,
transforms.indexHtml,
// i18nLocale is used when Ivy is disabled
locale || options.i18nLocale,
);
} catch (err) {
return { success: false, error: mapErrorToMessage(err) };
Expand All @@ -678,7 +680,7 @@ export function buildWebpackBrowser(
}

if (!options.watch && options.serviceWorker) {
for (const outputPath of outputPaths) {
for (const outputPath of outputPaths.values()) {
try {
await augmentAppWithServiceWorker(
host,
Expand All @@ -703,7 +705,7 @@ export function buildWebpackBrowser(
...event,
baseOutputPath,
outputPath: baseOutputPath,
outputPaths: outputPaths || [baseOutputPath],
outputPaths: outputPaths && Array.from(outputPaths.values()) || [baseOutputPath],
} as BrowserBuilderOutput),
),
);
Expand All @@ -719,6 +721,7 @@ function generateIndex(
noModuleFiles: EmittedFiles[] | undefined,
moduleFiles: EmittedFiles[] | undefined,
transformer?: IndexHtmlTransform,
locale?: string,
): Promise<void> {
const host = new NodeJsSyncHost();

Expand All @@ -736,7 +739,7 @@ function generateIndex(
styles: options.styles,
postTransform: transformer,
crossOrigin: options.crossOrigin,
lang: options.i18nLocale,
lang: locale,
}).toPromise();
}

Expand Down
4 changes: 2 additions & 2 deletions packages/angular_devkit/build_angular/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function execute(
const tsConfig = readTsconfig(options.tsConfig, root);
const target = tsConfig.options.target || ScriptTarget.ES5;
const baseOutputPath = path.resolve(root, options.outputPath);
let outputPaths: undefined | string[];
let outputPaths: undefined | Map<string, string>;

return from(initialize(options, context, transforms.webpackConfiguration)).pipe(
concatMap(({ config, i18n }) => {
Expand All @@ -81,7 +81,7 @@ export function execute(
emittedFiles,
i18n,
baseOutputPath,
outputPaths,
Array.from(outputPaths.values()),
[],
// tslint:disable-next-line: no-non-null-assertion
webpackStats.outputPath!,
Expand Down
18 changes: 11 additions & 7 deletions packages/angular_devkit/build_angular/src/utils/output-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import { existsSync, mkdirSync } from 'fs';
import { join } from 'path';
import { I18nOptions } from './i18n-options';

export function ensureOutputPaths(baseOutputPath: string, i18n: I18nOptions): string[] {
const outputPaths =
i18n.shouldInline && !i18n.flatOutput
? [...i18n.inlineLocales].map(l => join(baseOutputPath, l))
: [i18n.veCompatLocale ? join(baseOutputPath, i18n.veCompatLocale) : baseOutputPath];
export function ensureOutputPaths(baseOutputPath: string, i18n: I18nOptions): Map<string, string> {
const outputPaths: [string, string][] =
i18n.shouldInline
? [...i18n.inlineLocales].map(l => [l, i18n.flatOutput ? baseOutputPath : join(baseOutputPath, l)])
: [
i18n.veCompatLocale
? [i18n.veCompatLocale, join(baseOutputPath, i18n.veCompatLocale)]
: ['', baseOutputPath],
];

for (const outputPath of outputPaths) {
for (const [, outputPath] of outputPaths) {
if (!existsSync(outputPath)) {
mkdirSync(outputPath, { recursive: true });
}
}

return outputPaths;
return new Map(outputPaths);
}
3 changes: 3 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-dl-xliff2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export async function executeTest() {
await expectFileToMatch(`${outputPath}/vendor-es5.js`, lang);
await expectFileToMatch(`${outputPath}/vendor-es2015.js`, lang);

// Verify the HTML lang attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `lang="${lang}"`);

// Verify the locale data is registered using the global files
await expectFileToMatch(`${outputPath}/vendor-es5.js`, '.ng.common.locales');
await expectFileToMatch(`${outputPath}/vendor-es2015.js`, '.ng.common.locales');
Expand Down
3 changes: 3 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default async function() {
await expectFileNotToExist(`${outputPath}/main-es5.js`);
await expectFileToMatch(`${outputPath}/main.js`, lang);

// Verify the HTML lang attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `lang="${lang}"`);

// Execute Application E2E tests with dev server
await ng('e2e', `--configuration=${lang}`, '--port=0');

Expand Down
3 changes: 3 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default async function() {
await expectFileNotToExist(`${outputPath}/main-es2015.js`);
await expectFileToMatch(`${outputPath}/main.js`, lang);

// Verify the HTML lang attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `lang="${lang}"`);

// Execute Application E2E tests with dev server
await ng('e2e', `--configuration=${lang}`, '--port=0');

Expand Down
5 changes: 4 additions & 1 deletion tests/legacy-cli/e2e/tests/i18n/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export async function setupI18nConfig(useLocalize = true, format: keyof typeof f
for (const { lang, outputPath } of langTranslations) {
if (!useLocalize) {
if (lang == sourceLocale) {
buildConfigs[lang] = { outputPath };
buildConfigs[lang] = { outputPath, i18nLocale: lang };
} else {
buildConfigs[lang] = {
outputPath,
Expand Down Expand Up @@ -262,6 +262,9 @@ export default async function () {
await expectFileToMatch(`${outputPath}/main-es5.js`, translation.helloPartial);
await expectFileToMatch(`${outputPath}/main-es2015.js`, translation.helloPartial);

// Verify the HTML lang attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `lang="${lang}"`);

// Execute Application E2E tests with dev server
await ng('e2e', `--configuration=${lang}`, '--port=0');

Expand Down
3 changes: 3 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/ve-localize-es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export default async function() {
await expectFileToMatch(`${outputPath}/main.js`, translation.helloPartial);
await expectToFail(() => expectFileToMatch(`${outputPath}/main.js`, '$localize`'));

// Verify the HTML lang attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `lang="${lang}"`);

// Execute Application E2E tests with dev server
await ng('e2e', `--configuration=${lang}`, '--port=0');

Expand Down

0 comments on commit 406d792

Please sign in to comment.