Skip to content

Commit b835389

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@angular-devkit/build-angular): correctly extract messages when using cached build (#22266)
* fix(@angular-devkit/build-angular): correctly extract messages when using cached build Extracted messages are not part of Webpack pipeline and hence they cannot be retrieved from cache. Therefore, we need to mark the extraction loader as non cacheable. Closes #22264 * fixup! fix(@angular-devkit/build-angular): correctly extract messages when using cached build (cherry picked from commit 52c6c3d)
1 parent 0a8c13d commit b835389

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

packages/angular_devkit/build_angular/src/builders/extract-i18n/ivy-extract-loader.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export default function localizeExtractLoader(
2121
content: string,
2222
map: LoaderSourceMap,
2323
) {
24+
// This loader is not cacheable due to how message extraction works.
25+
// Extracted messages are not part of webpack pipeline and hence they cannot be retrieved from cache.
26+
// TODO: We should investigate in the future on making this deterministic and more cacheable.
27+
this.cacheable(false);
28+
2429
const options = this.getOptions();
2530
const callback = this.async();
2631

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { join } from 'path';
2+
import { getGlobalVariable } from '../../utils/env';
3+
import { expectFileToMatch, rimraf, writeFile } from '../../utils/fs';
4+
import { installPackage, uninstallPackage } from '../../utils/packages';
5+
import { ng } from '../../utils/process';
6+
import { updateJsonFile } from '../../utils/project';
7+
import { readNgVersion } from '../../utils/version';
8+
9+
export default async function () {
10+
// Enable disk cache
11+
updateJsonFile('angular.json', (config) => {
12+
config.cli ??= {};
13+
config.cli.cache = { environment: 'all' };
14+
});
15+
16+
// Setup an i18n enabled component
17+
await ng('generate', 'component', 'i18n-test');
18+
await writeFile(join('src/app/i18n-test', 'i18n-test.component.html'), '<p i18n>Hello world</p>');
19+
20+
// Install correct version
21+
let localizeVersion = '@angular/localize@' + readNgVersion();
22+
if (getGlobalVariable('argv')['ng-snapshots']) {
23+
localizeVersion = require('../../ng-snapshot/package.json').dependencies['@angular/localize'];
24+
}
25+
26+
await installPackage(localizeVersion);
27+
28+
for (let i = 0; i < 2; i++) {
29+
// Run the extraction twice and make sure the second time round works with cache.
30+
await rimraf('messages.xlf');
31+
await ng('extract-i18n');
32+
await expectFileToMatch('messages.xlf', 'Hello world');
33+
}
34+
35+
await uninstallPackage('@angular/localize');
36+
}

tests/legacy-cli/e2e/tests/i18n/extract-ivy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from 'path';
22
import { getGlobalVariable } from '../../utils/env';
3-
import { writeFile } from '../../utils/fs';
3+
import { expectFileToMatch, writeFile } from '../../utils/fs';
44
import { installPackage, uninstallPackage } from '../../utils/packages';
55
import { ng } from '../../utils/process';
66
import { updateJsonFile } from '../../utils/project';
@@ -31,5 +31,7 @@ export default async function () {
3131
throw new Error('Expected no warnings to be shown');
3232
}
3333

34+
expectFileToMatch('messages.xlf', 'Hello world');
35+
3436
await uninstallPackage('@angular/localize');
3537
}

0 commit comments

Comments
 (0)