Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/rolldown-plugin-dts. A Rolldown plugin to generate and bundle dts files.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151import path from 'node:path'import process from 'node:process'import Debug from 'debug'import { getTsconfig, parseTsconfig, type TsConfigJson } from 'get-tsconfig'import { createDtsInputPlugin } from './dts-input'import { createFakeJsPlugin } from './fake-js'import { createGeneratePlugin } from './generate'import { createDtsResolvePlugin } from './resolve'import type { IsolatedDeclarationsOptions } from 'oxc-transform'import type { Plugin } from 'rolldown'
const debug = Debug('rolldown-plugin-dts:options')
export interface Options { /** * The directory where the the plugin will look for the `tsconfig.json` file. */ cwd?: string
/** * When entries are `.d.ts` files (instead of `.ts` files), this option should be set to `true`. * * If enabled, the plugin will skip generating a `.d.ts` file for the entry point. */ dtsInput?: boolean
/** * When `true`, the plugin will only emit `.d.ts` files and remove all other chunks. * * This feature is particularly beneficial when you need to generate `d.ts` files for the CommonJS format as part of a separate build process. */ emitDtsOnly?: boolean
/** * The path to the `tsconfig.json` file. * * When set to `false`, the plugin will ignore any `tsconfig.json` file. * However, `compilerOptions` can still be specified directly in the options. * * @default `tsconfig.json` */ tsconfig?: string | boolean
/** * The `compilerOptions` for the TypeScript compiler. * * @see https://www.typescriptlang.org/docs/handbook/compiler-options.html */ compilerOptions?: TsConfigJson.CompilerOptions
/** * When `true`, the plugin will generate `.d.ts` files using `oxc-transform`, * which is blazingly faster than `typescript` compiler. * * This option is enabled when `isolatedDeclarations` in `compilerOptions` is set to `true`. */ isolatedDeclarations?: | boolean | Omit<IsolatedDeclarationsOptions, 'sourcemap'>
/** * When `true`, the plugin will generate declaration maps for `.d.ts` files. */ sourcemap?: boolean
/** Resolve external types used in dts files from `node_modules` */ resolve?: boolean | (string | RegExp)[]}
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
export type OptionsResolved = Overwrite< Required<Options>, { tsconfig: string | undefined isolatedDeclarations: IsolatedDeclarationsOptions | false }>
export function dts(options: Options = {}): Plugin[] { debug('resolving dts options') const resolved = resolveOptions(options) debug('resolved dts options %o', resolved)
const plugins: Plugin[] = [] if (options.dtsInput) { plugins.push(createDtsInputPlugin()) } else { plugins.push(createGeneratePlugin(resolved)) } plugins.push(createDtsResolvePlugin(resolved), createFakeJsPlugin(resolved)) return plugins}
export { createFakeJsPlugin, createGeneratePlugin }
export function resolveOptions({ cwd = process.cwd(), tsconfig, compilerOptions = {}, isolatedDeclarations, sourcemap, dtsInput = false, emitDtsOnly = false, resolve = false,}: Options): OptionsResolved { if (tsconfig === true || tsconfig == null) { const { config, path } = getTsconfig(cwd) || {} tsconfig = path compilerOptions = { ...config?.compilerOptions, ...compilerOptions, } } else if (typeof tsconfig === 'string') { tsconfig = path.resolve(cwd || process.cwd(), tsconfig) const config = parseTsconfig(tsconfig) compilerOptions = { ...config.compilerOptions, ...compilerOptions, } } else { tsconfig = undefined }
sourcemap ??= !!compilerOptions.declarationMap compilerOptions.declarationMap = sourcemap
if (isolatedDeclarations == null) { isolatedDeclarations = !!compilerOptions?.isolatedDeclarations } if (isolatedDeclarations === true) { isolatedDeclarations = {} } if (isolatedDeclarations) { isolatedDeclarations.stripInternal ??= !!compilerOptions?.stripInternal // @ts-expect-error omitted in user options isolatedDeclarations.sourcemap = !!compilerOptions.declarationMap }
return { cwd, tsconfig, compilerOptions, isolatedDeclarations, sourcemap, dtsInput, emitDtsOnly, resolve, }}