Skip to content

Commit 97dcb4e

Browse files
committed
feat: add tsconfig option
1 parent 7b8c23a commit 97dcb4e

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,23 @@ interface Options {
4545
*/
4646
emitDtsOnly?: boolean
4747

48+
/**
49+
* The path to the `tsconfig.json` file.
50+
*
51+
* When set to `false`, the plugin will ignore any `tsconfig.json` file.
52+
* However, `compilerOptions` can still be specified directly in the options.
53+
*
54+
* @default `tsconfig.json`
55+
*/
56+
tsconfig?: string | boolean
57+
4858
/**
4959
* The `compilerOptions` for the TypeScript compiler.
50-
* The default value will be inferred from the `tsconfig.json` file.
5160
*
5261
* @see https://www.typescriptlang.org/docs/handbook/compiler-options.html
5362
*/
5463
compilerOptions?: TsConfigJson.CompilerOptions
64+
5565
/**
5666
* When `true`, the plugin will generate `.d.ts` files using `oxc-transform`,
5767
* which is blazingly faster than `typescript` compiler.

src/generate.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createResolver } from 'dts-resolver'
2-
import { getTsconfig } from 'get-tsconfig'
2+
import { getTsconfig, parseTsconfig } from 'get-tsconfig'
33
import { isolatedDeclaration as oxcIsolatedDeclaration } from 'oxc-transform'
44
import {
55
filename_dts_to,
@@ -23,16 +23,47 @@ import type { Plugin } from 'rolldown'
2323
const meta = { dtsFile: true } as const
2424

2525
export function createGeneratePlugin({
26+
tsconfig,
2627
compilerOptions,
2728
isolatedDeclaration,
2829
resolve = false,
2930
emitDtsOnly = false,
3031
}: Pick<
3132
Options,
32-
'isolatedDeclaration' | 'resolve' | 'emitDtsOnly' | 'compilerOptions'
33+
| 'isolatedDeclaration'
34+
| 'resolve'
35+
| 'emitDtsOnly'
36+
| 'tsconfig'
37+
| 'compilerOptions'
3338
>): Plugin {
3439
const dtsMap = new Map<string, { code: string; src: string }>()
3540

41+
function resolveOptions(cwd?: string) {
42+
if (tsconfig === true || tsconfig == null) {
43+
const { config } = getTsconfig(cwd) || {}
44+
compilerOptions = {
45+
...config?.compilerOptions,
46+
...compilerOptions,
47+
}
48+
} else if (typeof tsconfig === 'string') {
49+
const config = parseTsconfig(tsconfig)
50+
compilerOptions = {
51+
...config.compilerOptions,
52+
...compilerOptions,
53+
}
54+
}
55+
56+
if (isolatedDeclaration == null) {
57+
isolatedDeclaration = !!compilerOptions?.isolatedDeclarations
58+
}
59+
if (isolatedDeclaration === true) {
60+
isolatedDeclaration = {}
61+
}
62+
if (isolatedDeclaration && isolatedDeclaration.stripInternal == null) {
63+
isolatedDeclaration.stripInternal = !!compilerOptions?.stripInternal
64+
}
65+
}
66+
3667
/**
3768
* A map of input id to output file name
3869
*
@@ -50,20 +81,7 @@ export function createGeneratePlugin({
5081
name: 'rolldown-plugin-dts:generate',
5182

5283
async buildStart(options) {
53-
if (!compilerOptions) {
54-
const { config } = getTsconfig(options.cwd) || {}
55-
compilerOptions = config?.compilerOptions as any
56-
}
57-
58-
if (isolatedDeclaration == null) {
59-
isolatedDeclaration = !!compilerOptions?.isolatedDeclarations
60-
}
61-
if (isolatedDeclaration === true) {
62-
isolatedDeclaration = {}
63-
}
64-
if (isolatedDeclaration && isolatedDeclaration.stripInternal == null) {
65-
isolatedDeclaration.stripInternal = !!compilerOptions?.stripInternal
66-
}
84+
resolveOptions(options.cwd)
6785

6886
if (!isolatedDeclaration) {
6987
initTs()

src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,23 @@ export interface Options {
1919
*/
2020
emitDtsOnly?: boolean
2121

22+
/**
23+
* The path to the `tsconfig.json` file.
24+
*
25+
* When set to `false`, the plugin will ignore any `tsconfig.json` file.
26+
* However, `compilerOptions` can still be specified directly in the options.
27+
*
28+
* @default `tsconfig.json`
29+
*/
30+
tsconfig?: string | boolean
31+
2232
/**
2333
* The `compilerOptions` for the TypeScript compiler.
24-
* The default value will be inferred from the `tsconfig.json` file.
2534
*
2635
* @see https://www.typescriptlang.org/docs/handbook/compiler-options.html
2736
*/
2837
compilerOptions?: TsConfigJson.CompilerOptions
38+
2939
/**
3040
* When `true`, the plugin will generate `.d.ts` files using `oxc-transform`,
3141
* which is blazingly faster than `typescript` compiler.

tests/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test('typescript compiler', async () => {
3131
emitDtsOnly: true,
3232
compilerOptions: {
3333
skipLibCheck: true,
34+
isolatedDeclarations: false,
3435
},
3536
isolatedDeclaration: false,
3637
}),

0 commit comments

Comments
 (0)