@@ -4,25 +4,39 @@ const mode = process.argv[2]; // --cjs or --mjs
44const files = process . argv . slice ( 3 ) ;
55
66const ext = mode === "--cjs" ? "cjs" : "mjs" ;
7+ const dtsExt = mode === "--cjs" ? "d.cts" : "d.mts" ;
78
89console . info ( `Fixing ${ mode } files with extension ${ ext } ` ) ;
910
1011for ( const file of files ) {
11- const fileMjs = file . replace ( / \. j s $ / , `.${ ext } ` ) ;
12- console . info ( `Processing ${ file } => ${ fileMjs } ` ) ;
13- // .js => .mjs
14- const content = fs . readFileSync ( file ) . toString ( "utf-8" ) ;
15- const newContent = content
16- . replace ( / \b f r o m " ( \. \. ? \/ [ ^ " ] + ) \. j s " ; / g, `from "$1.${ ext } ";` )
17- . replace ( / \b i m p o r t " ( \. \. ? \/ [ ^ " ] + ) \. j s " ; / g, `import "$1.${ ext } ";` )
18- . replace ( / \b r e q u i r e \( " ( \. \. ? \/ [ ^ " ] + ) \. j s " \) / g, `require("$1.${ ext } ");` )
19- . replace ( / \/ \/ # s o u r c e M a p p i n g U R L = ( .+ ) \. j s \. m a p $ / , `//# sourceMappingURL=$1.${ ext } .map` ) ;
20- fs . writeFileSync ( fileMjs , newContent ) ;
21- fs . unlinkSync ( file ) ;
12+ if ( file . endsWith ( ".d.ts" ) ) {
13+ // Handle declaration files: .d.ts => .d.mts or .d.cts
14+ const newFile = file . replace ( / \. d \. t s $ / , `.${ dtsExt } ` ) ;
15+ console . info ( `Processing ${ file } => ${ newFile } ` ) ;
16+ const content = fs . readFileSync ( file ) . toString ( "utf-8" ) ;
17+ // Fix import paths: .ts => .mjs or .cjs
18+ const newContent = content
19+ . replace ( / \b f r o m " ( \. \. ? \/ [ ^ " ] + ) \. t s " ; / g, `from "$1.${ ext } ";` )
20+ . replace ( / \b i m p o r t " ( \. \. ? \/ [ ^ " ] + ) \. t s " ; / g, `import "$1.${ ext } ";` ) ;
21+ fs . writeFileSync ( newFile , newContent ) ;
22+ fs . unlinkSync ( file ) ;
23+ } else if ( file . endsWith ( ".js" ) ) {
24+ // Handle JS files: .js => .mjs or .cjs
25+ const fileMjs = file . replace ( / \. j s $ / , `.${ ext } ` ) ;
26+ console . info ( `Processing ${ file } => ${ fileMjs } ` ) ;
27+ const content = fs . readFileSync ( file ) . toString ( "utf-8" ) ;
28+ const newContent = content
29+ . replace ( / \b f r o m " ( \. \. ? \/ [ ^ " ] + ) \. j s " ; / g, `from "$1.${ ext } ";` )
30+ . replace ( / \b i m p o r t " ( \. \. ? \/ [ ^ " ] + ) \. j s " ; / g, `import "$1.${ ext } ";` )
31+ . replace ( / \b r e q u i r e \( " ( \. \. ? \/ [ ^ " ] + ) \. j s " \) / g, `require("$1.${ ext } ");` )
32+ . replace ( / \/ \/ # s o u r c e M a p p i n g U R L = ( .+ ) \. j s \. m a p $ / , `//# sourceMappingURL=$1.${ ext } .map` ) ;
33+ fs . writeFileSync ( fileMjs , newContent ) ;
34+ fs . unlinkSync ( file ) ;
2235
23- // .js.map => .mjs.map
24- const mapping = JSON . parse ( fs . readFileSync ( `${ file } .map` ) . toString ( "utf-8" ) ) ;
25- mapping . file = mapping . file . replace ( / \. j s $ / , ext ) ;
26- fs . writeFileSync ( `${ fileMjs } .map` , JSON . stringify ( mapping ) ) ;
27- fs . unlinkSync ( `${ file } .map` ) ;
36+ // .js.map => .mjs.map
37+ const mapping = JSON . parse ( fs . readFileSync ( `${ file } .map` ) . toString ( "utf-8" ) ) ;
38+ mapping . file = mapping . file . replace ( / \. j s $ / , ext ) ;
39+ fs . writeFileSync ( `${ fileMjs } .map` , JSON . stringify ( mapping ) ) ;
40+ fs . unlinkSync ( `${ file } .map` ) ;
41+ }
2842}
0 commit comments