From 18ef4e3826cb035159dde99b4496ce4c2cd5dca3 Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Mon, 13 Jul 2026 20:47:40 +0900 Subject: [PATCH] fix: validate options closes #276 --- src/generate.ts | 2 +- src/options.ts | 53 +++++++++++++++++++++++++++++++++++++------------ src/tsgo.ts | 28 +++++++++++--------------- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/generate.ts b/src/generate.ts index 381a0d2..5708259 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -120,7 +120,7 @@ export function createGeneratePlugin({ tsgoContext = await runTsgo( logger, rootDir, - tsconfig, + tsconfig!, sourcemap, tsgo.path, ) diff --git a/src/options.ts b/src/options.ts index 9f46868..22492a8 100644 --- a/src/options.ts +++ b/src/options.ts @@ -7,7 +7,7 @@ import { type TsconfigJsonResolved, } from 'get-tsconfig' import { createDebug } from 'obug' -import { isTS7Installed } from './tsgo.ts' +import { isTS70Installed } from './tsgo.ts' import type { IsolatedDeclarationsOptions } from 'rolldown/experimental' const debug = createDebug('rolldown-plugin-dts:options') @@ -344,21 +344,55 @@ export function resolveOptions({ compilerOptions, } + // Volar relate + if (vue || tsMacro) { + if (isTS70Installed()) { + throw new Error( + 'TypeScript 7.0 does not yet have a stable API and is experimental. The `vue` and `tsMacro` options are not yet supported with TypeScript 7.0.', + ) + } + if (generator && generator !== 'tsc') { + logger.warn( + 'The `vue` and `tsMacro` options are enabled, which requires the `tsc` generator. The `generator` option is ignored.', + ) + } + generator = 'tsc' + } + if (!generator) { - if (vue || tsMacro) { - // Volar relate - generator = 'tsc' - } else if (tsgo) { + if (tsgo) { generator = 'tsgo' } else if (oxc || compilerOptions?.isolatedDeclarations) { generator = 'oxc' - } else if (isTS7Installed()) { + } else if (isTS70Installed()) { generator = 'tsgo' } else { generator = 'tsc' } } + if (generator === 'tsc') { + try { + require.resolve('typescript') + } catch { + throw new Error( + 'TypeScript is not installed. You can install `typescript` package, or enable `isolatedDeclarations` in your `tsconfig.json` to use Oxc instead.', + ) + } + } else if (generator === 'tsgo') { + if (!tsconfig) { + throw new Error( + 'tsgo generator requires a tsconfig file to be specified.', + ) + } + if (!warnedTsgo) { + warnedTsgo = true + logger.warn( + 'TypeScript 7.0 does not yet have a stable API and is experimental. Some options will be unavailable.', + ) + } + } + if (oxc === true || !oxc) oxc = {} if (oxc) { oxc.stripInternal ??= !!compilerOptions?.stripInternal @@ -369,13 +403,6 @@ export function resolveOptions({ emitJs ??= !!(compilerOptions.checkJs || compilerOptions.allowJs) - if (generator === 'tsgo' && !warnedTsgo) { - logger.warn( - 'TypeScript 7.0 does not yet have a stable API and is experimental. Some options will be unavailable.', - ) - warnedTsgo = true - } - const resolvedEntry = entry ? Array.isArray(entry) ? entry diff --git a/src/tsgo.ts b/src/tsgo.ts index 4a4b920..e0dde7f 100644 --- a/src/tsgo.ts +++ b/src/tsgo.ts @@ -3,26 +3,19 @@ import { mkdtemp, rm } from 'node:fs/promises' import { createRequire } from 'node:module' import { tmpdir } from 'node:os' import path from 'node:path' +import { styleText } from 'node:util' import { createDebug } from 'obug' import type { Logger } from './options.ts' -import { styleText } from 'node:util' const require = createRequire(import.meta.url) const debug = createDebug('rolldown-plugin-dts:tsgo') -export function getTypeScriptMajor(): number | undefined { +export function isTS70Installed(): boolean { try { - const { version } = require('typescript/package.json') - const major = +version.split('.', 1)[0] - return Number.isNaN(major) ? undefined : major - } catch { - return - } -} - -export function isTS7Installed(): boolean { - const major = getTypeScriptMajor() - return major != null && major >= 7 + const { versionMajorMinor } = require('typescript') + return versionMajorMinor === '7.0' + } catch {} + return false } const spawnAsync = (...args: Parameters) => @@ -39,7 +32,9 @@ export async function getTsgoPathFromNodeModules( ): Promise { if (tsgoPathCache) return tsgoPathCache - const pkgName = isTS7Installed() ? 'typescript' : '@typescript/native-preview' + const pkgName = isTS70Installed() + ? 'typescript' + : '@typescript/native-preview' const tsgoPkg = import.meta.resolve(`${pkgName}/package.json`) const { default: { version }, @@ -61,7 +56,7 @@ export interface TsgoContext { export async function runTsgo( logger: Logger, rootDir: string, - tsconfig?: string, + tsconfig: string, sourcemap?: boolean, tsgoPath?: string, ): Promise { @@ -84,7 +79,8 @@ export async function runTsgo( 'false', '--declaration', '--emitDeclarationOnly', - ...(tsconfig ? ['-p', tsconfig] : []), + '-p', + tsconfig, '--outDir', tsgoDist, '--rootDir', -- knotmirror 0.0.1