Skip to content

Commit 6a617ff

Browse files
alan-agius4dgp1130
authored andcommittedJan 12, 2022
fix(@angular-devkit/build-angular): ensure $localize calls are replaced in watch mode
When `translations` is undefined `$localize` calls are not replaced. https://github.com/angular/angular-cli/blob/2c9a33dddb38694b6940ec6981c49904de1ab636/packages/angular_devkit/build_angular/src/builders/dev-server/index.ts#L382 Closes #22435 (cherry picked from commit 426ddb6)
1 parent d674dcd commit 6a617ff

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/dev-server/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ async function setupLocalize(
378378
addError(compilation, message);
379379
},
380380
});
381-
i18nLoaderOptions.translation = localeDescription.translation;
381+
i18nLoaderOptions.translation = localeDescription.translation ?? {};
382382
}
383383

384384
compilation.hooks.finishModules.tap('build-angular', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/* eslint-disable max-len */
10+
import fetch from 'node-fetch'; // eslint-disable-line import/no-extraneous-dependencies
11+
import { concatMap, count, take, timeout } from 'rxjs/operators';
12+
import { URL } from 'url';
13+
import { serveWebpackBrowser } from '../../index';
14+
import {
15+
BASE_OPTIONS,
16+
BUILD_TIMEOUT,
17+
DEV_SERVER_BUILDER_INFO,
18+
describeBuilder,
19+
setupBrowserTarget,
20+
} from '../setup';
21+
22+
describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
23+
describe('Behavior: "i18n $localize calls are replaced during watching"', () => {
24+
beforeEach(() => {
25+
harness.useProject('test', {
26+
root: '.',
27+
sourceRoot: 'src',
28+
cli: {
29+
cache: {
30+
enabled: false,
31+
},
32+
},
33+
i18n: {
34+
sourceLocale: {
35+
'code': 'fr',
36+
},
37+
},
38+
});
39+
40+
setupBrowserTarget(harness, { localize: ['fr'] });
41+
});
42+
43+
it('$localize are replaced in watch', async () => {
44+
harness.useTarget('serve', {
45+
...BASE_OPTIONS,
46+
});
47+
48+
await harness.writeFile(
49+
'src/app/app.component.html',
50+
`
51+
<p id="hello" i18n="An introduction header for this sample">Hello {{ title }}! </p>
52+
`,
53+
);
54+
55+
const buildCount = await harness
56+
.execute()
57+
.pipe(
58+
timeout(BUILD_TIMEOUT),
59+
concatMap(async ({ result }, index) => {
60+
expect(result?.success).toBe(true);
61+
62+
const response = await fetch(new URL('main.js', `${result?.baseUrl}`));
63+
expect(await response?.text()).not.toContain('$localize`:');
64+
65+
switch (index) {
66+
case 0: {
67+
await harness.modifyFile('src/app/app.component.html', (content) =>
68+
content.replace('introduction', 'intro'),
69+
);
70+
break;
71+
}
72+
}
73+
}),
74+
take(2),
75+
count(),
76+
)
77+
.toPromise();
78+
79+
expect(buildCount).toBe(2);
80+
});
81+
});
82+
});

‎packages/angular_devkit/build_angular/src/builders/dev-server/tests/setup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const BASE_OPTIONS = Object.freeze<Schema>({
3838
* Maximum time for single build/rebuild
3939
* This accounts for CI variability.
4040
*/
41-
export const BUILD_TIMEOUT = 15000;
41+
export const BUILD_TIMEOUT = 15_000;
4242

4343
/**
4444
* Cached browser builder option schema

0 commit comments

Comments
 (0)
Please sign in to comment.