66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import * as fs from 'node:fs' ;
10- import { appendToFile } from '../../utils/fs' ;
9+ import assert from 'node:assert/strict' ;
10+ import { readdirSync , readFileSync } from 'node:fs' ;
11+ import { getGlobalVariable } from '../../utils/env' ;
12+ import { replaceInFile } from '../../utils/fs' ;
1113import { ng } from '../../utils/process' ;
14+ import { updateJsonFile } from '../../utils/project' ;
1215import { langTranslations , setupI18nConfig } from './setup' ;
1316
1417const OUTPUT_RE = / ^ (?< name > (?: m a i n | v e n d o r | \d + ) ) (?: \. | - ) (?< hash > [ a - z 0 - 9 ] + ) \. j s $ / i;
@@ -17,11 +20,27 @@ export default async function () {
1720 // Setup i18n tests and config.
1821 await setupI18nConfig ( ) ;
1922
23+ const esbuild = getGlobalVariable ( 'argv' ) [ 'esbuild' ] ;
24+ if ( esbuild ) {
25+ // Test the polyfills bundle that contains only the CLI-generated locale initialization.
26+ await updateJsonFile ( 'angular.json' , ( workspace ) => {
27+ workspace . projects [ 'test-project' ] . architect [ 'build' ] . options . polyfills = [ ] ;
28+ } ) ;
29+ }
30+
2031 // Build each locale and record output file hashes
2132 const hashes = new Map < string , string > ( ) ;
33+ const polyfills = new Map < string , { name : string ; content : string } > ( ) ;
2234 await ng ( 'build' , '--output-hashing=all' , '--configuration=development' ) ;
2335 for ( const { lang, outputPath } of langTranslations ) {
24- for ( const entry of fs . readdirSync ( outputPath ) ) {
36+ for ( const entry of readdirSync ( outputPath ) ) {
37+ if ( esbuild && / ^ p o l y f i l l s - [ a - z 0 - 9 ] + \. j s $ / i. test ( entry ) ) {
38+ polyfills . set ( lang , {
39+ name : entry ,
40+ content : readFileSync ( `${ outputPath } /${ entry } ` , 'utf-8' ) ,
41+ } ) ;
42+ }
43+
2544 const match = entry . match ( OUTPUT_RE ) ;
2645 if ( ! match ?. groups ) {
2746 continue ;
@@ -36,13 +55,27 @@ export default async function () {
3655 throw new Error ( 'No output entries found.' ) ;
3756 }
3857
39- // Alter content of a used translation file
40- await appendToFile ( 'src/locale/messages.fr.xlf' , '\n' ) ;
58+ if ( esbuild ) {
59+ assert . equal ( polyfills . size , langTranslations . length ) ;
60+ assert . equal ( new Set ( Array . from ( polyfills . values ( ) , ( { name } ) => name ) ) . size , 1 ) ;
61+ }
62+
63+ // Change a translated message without changing any application source.
64+ await replaceInFile ( 'src/locale/messages.fr.xlf' , 'Bonjour' , 'Salut' ) ;
4165
4266 // Build each locale and ensure hashes are different
4367 await ng ( 'build' , '--output-hashing=all' , '--configuration=development' ) ;
4468 for ( const { lang, outputPath } of langTranslations ) {
45- for ( const entry of fs . readdirSync ( outputPath ) ) {
69+ const localePolyfills = polyfills . get ( lang ) ;
70+ if ( localePolyfills ) {
71+ assert . equal (
72+ readFileSync ( `${ outputPath } /${ localePolyfills . name } ` , 'utf-8' ) ,
73+ localePolyfills . content ,
74+ `Expected the generated polyfills to stay cacheable for ${ lang } .` ,
75+ ) ;
76+ }
77+
78+ for ( const entry of readdirSync ( outputPath ) ) {
4679 const match = entry . match ( OUTPUT_RE ) ;
4780 if ( ! match ?. groups ) {
4881 continue ;
0 commit comments