1
1
import fs from 'node:fs'
2
2
import p from 'node:path'
3
- import process from 'node:process '
3
+ import path from 'node:path '
4
4
import type { BunPlugin } from 'bun'
5
- import ts from 'typescript'
6
-
7
- export interface TsOptions {
8
- rootDir : string
9
- base : string
10
- declaration : boolean
11
- emitDeclarationOnly : boolean
12
- noEmit : boolean
13
- declarationMap ?: boolean
14
- outDir ?: ts . CompilerOptions [ 'outDir' ]
15
- [ index : string ] : any
16
- }
5
+ import { isolatedDeclaration } from 'oxc-transform'
17
6
18
7
export interface DtsOptions {
19
- /**
20
- * The base directory of the source files. If not provided, it
21
- * will use the current working directory of the process.
22
- * @default process.cwd()
23
- */
24
- base ?: string
25
-
26
- /**
27
- * The TypeScript compiler options. If not provided, it will use
28
- * the `tsconfig.json` file in the current working directory.
29
- */
30
- compiler ?: ts . CompilerOptions
31
-
32
- /**
33
- * The path to the `tsconfig.json` file. If not provided, it will
34
- * use the `tsconfig.json` file in the current working directory.
35
- */
36
- tsconfigPath ?: string
37
-
38
- /**
39
- * The current working directory. If not provided, it will
40
- * use the current working directory of the process.
41
- */
42
8
cwd ?: string
43
-
44
- /**
45
- * The root directory of the source files. Please note,
46
- * it is relative to the current working directory.
47
- * @default 'src'
48
- */
49
9
root ?: string
50
-
51
- /**
52
- * The output directory of the declaration files. Please note,
53
- * it is relative to the current working directory.
54
- * @default 'dist'
55
- */
56
- outdir ?: ts . CompilerOptions [ 'outDir' ] // sadly, the bundler uses `outdir` instead of `outDir` and to avoid confusion, we'll use `outdir` here
57
-
58
- /**
59
- * The files to include. If not provided, it will include all files in the
60
- * `tsconfig.json` file, or the Bun build entry points if provided.
61
- */
62
10
include ?: string [ ]
63
11
}
64
12
65
- /**
66
- * Generate declaration files for the TypeScript source files.
67
- * @param entryPoints The entry points of the TypeScript source files.
68
- * @param options The options for generating the declaration files.
69
- */
70
13
export async function generate ( entryPoints : string | string [ ] , options ?: DtsOptions ) : Promise < void > {
71
- const cwd = options ?. cwd ?? process . cwd ( )
72
- const configPath = options ?. tsconfigPath ? p . resolve ( cwd , options . tsconfigPath ) : p . resolve ( cwd , 'tsconfig.json' )
73
- const root = p . resolve ( cwd , ( options ?. root ?? 'src' ) . replace ( / ^ \. \/ / , '' ) )
74
- const outDir = p . resolve ( cwd , options ?. outdir ?? './dist' )
75
-
76
- console . log ( 'TSConfig path:' , configPath )
77
- console . log ( 'Root directory:' , root )
78
- console . log ( 'Output directory:' , outDir )
79
- console . log ( 'Entry points:' , entryPoints )
80
-
81
- try {
82
- const configFile = ts . readConfigFile ( configPath , ts . sys . readFile )
83
- if ( configFile . error ) {
84
- throw new Error ( `Failed to read tsconfig: ${ configFile . error . messageText } ` )
14
+ let errs = ''
15
+ const start = performance . now ( )
16
+ let count = 0
17
+
18
+ for await ( const file of new Bun . Glob ( 'packages/*/src/**/*.ts' ) . scan ( ) ) {
19
+ const ts = fs . readFileSync ( file , 'utf-8' )
20
+ const dts = isolatedDeclaration ( file , ts , { sourcemap : false } )
21
+
22
+ if ( dts . errors . length ) {
23
+ dts . errors . forEach ( ( err ) => {
24
+ // temporary workaround for https://github.com/oxc-project/oxc/issues/5668
25
+ if ( ! err . includes ( 'set value(_: S)' ) ) {
26
+ console . error ( err )
27
+ }
28
+ errs += `${ err } \n`
29
+ } )
85
30
}
86
31
87
- const parsedCommandLine = ts . parseJsonConfigFileContent ( configFile . config , ts . sys , cwd )
32
+ const code = dts . code
88
33
89
- if ( parsedCommandLine . errors . length ) {
90
- throw new Error ( `Failed to parse tsconfig: ${ parsedCommandLine . errors . map ( ( e ) => e . messageText ) . join ( ', ' ) } ` )
91
- }
92
-
93
- const compilerOptions : ts . CompilerOptions = {
94
- ...parsedCommandLine . options ,
95
- ...options ?. compiler ,
96
- rootDir : root ,
97
- outDir,
98
- declaration : true ,
99
- emitDeclarationOnly : true ,
100
- noEmit : false ,
101
- skipLibCheck : true ,
102
- isolatedModules : true ,
103
- }
34
+ write ( path . join ( 'temp' , file . replace ( / \. t s $ / , '.d.ts' ) ) , code )
35
+ count ++
36
+ }
104
37
105
- console . log ( 'Compiler Options:' , JSON . stringify ( compilerOptions , null , 2 ) )
38
+ console . log ( `\n ${ count } isolated dts files generated in ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms.` )
106
39
107
- const host = ts . createCompilerHost ( compilerOptions )
40
+ if ( errs ) {
41
+ write ( path . join ( 'temp' , 'oxc-iso-decl-errors.txt' ) , errs )
42
+ }
108
43
109
- // Ensure entryPoints is an array and resolve to absolute paths
110
- const entryPointsArray = ( Array . isArray ( entryPoints ) ? entryPoints : [ entryPoints ] ) . map ( ( entry ) =>
111
- p . resolve ( cwd , entry ) ,
112
- )
44
+ console . log ( 'bundling dts with rollup-plugin-dts...' )
113
45
114
- // Use only the entry points that are within the root directory
115
- const validEntryPoints = entryPointsArray . filter ( ( entry ) => entry . startsWith ( root ) )
46
+ // bundle with rollup-plugin-dts
47
+ // const rollupConfigs = (await import('../rollup.dts.config.js')).default
116
48
117
- if ( validEntryPoints . length === 0 ) {
118
- throw new Error ( 'No valid entry points found within the specified root directory' )
119
- }
49
+ // await Promise.all(
50
+ // rollupConfigs.map(c =>
51
+ // rollup(c).then(bundle => {
52
+ // return bundle.write(c.output).then(() => {
53
+ // console.log(picocolors.gray('built: ') + picocolors.blue(c.output.file))
54
+ // })
55
+ // }),
56
+ // ),
57
+ // )
120
58
121
- const program = ts . createProgram ( validEntryPoints , compilerOptions , host )
59
+ console . log ( `bundled dts generated in ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms.` )
122
60
123
- const emitResult = program . emit ( undefined , ( fileName , data ) => {
124
- if ( fileName . endsWith ( '.d.ts' ) || fileName . endsWith ( '.d.ts.map' ) ) {
125
- const outputPath = p . join ( outDir , p . relative ( root , fileName ) )
126
- const dir = p . dirname ( outputPath )
127
- if ( ! fs . existsSync ( dir ) ) {
128
- fs . mkdirSync ( dir , { recursive : true } )
129
- }
130
- fs . writeFileSync ( outputPath , data )
131
- console . log ( 'Emitted:' , outputPath )
132
- }
133
- } )
134
-
135
- const allDiagnostics = ts . getPreEmitDiagnostics ( program ) . concat ( emitResult . diagnostics )
136
-
137
- if ( allDiagnostics . length ) {
138
- const formatHost : ts . FormatDiagnosticsHost = {
139
- getCanonicalFileName : ( path ) => path ,
140
- getCurrentDirectory : ts . sys . getCurrentDirectory ,
141
- getNewLine : ( ) => ts . sys . newLine ,
142
- }
143
- const message = ts . formatDiagnosticsWithColorAndContext ( allDiagnostics , formatHost )
144
- console . error ( message )
145
- }
61
+ // } catch (error) {
62
+ // console.error('Error generating types:', error)
63
+ // throw error
64
+ // }
65
+ }
146
66
147
- if ( emitResult . emitSkipped ) {
148
- throw new Error ( 'TypeScript compilation failed' )
149
- }
150
- } catch ( error ) {
151
- console . error ( 'Error generating types:' , error )
152
- throw error
153
- }
67
+ function write ( file : string , content : string ) {
68
+ const dir = p . dirname ( file )
69
+ if ( ! fs . existsSync ( dir ) ) fs . mkdirSync ( dir , { recursive : true } )
70
+ fs . writeFileSync ( file , content )
154
71
}
155
72
156
- /**
157
- * A Bun plugin to generate declaration files for the TypeScript source files.
158
- * @param options The options for generating the declaration files.
159
- */
160
73
export function dts ( options ?: DtsOptions ) : BunPlugin {
161
74
return {
162
75
name : 'bun-plugin-dts-auto' ,
@@ -165,17 +78,7 @@ export function dts(options?: DtsOptions): BunPlugin {
165
78
const entrypoints = [ ...build . config . entrypoints ] . sort ( )
166
79
const root = options ?. root ?? build . config . root ?? 'src'
167
80
168
- await generate ( entrypoints , {
169
- root,
170
- include : entrypoints , // Use only the entrypoints from build.ts
171
- cwd : options ?. cwd || process . cwd ( ) ,
172
- tsconfigPath : options ?. tsconfigPath ,
173
- outdir : options ?. outdir || build . config . outdir ,
174
- compiler : {
175
- ...options ?. compiler ,
176
- paths : undefined , // Remove the paths option to avoid generating extra dts files
177
- } ,
178
- } )
81
+ // generate dts here
179
82
} ,
180
83
}
181
84
}
0 commit comments