Skip to content

Commit a3a1023

Browse files
committed
fix(@schematics/angular): migrate localized base HREF options for 9.0
1 parent 78217d9 commit a3a1023

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

packages/schematics/angular/migrations/update-9/update-workspace-config.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function addProjectI18NOptions(
7171
}
7272

7373
// browser builder options
74-
let locales: Record<string, string> | undefined;
74+
let locales: Record<string, string | { translation: string; baseHref: string }> | undefined;
7575
const options = getAllOptions(browserConfig);
7676
for (const option of options) {
7777
const localeId = findPropertyInAstObject(option, 'i18nLocale');
@@ -87,12 +87,30 @@ function addProjectI18NOptions(
8787
const localIdValue = localeId.value;
8888
const localeFileValue = localeFile.value;
8989

90+
const baseHref = findPropertyInAstObject(option, 'baseHref');
91+
let baseHrefValue;
92+
if (baseHref && baseHref.kind === 'string' && baseHref.value !== `/${localIdValue}/`) {
93+
baseHrefValue = baseHref.value;
94+
}
95+
9096
if (!locales) {
9197
locales = {
92-
[localIdValue]: localeFileValue,
98+
[localIdValue]:
99+
baseHrefValue === undefined
100+
? localeFileValue
101+
: {
102+
translation: localeFileValue,
103+
baseHref: baseHrefValue,
104+
},
93105
};
94106
} else {
95-
locales[localIdValue] = localeFileValue;
107+
locales[localIdValue] =
108+
baseHrefValue === undefined
109+
? localeFileValue
110+
: {
111+
translation: localeFileValue,
112+
baseHref: baseHrefValue,
113+
};
96114
}
97115
}
98116

@@ -145,6 +163,12 @@ function addBuilderI18NOptions(recorder: UpdateRecorder, builderConfig: JsonAstO
145163
if (i18nFormat) {
146164
removePropertyInAstObject(recorder, option, 'i18nFormat');
147165
}
166+
167+
// localize base HREF values are either automatic or controlled by the i18n configuration
168+
const baseHref = findPropertyInAstObject(option, 'baseHref');
169+
if (localeId && i18nFile && baseHref) {
170+
removePropertyInAstObject(recorder, option, 'baseHref');
171+
}
148172
}
149173
}
150174

packages/schematics/angular/migrations/update-9/update-workspace-config_spec.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const stylesExpectWithLazy = [
5555
const workspacePath = '/angular.json';
5656

5757
// tslint:disable:no-big-function
58-
describe('Migration to version 9', () => {
58+
fdescribe('Migration to version 9', () => {
5959
describe('Migrate workspace config', () => {
6060
const schematicRunner = new SchematicTestRunner(
6161
'migrations',
@@ -350,6 +350,41 @@ describe('Migration to version 9', () => {
350350
expect(config.configurations.de.i18nLocale).toBeUndefined();
351351
});
352352

353+
it('should remove baseHref option when used with i18n options and uses locale value', async () => {
354+
let config = getWorkspaceTargets(tree);
355+
config.build.options = getI18NConfig('fr');
356+
config.build.configurations.de = { ...getI18NConfig('de'), baseHref: '/de/' };
357+
updateWorkspaceTargets(tree, config);
358+
359+
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
360+
config = getWorkspaceTargets(tree2).build;
361+
expect(config.configurations.de.baseHref).toBeUndefined();
362+
});
363+
364+
it('should keep baseHref option when not used with i18n options', async () => {
365+
let config = getWorkspaceTargets(tree);
366+
config.build.options = getI18NConfig('fr');
367+
config.build.configurations.de = getI18NConfig('de');
368+
config.build.configurations.staging = { baseHref: '/de/' };
369+
updateWorkspaceTargets(tree, config);
370+
371+
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
372+
config = getWorkspaceTargets(tree2).build;
373+
expect(config.configurations.staging.baseHref).toBe('/de/');
374+
});
375+
376+
it('should keep main baseHref option when used with i18n options', async () => {
377+
let config = getWorkspaceTargets(tree);
378+
config.build.options = { baseHref: '/my-app/' };
379+
config.build.configurations.de = { ...getI18NConfig('de'), baseHref: '/de/' };
380+
updateWorkspaceTargets(tree, config);
381+
382+
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
383+
config = getWorkspaceTargets(tree2).build;
384+
expect(config.options.baseHref).toBe('/my-app/');
385+
expect(config.configurations.de.baseHref).toBeUndefined();
386+
});
387+
353388
it('should remove deprecated extract-i18n options', async () => {
354389
let config = getWorkspaceTargets(tree);
355390
config['extract-i18n'].options.i18nFormat = 'xmb';
@@ -392,6 +427,22 @@ describe('Migration to version 9', () => {
392427
fr: 'src/locale/messages.fr.xlf',
393428
});
394429
});
430+
431+
it(`should add i18n 'locales' project config when using baseHref options`, async () => {
432+
const config = getWorkspaceTargets(tree);
433+
config.build.options = { baseHref: '/my-app/' };
434+
config.build.configurations.fr = { ...getI18NConfig('fr'), baseHref: '/fr/' };
435+
config.build.configurations.de = { ...getI18NConfig('de'), baseHref: '/abc/' };
436+
updateWorkspaceTargets(tree, config);
437+
438+
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
439+
const projectConfig = JSON.parse(tree2.readContent(workspacePath)).projects['migration-test'];
440+
expect(projectConfig.i18n.sourceLocale).toBeUndefined();
441+
expect(projectConfig.i18n.locales).toEqual({
442+
de: { translation: 'src/locale/messages.de.xlf', baseHref: '/abc/' },
443+
fr: 'src/locale/messages.fr.xlf',
444+
});
445+
});
395446
});
396447

397448
describe('when i18n builder options are not set', () => {

0 commit comments

Comments
 (0)