unplugin-isolated-decl β
β‘οΈ A blazing-fast tool for generating isolated declarations.
Features β
- π Fast: Generates
.d.ts
files significantly faster thantsc
. - π¨ Transformer: Support Oxc, SWC, and TypeScript transformer.
- π¦ Zero Config: No configuration required, works out of the box.
- β¨ Bundler Support: Works with Vite, Rollup, esbuild and Farm. (PR of Webpack/Rspack support is welcome)
Installation β
bash
# npm
npm i -D unplugin-isolated-decl
# jsr
npx jsr add -D @unplugin/isolated-decl
Usage β
Vite
ts
// vite.config.ts
import UnpluginIsolatedDecl from 'unplugin-isolated-decl/vite'
export default defineConfig({
plugins: [UnpluginIsolatedDecl()],
})
Rollup
ts
// rollup.config.js
import UnpluginIsolatedDecl from 'unplugin-isolated-decl/rollup'
export default {
plugins: [UnpluginIsolatedDecl()],
}
Rolldown
ts
// rolldown.config.js
import UnpluginIsolatedDecl from 'unplugin-isolated-decl/rolldown'
export default {
plugins: [UnpluginIsolatedDecl()],
}
esbuild
ts
// esbuild.config.js
import { build } from 'esbuild'
build({
plugins: [require('unplugin-isolated-decl/esbuild')()],
})
Farm
ts
// farm.config.ts
import UnpluginIsolatedDecl from 'unplugin-isolated-decl/farm'
export default defineConfig({
plugins: [UnpluginIsolatedDecl()],
})
Options β
ts
export interface Options {
include?: FilterPattern
exclude?: FilterPattern
enforce?: 'pre' | 'post' | undefined
/**
* You need to install one of the supported transformers yourself.
* oxc: @oxc/transformer
* swc: @swc/core
* typescript: typescript
*
* @default typescript
*/
transformer?: 'oxc' | 'swc' | 'typescript'
/**
* Whether to generate declaration source maps.
*
* Supported by `typescript` and `oxc` transformer only.
*
* @link https://www.typescriptlang.org/tsconfig/#declarationMap
*/
sourceMap?: boolean
/** Only for typescript transformer */
transformOptions?: TranspileOptions
ignoreErrors?: boolean
/** An extra directory layer for output files. */
extraOutdir?: string
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
autoAddExts?: boolean
rewriteImports?: (
id: string,
importer: string,
) => string | void | null | undefined
}
rewriteImports
β
Rewrite imports in .d.ts
files. (esbuild support is not available)
Here is an example of rewriting imports on Rollup:
js
// rollup.config.js
import alias from '@rollup/plugin-alias'
export default {
// ...
plugins: [
alias({ entries: [{ find: '~', replacement: '.' }] }),
UnpluginIsolatedDecl({
rewriteImports(id, importer) {
if (id[0] === '~') return `.${id.slice(1)}`
},
}),
// ...
],
}
autoAddExts
β
Automatically add .js
extension to resolve in Node 16+ ESM mode. (esbuild support is not available)
ts
// index.d.ts
import {} from './foo'
With autoAddExts
, it will be transformed to:
ts
// index.d.ts
import {} from './foo.js'
patchCjsDefaultExport
β
Patch export default
in .d.cts
to export =
Note
For the exhaustive set of options check options
Sponsors β
License β
MIT License Β© 2024-PRESENT δΈε²ζΊε