diff --git a/README.md b/README.md index 6af0cd1..f54e2f1 100644 --- a/README.md +++ b/README.md @@ -45,13 +45,23 @@ interface Options { */ 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. - * The default value will be inferred from the `tsconfig.json` file. * * @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. diff --git a/src/generate.ts b/src/generate.ts index 9ae496d..5555d29 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -1,5 +1,5 @@ import { createResolver } from 'dts-resolver' -import { getTsconfig } from 'get-tsconfig' +import { getTsconfig, parseTsconfig } from 'get-tsconfig' import { isolatedDeclaration as oxcIsolatedDeclaration } from 'oxc-transform' import { filename_dts_to, @@ -23,16 +23,47 @@ import type { Plugin } from 'rolldown' const meta = { dtsFile: true } as const export function createGeneratePlugin({ + tsconfig, compilerOptions, isolatedDeclaration, resolve = false, emitDtsOnly = false, }: Pick< Options, - 'isolatedDeclaration' | 'resolve' | 'emitDtsOnly' | 'compilerOptions' + | 'isolatedDeclaration' + | 'resolve' + | 'emitDtsOnly' + | 'tsconfig' + | 'compilerOptions' >): Plugin { const dtsMap = new Map() + function resolveOptions(cwd?: string) { + if (tsconfig === true || tsconfig == null) { + const { config } = getTsconfig(cwd) || {} + compilerOptions = { + ...config?.compilerOptions, + ...compilerOptions, + } + } else if (typeof tsconfig === 'string') { + const config = parseTsconfig(tsconfig) + compilerOptions = { + ...config.compilerOptions, + ...compilerOptions, + } + } + + if (isolatedDeclaration == null) { + isolatedDeclaration = !!compilerOptions?.isolatedDeclarations + } + if (isolatedDeclaration === true) { + isolatedDeclaration = {} + } + if (isolatedDeclaration && isolatedDeclaration.stripInternal == null) { + isolatedDeclaration.stripInternal = !!compilerOptions?.stripInternal + } + } + /** * A map of input id to output file name * @@ -50,20 +81,7 @@ export function createGeneratePlugin({ name: 'rolldown-plugin-dts:generate', async buildStart(options) { - if (!compilerOptions) { - const { config } = getTsconfig(options.cwd) || {} - compilerOptions = config?.compilerOptions as any - } - - if (isolatedDeclaration == null) { - isolatedDeclaration = !!compilerOptions?.isolatedDeclarations - } - if (isolatedDeclaration === true) { - isolatedDeclaration = {} - } - if (isolatedDeclaration && isolatedDeclaration.stripInternal == null) { - isolatedDeclaration.stripInternal = !!compilerOptions?.stripInternal - } + resolveOptions(options.cwd) if (!isolatedDeclaration) { initTs() diff --git a/src/index.ts b/src/index.ts index 2db4ffd..606585e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,13 +19,23 @@ export interface Options { */ 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. - * The default value will be inferred from the `tsconfig.json` file. * * @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. diff --git a/tests/index.test.ts b/tests/index.test.ts index f15a3bf..d674195 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -31,6 +31,7 @@ test('typescript compiler', async () => { emitDtsOnly: true, compilerOptions: { skipLibCheck: true, + isolatedDeclarations: false, }, isolatedDeclaration: false, }),