88
99import type { ɵParsedTranslation } from '@angular/localize' ;
1010import { transform } from 'esbuild' ;
11+ import assert from 'node:assert' ;
1112import fs from 'node:fs/promises' ;
1213import os from 'node:os' ;
1314import path from 'node:path' ;
@@ -20,6 +21,7 @@ import {
2021 createOutputFile ,
2122} from '../esbuild/bundler-files' ;
2223import { I18nInliner , type I18nInlinerOptions } from './i18n-inliner' ;
24+ import { createLocalizedOutputHasher } from './i18n-output-hashes' ;
2325
2426/**
2527 * A module that uses a `$localize` message with an explicit message identifier so that the
@@ -1185,3 +1187,161 @@ describe('I18nInliner', () => {
11851187 } ) ;
11861188 } ) ;
11871189} ) ;
1190+
1191+ describe ( 'Localized output hashing' , ( ) => {
1192+ beforeAll ( initializeHash ) ;
1193+
1194+ function renamed ( names : ReadonlyMap < string , string > , file : string ) : string {
1195+ const result = names . get ( file ) ;
1196+ assert ( result , 'Expected renamed output file: ' + file ) ;
1197+
1198+ return result ;
1199+ }
1200+
1201+ function outputMetadata (
1202+ ...entries : [ string , [ string , 'import-statement' | 'dynamic-import' , boolean ?] [ ] ] [ ]
1203+ ) : Parameters < typeof createLocalizedOutputHasher > [ 2 ] {
1204+ return {
1205+ inputs : { } ,
1206+ outputs : Object . fromEntries (
1207+ entries . map ( ( [ file , imports ] ) => [
1208+ file ,
1209+ {
1210+ bytes : 0 ,
1211+ inputs : { } ,
1212+ exports : [ ] ,
1213+ imports : imports . map ( ( [ path , kind , external ] ) => ( { path, kind, external } ) ) ,
1214+ } ,
1215+ ] ) ,
1216+ ) ,
1217+ } ;
1218+ }
1219+
1220+ it ( 'invalidates a cycle and its importers without invalidating a separate cycle' , ( ) => {
1221+ const original = [
1222+ browserFile ( 'first-AAAAAAAA.js' , 'console.log($localize`A`); import "./second-BBBBBBBB.js";' ) ,
1223+ browserFile ( 'second-BBBBBBBB.js' , 'export {} from "./first-AAAAAAAA.js";' ) ,
1224+ browserFile ( 'third-CCCCCCCC.js' , 'import "./fourth-DDDDDDDD.js"; console.log($localize`B`);' ) ,
1225+ browserFile ( 'fourth-DDDDDDDD.js' , 'import "./third-CCCCCCCC.js";' ) ,
1226+ browserFile ( 'main-EEEEEEEE.js' , 'import(`./second-BBBBBBBB.js`);' ) ,
1227+ ] ;
1228+ const hash = createLocalizedOutputHasher (
1229+ original ,
1230+ new Set ( ) ,
1231+ outputMetadata (
1232+ [ 'first-AAAAAAAA.js' , [ [ 'second-BBBBBBBB.js' , 'import-statement' ] ] ] ,
1233+ [ 'second-BBBBBBBB.js' , [ [ 'first-AAAAAAAA.js' , 'import-statement' ] ] ] ,
1234+ [ 'third-CCCCCCCC.js' , [ [ 'fourth-DDDDDDDD.js' , 'import-statement' ] ] ] ,
1235+ [ 'fourth-DDDDDDDD.js' , [ [ 'third-CCCCCCCC.js' , 'import-statement' ] ] ] ,
1236+ [ 'main-EEEEEEEE.js' , [ [ 'second-BBBBBBBB.js' , 'dynamic-import' ] ] ] ,
1237+ ) ,
1238+ ) ;
1239+ const localize = ( text : string ) =>
1240+ original . map ( ( file ) =>
1241+ browserFile (
1242+ file . path ,
1243+ file . text
1244+ . replace ( '$localize`A`' , JSON . stringify ( text ) )
1245+ . replace ( '$localize`B`' , '"Other"' ) ,
1246+ ) ,
1247+ ) ;
1248+
1249+ const firstFiles = localize ( 'Before' ) ;
1250+ const first = hash ( firstFiles ) ;
1251+ const secondFiles = localize ( 'Après 😀' ) ;
1252+ const second = hash ( secondFiles ) ;
1253+
1254+ expect ( second . size ) . toBe ( 5 ) ;
1255+ for ( const path of [ 'first-AAAAAAAA.js' , 'second-BBBBBBBB.js' , 'main-EEEEEEEE.js' ] ) {
1256+ expect ( second . get ( path ) ) . not . toBe ( first . get ( path ) ) ;
1257+ }
1258+ for ( const path of [ 'third-CCCCCCCC.js' , 'fourth-DDDDDDDD.js' ] ) {
1259+ expect ( second . get ( path ) ) . toBe ( first . get ( path ) ) ;
1260+ }
1261+ expect ( findFile ( secondFiles , renamed ( second , 'first-AAAAAAAA.js' ) ) . text ) . toContain (
1262+ renamed ( second , 'second-BBBBBBBB.js' ) ,
1263+ ) ;
1264+ expect ( findFile ( secondFiles , renamed ( second , 'second-BBBBBBBB.js' ) ) . text ) . toContain (
1265+ renamed ( second , 'first-AAAAAAAA.js' ) ,
1266+ ) ;
1267+ expect ( findFile ( secondFiles , renamed ( second , 'main-EEEEEEEE.js' ) ) . text ) . toContain (
1268+ renamed ( second , 'second-BBBBBBBB.js' ) ,
1269+ ) ;
1270+ } ) ;
1271+
1272+ it ( 'rewrites only actual module specifiers using workspace-relative metadata' , ( ) => {
1273+ const main = 'app/main-AAAAAAAA.js' ;
1274+ const leaf = 'chunk-BBBBBBBB.js' ;
1275+ const external = 'chunk-CCCCCCCC.js' ;
1276+ const source = `export {a, b} from '../${ leaf } '; import(\`../${ leaf } \`); import('../${ external } '); console.log('../${ leaf } ');` ;
1277+ const files = [
1278+ browserFile ( main , source ) ,
1279+ browserFile ( leaf , 'export const a = $localize`Hello`, b = 1;' ) ,
1280+ browserFile ( external , 'export const a = $localize`Hello`;' ) ,
1281+ ] ;
1282+ const hash = createLocalizedOutputHasher (
1283+ files ,
1284+ new Set ( ) ,
1285+ outputMetadata (
1286+ [
1287+ main ,
1288+ [
1289+ [ './' + leaf , 'import-statement' ] ,
1290+ [ './' + leaf , 'dynamic-import' ] ,
1291+ [ external , 'dynamic-import' , true ] ,
1292+ ] ,
1293+ ] ,
1294+ [ leaf , [ ] ] ,
1295+ [ external , [ ] ] ,
1296+ ) ,
1297+ ) ;
1298+ const localized = files . map ( ( file ) =>
1299+ browserFile ( file . path , file . text . replace ( '$localize`Hello`' , '"Bonjour"' ) ) ,
1300+ ) ;
1301+
1302+ const names = hash ( localized ) ;
1303+ const newLeaf = renamed ( names , leaf ) ;
1304+ expect ( renamed ( names , external ) ) . not . toBe ( external ) ;
1305+ const mainText = findFile ( localized , renamed ( names , main ) ) . text ;
1306+ expect ( mainText ) . toBe (
1307+ `export {a, b} from '../${ newLeaf } '; import(\`../${ newLeaf } \`); import('../${ external } '); console.log('../${ leaf } ');` ,
1308+ ) ;
1309+ } ) ;
1310+
1311+ it ( 'rewrites a worker URL and its source map without rewriting other strings' , ( ) => {
1312+ const worker = 'worker-aB3_-9cD.js' ;
1313+ const main = 'main-BBBBBBBB.js' ;
1314+ const mainFile = browserFile (
1315+ main ,
1316+ `console.log("${ worker } ");
1317+ new Worker(new URL("${ worker } ", import.meta.url));
1318+ new Worker(new URL(\`./${ worker } \`, import.meta.url));` ,
1319+ ) ;
1320+ const sourceMap = browserFile ( worker + '.map' , JSON . stringify ( { version : 3 , file : worker } ) ) ;
1321+ const hash = createLocalizedOutputHasher (
1322+ [ mainFile , browserFile ( worker , 'console.log($localize`Hello`);' ) , sourceMap ] ,
1323+ new Set ( ) ,
1324+ outputMetadata ( [ main , [ ] ] , [ worker , [ ] ] ) ,
1325+ ) ;
1326+ const files = [
1327+ mainFile . clone ( ) ,
1328+ browserFile (
1329+ worker ,
1330+ `console.log("Bonjour");
1331+ //# sourceMappingURL=${ worker } .map
1332+ ` ,
1333+ ) ,
1334+ sourceMap . clone ( ) ,
1335+ ] ;
1336+
1337+ const names = hash ( files ) ;
1338+ const newWorker = renamed ( names , worker ) ;
1339+ expect ( newWorker ) . not . toBe ( worker ) ;
1340+ const mainText = findFile ( files , renamed ( names , main ) ) . text ;
1341+ expect ( mainText ) . toContain ( `new URL("${ newWorker } "` ) ;
1342+ expect ( mainText ) . toContain ( `new URL(\`./${ newWorker } \`` ) ;
1343+ expect ( mainText ) . toContain ( `console.log("${ worker } ")` ) ;
1344+ expect ( findFile ( files , newWorker ) . text ) . toContain ( `sourceMappingURL=${ newWorker } .map` ) ;
1345+ expect ( JSON . parse ( findFile ( files , newWorker + '.map' ) . text ) . file ) . toBe ( newWorker ) ;
1346+ } ) ;
1347+ } ) ;
0 commit comments