@@ -58,6 +58,7 @@ const extMap = { cjs: '.js', esm: '.mjs' }
58
58
async function buildFiles ( { files, module, outDir } ) {
59
59
console . log ( `building for ${ module } ` )
60
60
await promisify ( exec ) ( `npx tsc --outDir ${ outDir } ` , { stdio : 'inherit' } )
61
+ const ext = extMap [ module ]
61
62
62
63
const opt = options ( module )
63
64
for ( const file of files ) {
@@ -69,7 +70,7 @@ async function buildFiles({ files, module, outDir }) {
69
70
const outDirPath = path . dirname ( outFilePath )
70
71
71
72
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 )
73
74
74
75
if ( file . path . endsWith ( '.d.ts' ) ) {
75
76
await fsp . copyFile ( file . path , outFilePath )
@@ -88,19 +89,8 @@ async function buildFiles({ files, module, outDir }) {
88
89
throw e
89
90
}
90
91
}
91
- }
92
-
93
- async function main ( ) {
94
- await fsp . rm ( 'dist' , { recursive : true , force : true } )
95
-
96
- const entries = fsWalk . walkSync ( 'src/' )
97
92
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 ) ) {
104
94
if ( file . dirent . isDirectory ( ) ) {
105
95
continue
106
96
}
@@ -114,14 +104,28 @@ async function main() {
114
104
const mts = babel . transformSync ( fileContent , {
115
105
filename : file . path ,
116
106
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 } } ] ] ,
118
108
} )
119
109
120
110
await fsp . unlink ( file . path )
121
111
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
+ }
123
116
await fsp . writeFile ( outFilePath , mts . code )
124
117
}
125
118
}
126
119
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
+
127
131
await main ( )
0 commit comments