@@ -17,13 +17,29 @@ import { resolve } from "@std/path";
1717
1818const configPath = resolve ( Deno . cwd ( ) , "./deno.json" ) ;
1919
20- async function stripExtensionsInDts ( path : string ) : Promise < void > {
21- if ( ! path . endsWith ( ".d.ts" ) ) return ;
22- const src = await Deno . readTextFile ( path ) ;
23- // Replace .tsx and .ts extensions in relative import/export paths so that
24- // TypeScript resolves to companion .d.ts files rather than source files.
25- const out = src . replace ( / ( f r o m \s + [ " ' ] ) ( \. \. ? \/ [ ^ " ' ] * ) \. ( t s x ? ) (? = [ " ' ] ) / g, "$1$2" ) ;
26- if ( out !== src ) await Deno . writeTextFile ( path , out ) ;
20+ async function stripExtensionsInDts ( filePath : string ) : Promise < void > {
21+ if ( ! filePath . endsWith ( ".d.ts" ) ) return ;
22+ let src = await Deno . readTextFile ( filePath ) ;
23+
24+ // Rewrite paths that escape dist back into src/ (e.g. ../../src/state-builder/index.ts)
25+ // to their companion .d.ts locations inside dist/ (e.g. ../state-builder/index).
26+ // This happens because tsc resolves @molstar /state-builder path aliases to the
27+ // absolute source path and encodes them as relative paths in the emitted .d.ts.
28+ src = src . replace (
29+ / ( f r o m \s + [ " ' ] ) ( (?: \. \. \/ ) + ) s r c \/ s t a t e - b u i l d e r \/ ( [ ^ " ' ] + ) (? = [ " ' ] ) / g,
30+ ( _match , from , upSegments , subPath ) => {
31+ const depth = ( upSegments . match ( / \. \. \/ / g) ?? [ ] ) . length ;
32+ const newUp = "../" . repeat ( depth - 1 ) ;
33+ const cleanPath = subPath . replace ( / \. t s x ? $ / , "" ) ;
34+ return `${ from } ${ newUp } state-builder/${ cleanPath } ` ;
35+ } ,
36+ ) ;
37+
38+ // Strip any remaining .ts/.tsx extensions from relative import/export paths so
39+ // that TypeScript resolves to companion .d.ts files rather than source files.
40+ src = src . replace ( / ( f r o m \s + [ " ' ] ) ( \. \. ? \/ [ ^ " ' ] * ) \. ( t s x ? ) (? = [ " ' ] ) / g, "$1$2" ) ;
41+
42+ await Deno . writeTextFile ( filePath , src ) ;
2743}
2844
2945async function walkAndStrip ( dir : string ) : Promise < void > {
0 commit comments