- [x] Normal
.ts
,.tsx
files - [x]
.vue
files, using@vue/compiler-sfc
- [x]
.svelte
files, usingsvelte2tsx
- [x]
.astro
files, using@astrojs/compiler
- [x]
.vine.ts
files, using@vue-vine/compiler
All special files can be disabled by options, just set like vue: false
, svelte: false
, etc.
I will keep the configuration as simple as possible, just like tsup ⚡️
pnpm install undts
Create a file in your project root directory:
import { build } from 'undts'
build({
entry: [
// Add your entry files here
'./src/index.ts'
],
// ... more options, you can see jsdoc in the source code
})
Then run this file with node
/tsx
/ts-node
, for example:
pnpx tsx your-file.ts
It will generate d.ts file in dist
directory by default. You can change the output directory by outDir
option:
build({
entry: ['./src/index.ts'],
outDir: './types', // default is './dist'
})
Thanks to the powerful unplugin
, undts can be used in combination with vite
/rollup
/esbuild
with zero configuration, it will automatically using the vite
/rollup
/esbuild
configuration file's entry
/input
option as the entry
option of undts
.
Using with Vite Lib mode(Recommended)
// vite.config.ts
import undts from 'undts/vite'
import { defineConfig } from 'vite'
export default defineConfig({
build: {
lib: {
// undts will automatically use this entry, you don't need to set it again in plugin options
entry: 'src/index.ts',
},
},
plugins: [
undts()
],
})
Using with Rollup + SWC
// rollup.config.mjs
import swc from '@rollup/plugin-swc'
import { defineConfig } from 'rollup'
import undts from 'undts/rollup'
export default defineConfig({
input: 'src/index.ts',
output: {
dir: 'dist',
format: 'es',
},
plugins: [
swc(),
// It will automatically use input option as the entry option of undts
undts(),
],
})
Using with tsup(it powered by esbuild)
// tsup.config.ts
import { defineConfig } from 'tsup'
import undts from 'undts/esbuild'
export default defineConfig({
entry: ['src/index.ts'],
// Disable tsup's default dts generation, use undts instead
dts: false,
sourcemap: true,
esbuildPlugins: [
// It will automatically use entry option as the entry option of undts
undts(),
],
})
undts
's plugin system is extended from rollup
and added some new hooks, you can use it to customize the behavior of undts
.
-
dtsConfig
: This hook is called when starting callbuild
function, you can modify theundts
's configuration by this hook. -
transformInclude
: This hook is called whenundts
encounters a file that needs to be included in the cache bundle, you can use this hook to modify the file content before it is included in the cache bundle. -
transformCallExpression
: This hook is called whenundts
encounters a TypeScriptCallExpression
node, the main purpose of this hook is to convert theimport()
function. -
transformImportDeclaration/transformExportDeclaration
: These hooks are called whenundts
encounters a TypeScriptImportDeclaration
/ExportDeclaration
node, the main purpose of these hooks is to convert theimport
/export
statement. -
transformSourceFile
: This hook is called whenundts
encounters a TypeScriptSourceFile
instance, this hook will be called after all of the above hooks have been called. -
writeCacheBundle
: This hook is called whenundts
writes the cache bundle to the disk, you can use this hook to modify the cache bundle before it is written to the disk. It enabledbundled
option (default is enabled), the real cache bundle path will be delivered torollup
'sinput
option and transform byrollup-plugin-dts
plugin.
Naily Zero & MIT