Skip to content

Commit b7c4840

Browse files
committed
chore: wip
1 parent cd10f01 commit b7c4840

File tree

3 files changed

+51
-146
lines changed

3 files changed

+51
-146
lines changed

bun.lockb

7.27 KB
Binary file not shown.

storage/framework/core/actions/dts.ts

Lines changed: 49 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,75 @@
11
import fs from 'node:fs'
22
import p from 'node:path'
3-
import process from 'node:process'
3+
import path from 'node:path'
44
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'
176

187
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-
*/
428
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-
*/
499
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-
*/
6210
include?: string[]
6311
}
6412

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-
*/
7013
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+
})
8530
}
8631

87-
const parsedCommandLine = ts.parseJsonConfigFileContent(configFile.config, ts.sys, cwd)
32+
const code = dts.code
8833

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(/\.ts$/, '.d.ts')), code)
35+
count++
36+
}
10437

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.`)
10639

107-
const host = ts.createCompilerHost(compilerOptions)
40+
if (errs) {
41+
write(path.join('temp', 'oxc-iso-decl-errors.txt'), errs)
42+
}
10843

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...')
11345

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
11648

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+
// )
12058

121-
const program = ts.createProgram(validEntryPoints, compilerOptions, host)
59+
console.log(`bundled dts generated in ${(performance.now() - start).toFixed(2)}ms.`)
12260

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+
}
14666

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)
15471
}
15572

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-
*/
16073
export function dts(options?: DtsOptions): BunPlugin {
16174
return {
16275
name: 'bun-plugin-dts-auto',
@@ -165,17 +78,7 @@ export function dts(options?: DtsOptions): BunPlugin {
16578
const entrypoints = [...build.config.entrypoints].sort()
16679
const root = options?.root ?? build.config.root ?? 'src'
16780

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
17982
},
18083
}
18184
}

storage/framework/core/actions/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
"@stacksjs/utils": "workspace:*",
6363
"@stacksjs/validation": "workspace:*",
6464
"markdown-it": "^14.1.0",
65+
"oxc-parser": "^0.30.5",
66+
"oxc-transform": "^0.30.5",
6567
"vue-component-meta": "^2.1.6"
6668
},
6769
"devDependencies": {

0 commit comments

Comments
 (0)