Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): correctly extract messages when u…
Browse files Browse the repository at this point in the history
…sing 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)
  • Loading branch information
alan-agius4 authored and dgp1130 committed Dec 1, 2021
1 parent 0a8c13d commit b835389
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export default function localizeExtractLoader(
content: string,
map: LoaderSourceMap,
) {
// This loader is not cacheable due to how message extraction works.
// Extracted messages are not part of webpack pipeline and hence they cannot be retrieved from cache.
// TODO: We should investigate in the future on making this deterministic and more cacheable.
this.cacheable(false);

const options = this.getOptions();
const callback = this.async();

Expand Down
36 changes: 36 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/extract-ivy-disk-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { join } from 'path';
import { getGlobalVariable } from '../../utils/env';
import { expectFileToMatch, rimraf, writeFile } from '../../utils/fs';
import { installPackage, uninstallPackage } from '../../utils/packages';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { readNgVersion } from '../../utils/version';

export default async function () {
// Enable disk cache
updateJsonFile('angular.json', (config) => {
config.cli ??= {};
config.cli.cache = { environment: 'all' };
});

// Setup an i18n enabled component
await ng('generate', 'component', 'i18n-test');
await writeFile(join('src/app/i18n-test', 'i18n-test.component.html'), '<p i18n>Hello world</p>');

// Install correct version
let localizeVersion = '@angular/localize@' + readNgVersion();
if (getGlobalVariable('argv')['ng-snapshots']) {
localizeVersion = require('../../ng-snapshot/package.json').dependencies['@angular/localize'];
}

await installPackage(localizeVersion);

for (let i = 0; i < 2; i++) {
// Run the extraction twice and make sure the second time round works with cache.
await rimraf('messages.xlf');
await ng('extract-i18n');
await expectFileToMatch('messages.xlf', 'Hello world');
}

await uninstallPackage('@angular/localize');
}
4 changes: 3 additions & 1 deletion tests/legacy-cli/e2e/tests/i18n/extract-ivy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path';
import { getGlobalVariable } from '../../utils/env';
import { writeFile } from '../../utils/fs';
import { expectFileToMatch, writeFile } from '../../utils/fs';
import { installPackage, uninstallPackage } from '../../utils/packages';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
Expand Down Expand Up @@ -31,5 +31,7 @@ export default async function () {
throw new Error('Expected no warnings to be shown');
}

expectFileToMatch('messages.xlf', 'Hello world');

await uninstallPackage('@angular/localize');
}

0 comments on commit b835389

Please sign in to comment.