@@ -398,7 +398,7 @@ function addExternalAsset(ctx: ExternalAssetContext, absPath: string): string {
398398
399399 const rel = relative ( ctx . absProjectDir , absPath ) . replaceAll ( "\\" , "/" ) ;
400400 const stripped = rel . replace ( / ^ (?: \. \. \/ ) + / , "" ) ;
401- let archivePath = `${ EXT_ASSETS_PREFIX } /${ stripped } ` ;
401+ let archivePath = archiveKey ( `${ EXT_ASSETS_PREFIX } /${ stripped } ` ) ;
402402
403403 if ( ctx . usedArchivePaths . has ( archivePath ) ) {
404404 const ext = posix . extname ( archivePath ) ;
@@ -425,7 +425,7 @@ function includeReferencedProjectFile(
425425 for ( const candidate of [ resolvedPath , fromRoot ] ) {
426426 const absPath = candidate . replace ( / [ ? # ] .* $ / , "" ) ;
427427 if ( ! isPathInside ( absPath , ctx . absProjectDir ) ) continue ;
428- const rel = relative ( ctx . absProjectDir , absPath ) . replaceAll ( "\\" , "/" ) ;
428+ const rel = archiveKey ( relative ( ctx . absProjectDir , absPath ) . replaceAll ( "\\" , "/" ) ) ;
429429 if ( ! rel || ctx . fileContents . has ( rel ) || rel . split ( "/" ) . some ( shouldIgnoreSegment ) ) return ;
430430 if ( ! existsSync ( absPath ) ) continue ;
431431 if ( statSync ( absPath ) . isFile ( ) ) {
@@ -673,6 +673,26 @@ export function buildPublishFileMap(projectDir: string): Map<string, Buffer> {
673673 return stagePublishFiles ( projectDir , Infinity ) as Map < string , Buffer > ;
674674}
675675
676+ export const archiveKey = ( path : string ) : string => path . normalize ( "NFC" ) ;
677+
678+ function composeNamesInText ( fileContents : Map < string , StagedFile > , filePaths : string [ ] ) : void {
679+ const names = new Map < string , string > ( ) ;
680+ for ( const segment of filePaths . flatMap ( ( path ) => path . split ( "/" ) ) ) {
681+ const composed = archiveKey ( segment ) ;
682+ if ( composed === segment ) continue ;
683+ names . set ( segment , composed ) ;
684+ names . set ( encodeURIComponent ( segment ) , encodeURIComponent ( composed ) ) ;
685+ }
686+ if ( names . size === 0 ) return ;
687+ for ( const [ key , file ] of fileContents ) {
688+ if ( ! Buffer . isBuffer ( file ) || ! TEXT_FILE_EXTS . test ( key ) ) continue ;
689+ let text = file . toString ( "utf-8" ) ;
690+ const before = text ;
691+ for ( const [ from , to ] of names ) text = text . replaceAll ( from , to ) ;
692+ if ( text !== before ) fileContents . set ( key , Buffer . from ( text , "utf-8" ) ) ;
693+ }
694+ }
695+
676696/** {@link buildPublishFileMap }, but non-text files of `onDiskMinBytes` or more stay on disk. */
677697export function stagePublishFiles (
678698 projectDir : string ,
@@ -688,10 +708,20 @@ export function stagePublishFiles(
688708 }
689709
690710 const fileContents = new Map < string , StagedFile > ( ) ;
711+ const diskPathOf = new Map < string , string > ( ) ;
691712 for ( const filePath of filePaths ) {
692- fileContents . set ( filePath , stageFile ( join ( absProjectDir , filePath ) , onDiskMinBytes ) ) ;
713+ const key = archiveKey ( filePath ) ;
714+ const clash = diskPathOf . get ( key ) ;
715+ if ( clash !== undefined ) {
716+ throw new Error (
717+ `"${ clash } " and "${ filePath } " have the same name written in two Unicode forms and would overwrite each other in the archive. Rename one of them.` ,
718+ ) ;
719+ }
720+ diskPathOf . set ( key , filePath ) ;
721+ fileContents . set ( key , stageFile ( join ( absProjectDir , filePath ) , onDiskMinBytes ) ) ;
693722 }
694723
724+ composeNamesInText ( fileContents , filePaths ) ;
695725 localizeExternalAssets ( absProjectDir , fileContents , onDiskMinBytes ) ;
696726 return fileContents ;
697727}
0 commit comments