Skip to content

Commit 5c3dc60

Browse files
committed
fix: add type definition generating script with oxc-transform
1 parent 13edbea commit 5c3dc60

File tree

4 files changed

+221
-5
lines changed

4 files changed

+221
-5
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"eslint-config-prettier": "^9.1.0",
110110
"eslint-plugin-vue": "^9.28.0",
111111
"execa": "^9.3.0",
112+
"fast-glob": "^3.3.3",
112113
"fixpack": "^4.0.0",
113114
"get-port-please": "^3.1.2",
114115
"globals": "^15.9.0",
@@ -121,6 +122,7 @@
121122
"mitata": "^1.0.20",
122123
"npm-run-all2": "^7.0.0",
123124
"opener": "^1.5.2",
125+
"oxc-transform": "^0.44.0",
124126
"pathe": "^1.1.2",
125127
"picocolors": "^1.0.0",
126128
"pkg-pr-new": "^0.0.37",

pnpm-lock.yaml

Lines changed: 125 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/build-rolldown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async function main() {
112112
}
113113

114114
if (buildTypes) {
115-
// TODO: build types
115+
await import('./build-types')
116116
}
117117
}
118118

scripts/build-types.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { globSync } from 'fast-glob'
2+
import {
3+
existsSync,
4+
mkdirSync,
5+
readFileSync,
6+
rmSync,
7+
writeFileSync
8+
} from 'node:fs'
9+
import path from 'node:path'
10+
import { fileURLToPath } from 'node:url'
11+
import { isolatedDeclaration } from 'oxc-transform'
12+
13+
const __dirname = fileURLToPath(new URL('.', import.meta.url))
14+
15+
if (existsSync(path.resolve(__dirname, '../temp/packages'))) {
16+
rmSync(path.resolve(__dirname, '../temp/packages'), { recursive: true })
17+
}
18+
19+
let errs = ''
20+
let start = performance.now()
21+
let count = 0
22+
23+
const IGNORES = [
24+
'format-explorer',
25+
'size-check-core',
26+
'size-check-vue-i18n',
27+
'size-check-vue-i18n'
28+
]
29+
30+
for (const file of globSync(
31+
path.resolve(__dirname, '../packages/*/src/**/*.ts')
32+
)) {
33+
for (const ignore of IGNORES) {
34+
if (file.includes(ignore)) {
35+
continue
36+
}
37+
}
38+
39+
const ts = readFileSync(file, 'utf-8')
40+
const dts = isolatedDeclaration(file, ts, {
41+
sourcemap: false,
42+
stripInternal: true
43+
})
44+
if (dts.errors.length) {
45+
dts.errors.forEach(err => {
46+
// temporary workaround for https://github.com/oxc-project/oxc/issues/5668
47+
if (!err.message.includes('set value(_: S)')) {
48+
console.error(err)
49+
}
50+
errs += err.message + '\n'
51+
})
52+
}
53+
54+
write(path.join('temp', file.replace(/\.ts$/, '.d.ts')), dts.code)
55+
count++
56+
}
57+
58+
console.log(
59+
`\n${count} isolated dts files generated in ${(performance.now() - start).toFixed(2)}ms.`
60+
)
61+
62+
if (errs) {
63+
write(path.join('temp', 'oxc-iso-decl-errors.txt'), errs)
64+
}
65+
66+
console.log('bundling dts with rollup-plugin-dts...')
67+
68+
// bundle with rollup-plugin-dts
69+
// const rollupConfigs = (await import('../rollup.dts.config.js')).default
70+
71+
start = performance.now()
72+
73+
// await Promise.all(
74+
// rollupConfigs.map(c =>
75+
// rollup(c).then(bundle => {
76+
// return bundle.write(c.output).then(() => {
77+
// console.log(pc.gray('built: ') + pc.blue(c.output.file))
78+
// })
79+
// }),
80+
// ),
81+
// )
82+
83+
console.log(
84+
`bundled dts generated in ${(performance.now() - start).toFixed(2)}ms.`
85+
)
86+
87+
function write(file: string, content: string) {
88+
const dir = path.dirname(file)
89+
if (!existsSync(dir)) {
90+
mkdirSync(dir, { recursive: true })
91+
}
92+
writeFileSync(file, content)
93+
}

0 commit comments

Comments
 (0)