@@ -58,6 +58,7 @@ const extMap = { cjs: '.js', esm: '.mjs' }
5858async function buildFiles ( { files, module, outDir } ) {
5959 console . log ( `building for ${ module } ` )
6060 await promisify ( exec ) ( `npx tsc --outDir ${ outDir } ` , { stdio : 'inherit' } )
61+ const ext = extMap [ module ]
6162
6263 const opt = options ( module )
6364 for ( const file of files ) {
@@ -69,7 +70,7 @@ async function buildFiles({ files, module, outDir }) {
6970 const outDirPath = path . dirname ( outFilePath )
7071
7172 await fsp . mkdir ( outDirPath , { recursive : true } )
72- const distCodePath = outFilePath . replace ( / \. [ t j ] s $ / g, extMap [ module ] )
73+ const distCodePath = outFilePath . replace ( / \. [ t j ] s $ / g, ext )
7374
7475 if ( file . path . endsWith ( '.d.ts' ) ) {
7576 await fsp . copyFile ( file . path , outFilePath )
@@ -88,19 +89,8 @@ async function buildFiles({ files, module, outDir }) {
8889 throw e
8990 }
9091 }
91- }
92-
93- async function main ( ) {
94- await fsp . rm ( 'dist' , { recursive : true , force : true } )
95-
96- const entries = fsWalk . walkSync ( 'src/' )
9792
98- await Promise . all ( [
99- buildFiles ( { files : entries , module : 'cjs' , outDir : './dist/main/' } ) ,
100- buildFiles ( { files : entries , module : 'esm' , outDir : './dist/esm/' } ) ,
101- ] )
102-
103- for ( const file of fsWalk . walkSync ( 'dist/esm/' ) ) {
93+ for ( const file of fsWalk . walkSync ( outDir ) ) {
10494 if ( file . dirent . isDirectory ( ) ) {
10595 continue
10696 }
@@ -114,14 +104,28 @@ async function main() {
114104 const mts = babel . transformSync ( fileContent , {
115105 filename : file . path ,
116106 sourceMaps : true ,
117- plugins : [ [ '@babel/plugin-syntax-typescript' ] , [ 'replace-import-extension' , { extMapping : { '.ts' : '.mjs' } } ] ] ,
107+ plugins : [ [ '@babel/plugin-syntax-typescript' ] , [ 'replace-import-extension' , { extMapping : { '.ts' : ext } } ] ] ,
118108 } )
119109
120110 await fsp . unlink ( file . path )
121111
122- const outFilePath = file . path . slice ( 0 , file . path . length - '.d.ts' . length ) + '.d.mts'
112+ let outFilePath = file . path . slice ( 0 , file . path . length - '.d.ts' . length ) + '.d.ts'
113+ if ( module === 'esm' ) {
114+ outFilePath = file . path . slice ( 0 , file . path . length - '.d.ts' . length ) + '.d.mts'
115+ }
123116 await fsp . writeFile ( outFilePath , mts . code )
124117 }
125118}
126119
120+ async function main ( ) {
121+ await fsp . rm ( 'dist' , { recursive : true , force : true } )
122+
123+ const entries = fsWalk . walkSync ( 'src/' )
124+
125+ await Promise . all ( [
126+ buildFiles ( { files : entries , module : 'cjs' , outDir : './dist/main/' } ) ,
127+ buildFiles ( { files : entries , module : 'esm' , outDir : './dist/esm/' } ) ,
128+ ] )
129+ }
130+
127131await main ( )
0 commit comments