@@ -43,6 +43,7 @@ export interface DtsCompileOptions {
43
43
alias : TaskConfig [ 'alias' ] ;
44
44
rootDir : string ;
45
45
outputDir : string ;
46
+ usingOxc : boolean ;
46
47
}
47
48
48
49
function formatAliasToTSPathsConfig ( alias : TaskConfig [ 'alias' ] ) {
@@ -116,7 +117,7 @@ async function getProjectTSConfig(rootDir: string): Promise<ts.ParsedCommandLine
116
117
} ;
117
118
}
118
119
119
- export async function dtsCompile ( { files, rootDir, outputDir, alias } : DtsCompileOptions ) : Promise < DtsInputFile [ ] > {
120
+ export async function dtsCompile ( { files, rootDir, outputDir, alias, usingOxc } : DtsCompileOptions ) : Promise < DtsInputFile [ ] > {
120
121
if ( ! files . length ) {
121
122
return [ ] ;
122
123
}
@@ -132,18 +133,53 @@ export async function dtsCompile({ files, rootDir, outputDir, alias }: DtsCompil
132
133
dtsPath : normalizePath ( dtsPath ) ,
133
134
} ) ) ;
134
135
136
+ const compileFunction = usingOxc ? compileFromOxc : compileFromTsc ;
137
+ const dtsFiles = await compileFunction ( _files , tsConfig )
138
+
139
+ if ( ! Object . keys ( alias ) . length ) {
140
+ // no alias config
141
+ return _files . map ( ( file ) => ( {
142
+ ...file ,
143
+ dtsContent : dtsFiles [ file . dtsPath ] ,
144
+ } ) ) ;
145
+ }
146
+
147
+ // We use tsc-alias to resolve d.ts alias.
148
+ // Reason: https://github.com/microsoft/TypeScript/issues/30952#issuecomment-1114225407
149
+ const tsConfigLocalPath = path . join ( rootDir , 'node_modules/.cache/ice-pkg/tsconfig.json' ) ;
150
+ await fse . ensureFile ( tsConfigLocalPath ) ;
151
+ await fse . writeJSON ( tsConfigLocalPath , {
152
+ ...tsConfig ,
153
+ compilerOptions : tsConfig . options ,
154
+ } , { spaces : 2 } ) ;
155
+
156
+ const runFile = await prepareSingleFileReplaceTscAliasPaths ( {
157
+ configFile : tsConfigLocalPath ,
158
+ outDir : outputDir ,
159
+ } ) ;
160
+
161
+ const result = _files . map ( ( file ) => ( {
162
+ ...file ,
163
+ dtsContent : dtsFiles [ file . dtsPath ] ? runFile ( { fileContents : dtsFiles [ file . dtsPath ] , filePath : file . dtsPath } ) : '' ,
164
+ } ) ) ;
165
+
166
+ return result ;
167
+ }
168
+
169
+ async function compileFromTsc ( files : DtsInputFile [ ] , tsConfig : ts . ParsedCommandLine ) : Promise < Record < string , string > > {
135
170
// In order to only include the update files instead of all the files in the watch mode.
136
171
function getProgramRootNames ( originalFilenames : string [ ] ) {
137
172
// Should include all the resolved .d.ts file to avoid dts generate error:
138
173
// TS4025: Exported variable '<name>' has or is using private name '<name>'.
139
174
const dtsFilenames = originalFilenames . filter ( ( filename ) => filename . endsWith ( '.d.ts' ) ) ;
140
- const needCompileFileNames = _files . map ( ( { filePath } ) => filePath ) ;
175
+ const needCompileFileNames = files . map ( ( { filePath } ) => filePath ) ;
141
176
return [ ...needCompileFileNames , ...dtsFilenames ] ;
142
177
}
143
178
144
- const dtsFiles = { } ;
145
179
const host = ts . createCompilerHost ( tsConfig . options ) ;
146
180
181
+ const dtsFiles : Record < string , string > = { } ;
182
+
147
183
host . writeFile = ( fileName , contents ) => {
148
184
dtsFiles [ fileName ] = contents ;
149
185
} ;
@@ -171,32 +207,23 @@ export async function dtsCompile({ files, rootDir, outputDir, alias }: DtsCompil
171
207
} ) ;
172
208
}
173
209
174
- if ( ! Object . keys ( alias ) . length ) {
175
- // no alias config
176
- return _files . map ( ( file ) => ( {
177
- ...file ,
178
- dtsContent : dtsFiles [ file . dtsPath ] ,
179
- } ) ) ;
180
- }
181
-
182
- // We use tsc-alias to resolve d.ts alias.
183
- // Reason: https://github.com/microsoft/TypeScript/issues/30952#issuecomment-1114225407
184
- const tsConfigLocalPath = path . join ( rootDir , 'node_modules/.cache/ice-pkg/tsconfig.json' ) ;
185
- await fse . ensureFile ( tsConfigLocalPath ) ;
186
- await fse . writeJSON ( tsConfigLocalPath , {
187
- ...tsConfig ,
188
- compilerOptions : tsConfig . options ,
189
- } , { spaces : 2 } ) ;
210
+ return dtsFiles
211
+ }
190
212
191
- const runFile = await prepareSingleFileReplaceTscAliasPaths ( {
192
- configFile : tsConfigLocalPath ,
193
- outDir : outputDir ,
194
- } ) ;
213
+ async function compileFromOxc ( absFiles : DtsInputFile [ ] , tsConfig : ts . ParsedCommandLine ) : Promise < Record < string , string > > {
214
+ if ( ! tsConfig . options . isolatedDeclarations ) {
215
+ consola . warn ( `Enable isolatedDeclarations in tsconfig.json for correct .d.ts file generation` )
216
+ }
217
+ const oxc = await import ( 'oxc-transform' ) ;
218
+ const dtsFiles : Record < string , string > = { } ;
219
+ for ( const file of absFiles ) {
220
+ const fileContent = fse . readFileSync ( file . filePath , 'utf-8' ) ;
221
+ const { code } = oxc . isolatedDeclaration ( file . filePath , fileContent , {
222
+ sourcemap : false ,
223
+ } ) ;
195
224
196
- const result = _files . map ( ( file ) => ( {
197
- ...file ,
198
- dtsContent : dtsFiles [ file . dtsPath ] ? runFile ( { fileContents : dtsFiles [ file . dtsPath ] , filePath : file . dtsPath } ) : '' ,
199
- } ) ) ;
225
+ dtsFiles [ file . dtsPath ] = code ;
226
+ }
200
227
201
- return result ;
228
+ return dtsFiles
202
229
}
0 commit comments