Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
"@rstest/core": "^0.10.6",
"@types/fs-extra": "^11.0.4",
"@types/node": "^24.13.2",
"@typescript/native-preview": "7.0.0-dev.20260704.1",
"cspell-ban-words": "^0.0.4",
"fs-extra": "^11.3.6",
"heading-case": "^1.1.3",
"nano-staged": "^1.0.2",
"prebundle": "1.6.5",
"prettier": "^3.9.4",
"prettier-plugin-packagejson": "^3.0.2",
"simple-git-hooks": "^2.13.1",
Expand Down
5 changes: 2 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,16 @@
"get-tsconfig": "5.0.0-beta.5",
"memfs": "^4.57.8",
"path-serializer": "0.6.0",
"prebundle": "1.6.5",
"rsbuild-plugin-publint": "^1.0.0",
"rslib": "npm:@rslib/core@0.23.2",
"rslog": "^2.1.3",
"semver": "^7.8.5",
"tinyglobby": "^0.2.17",
"typescript": "^6.0.3"
"typescript": "^7.0.2"
},
"peerDependencies": {
"@microsoft/api-extractor": "^7",
"typescript": "^5 || ^6 || ^7.0.1-0"
"typescript": "^5 || ^6 || ^7"
},
"peerDependenciesMeta": {
"@microsoft/api-extractor": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-rslib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"rsbuild-plugin-publint": "^1.0.0",
"rslib": "npm:@rslib/core@0.23.2",
"tsx": "^4.23.0",
"typescript": "^6.0.3"
"typescript": "^7.0.2"
},
"engines": {
"node": "^20.19.0 || >=22.12.0"
Expand Down
22 changes: 4 additions & 18 deletions packages/plugin-dts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,30 +294,16 @@ import { foo } from './foo.mjs'; // expected output of './dist/bar.d.mts'
### tsgo

- **Type:** `boolean`
- **Default:** `true` when the installed `typescript` package is version 7 or higher, otherwise `false`
- **Default:** `true` when TypeScript 7+ is detected, otherwise `false`

Whether to generate declaration files with [TypeScript Go](https://github.com/microsoft/typescript-go).
Whether to generate declaration files using [native TypeScript](https://github.com/microsoft/typescript-go).

After installing TypeScript 7 or higher, Rslib will automatically enable this option.
When unset, Rslib enables this option automatically when TypeScript 7+ is detected.

```bash
npm add typescript@rc -D
npm add typescript@latest -D
```

You can also install [@typescript/native-preview](https://www.npmjs.com/package/@typescript/native-preview) and manually enable this option.

```bash
npm add @typescript/native-preview -D
```

```js
pluginDts({
tsgo: true,
});
```

> The `@typescript/native-preview` usage is deprecated and kept only for compatibility. Prefer installing `typescript@rc` to use tsgo.

To ensure consistency during local development, you need to install the corresponding [VS Code Preview Extension](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview) and add the following setting to VS Code:

```json
Expand Down
10 changes: 3 additions & 7 deletions packages/plugin-dts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,22 @@
"@rslib/tsconfig": "workspace:*",
"get-tsconfig": "5.0.0-beta.5",
"magic-string": "^0.30.21",
"prebundle": "1.6.5",
"rsbuild-plugin-publint": "^1.0.0",
"rslib": "npm:@rslib/core@0.23.2",
"tinyglobby": "^0.2.17",
"tsconfig-paths": "^4.2.0",
"typescript": "^6.0.3"
"typescript": "^7.0.2",
"typescript6-api": "npm:typescript@^6.0.3"
},
"peerDependencies": {
"@microsoft/api-extractor": "^7",
"@rsbuild/core": "^2.0.0",
"@typescript/native-preview": "^7.0.0-0",
"typescript": "^5 || ^6 || ^7.0.1-0"
"typescript": "^5 || ^6 || ^7"
Comment thread
Timeless0911 marked this conversation as resolved.
},
"peerDependenciesMeta": {
"@microsoft/api-extractor": {
"optional": true
},
"@typescript/native-preview": {
"optional": true
},
"typescript": {
"optional": true
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-dts/src/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs';
import { join, relative } from 'node:path';
import type * as ApiExtractor from '@microsoft/api-extractor';
import { logger } from '@rsbuild/core';
import type { DtsEntry } from './index';
import type { DtsEntry } from './types/internal';
import { addBannerAndFooter, color, getTimeCost } from './utils';

const logPrefixApiExtractor = color.dim('[api-extractor]');
Expand Down
25 changes: 7 additions & 18 deletions packages/plugin-dts/src/backend.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import fs from 'node:fs';
import type { PluginDtsOptions } from './index';
import type { DtsGenerationBackend } from './types/internal';
import type { PluginDtsOptions } from './types/options';
import { createRequireFromPackageJson } from './utils';

export type DtsGenerationBackend =
| 'api-old'
| 'tsc-executable'
| 'tsgo-executable'
| 'isolated';

type ParsedTypescriptVersion = {
major: number;
minor: number;
Expand Down Expand Up @@ -52,10 +47,7 @@ const isTypeScriptVersionAtLeast7 = (version: string | undefined): boolean => {
};

export function validateExplicitIsolatedDtsOptions(
options: Pick<
PluginDtsOptions,
'isolated' | 'tsgo' | 'build' | 'abortOnError'
>,
options: PluginDtsOptions,
): void {
if (options.isolated !== true) {
return;
Expand All @@ -77,10 +69,7 @@ export function validateExplicitIsolatedDtsOptions(
}

export function resolveDtsGenerationBackend(
options: Pick<
PluginDtsOptions,
'isolated' | 'tsgo' | 'build' | 'abortOnError'
>,
options: PluginDtsOptions,
typescriptVersion?: string,
): DtsGenerationBackend {
validateExplicitIsolatedDtsOptions(options);
Expand All @@ -92,15 +81,15 @@ export function resolveDtsGenerationBackend(
if (isTypeScriptVersionAtLeast7(typescriptVersion)) {
if (options.tsgo === false) {
throw new Error(
'Can not set "dts.tsgo: false" when using TypeScript 7 or higher.',
'Can not set "dts.tsgo: false" when using `typescript` >= 7.0.0.',
);
}

return 'tsc-executable';
return 'ts7-executable';
}

if (options.tsgo === true) {
return 'tsgo-executable';
throw new Error('`dts.tsgo` requires `typescript` >= 7.0.0.');
Comment thread
Timeless0911 marked this conversation as resolved.
}

return 'api-old';
Expand Down
22 changes: 9 additions & 13 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@ import {
relative,
resolve,
} from 'node:path';
import type { DtsEntry, DtsGenOptions, DtsRedirect } from './index';
import type { DtsRedirect } from './types/options';
import type {
CompilerApiTsconfigResultForApi,
DtsEntry,
DtsGenOptions,
GetTsconfigTsconfigResultForExecutable,
} from './types/internal';
import {
calcLongestCommonPath,
color,
ensureTempDeclarationDir,
mergeAliasWithTsConfigPaths,
type CompilerApiTsconfigResultForApi,
type GetTsconfigTsconfigResultForExecutable,
} from './utils';

const isObject = (obj: unknown): obj is Record<string, any> =>
Object.prototype.toString.call(obj) === '[object Object]';

export const DEFAULT_EXCLUDED_PACKAGES: string[] = ['@types/react'];

type ExecutableDtsGenerationBackend = Extract<
DtsGenOptions['dtsBackend'],
'tsc-executable' | 'tsgo-executable'
>;

const isExecutableBackend = (
dtsBackend: DtsGenOptions['dtsBackend'],
): dtsBackend is ExecutableDtsGenerationBackend =>
dtsBackend === 'tsc-executable' || dtsBackend === 'tsgo-executable';
): boolean => dtsBackend === 'ts7-executable';

type CalculateBundledPackagesOptions = {
cwd: string;
Expand Down Expand Up @@ -144,8 +142,7 @@ export type PreparedDtsContext = {

export type EmitDtsOptions<
Tsconfig extends
| CompilerApiTsconfigResultForApi
| GetTsconfigTsconfigResultForExecutable,
CompilerApiTsconfigResultForApi | GetTsconfigTsconfigResultForExecutable,
> = {
name: string;
cwd: string;
Expand Down Expand Up @@ -362,7 +359,6 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
mod.emitDtsTsgo(
{
...emitOptions,
dtsBackend,
tsConfigResult:
tsConfigResult as GetTsconfigTsconfigResultForExecutable,
},
Expand Down
81 changes: 9 additions & 72 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,33 @@
import {
logger,
type LogLevel,
type RsbuildConfig,
type RsbuildPlugin,
} from '@rsbuild/core';
import { logger, type LogLevel, type RsbuildPlugin } from '@rsbuild/core';
import { type ChildProcess, fork } from 'node:child_process';
import { extname, join } from 'node:path';

import {
type DtsGenerationBackend,
readTypescriptVersion,
resolveDtsGenerationBackend,
} from './backend';
import { readTypescriptVersion, resolveDtsGenerationBackend } from './backend';
import {
createIsolatedDtsContext,
type IsolatedDtsContext,
processIsolatedDts,
} from './isolated';
import type { DtsGenOptions, DtsTsconfigResult } from './types/internal';
import type { PluginDtsOptions } from './types/options';
import {
cleanDtsFiles,
cleanTsBuildInfoFile,
clearTempDeclarationDir,
color,
type CompilerApiTsconfigResultForApi,
getDtsEmitPath,
type GetTsconfigTsconfigResultForExecutable,
loadTsconfig,
loadTsconfigResultForExecutable,
loadTypescript,
processSourceEntry,
warnIfOutside,
} from './utils';

export type DtsRedirect = {
path?: boolean;
extension?: boolean;
};

export type ApiExtractorOptions = {
bundledPackages?: string[];
};

export type PluginDtsOptions = {
bundle?: boolean | ApiExtractorOptions;
distPath?: string;
build?: boolean;
abortOnError?: boolean;
dtsExtension?: string;
alias?: Record<string, string>;
isolated?: boolean;
autoExternal?:
| boolean
| {
dependencies?: boolean;
optionalDependencies?: boolean;
peerDependencies?: boolean;
devDependencies?: boolean;
};
banner?: string;
footer?: string;
redirect?: DtsRedirect;
tsgo?: boolean;
};

export type DtsEntry = {
name: string;
path: string;
};

export type DtsGenOptions = Omit<
export type {
ApiExtractorOptions,
DtsRedirect,
PluginDtsOptions,
'bundle' | 'isolated' | 'tsgo'
> & {
bundle: boolean;
name: string;
cwd: string;
isWatch: boolean;
dtsEntry: DtsEntry[];
dtsEmitPath: string;
build?: boolean;
tsconfigPath: string;
tsConfigResult:
| CompilerApiTsconfigResultForApi
| GetTsconfigTsconfigResultForExecutable;
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
apiExtractorOptions?: ApiExtractorOptions;
loggerLevel: LogLevel;
dtsBackend: DtsGenerationBackend;
};
} from './types/options';

interface TaskResult {
status: 'success' | 'error';
Expand Down Expand Up @@ -172,10 +112,7 @@ export const pluginDts: (options?: PluginDtsOptions) => RsbuildPlugin = (
const configuredTsconfigPath =
config.source.tsconfigPath ?? 'tsconfig.json';
let tsconfigPath: string | undefined;
let tsConfigResult:
| CompilerApiTsconfigResultForApi
| GetTsconfigTsconfigResultForExecutable
| undefined;
let tsConfigResult: DtsTsconfigResult | undefined;

if (tsApi) {
tsconfigPath = tsApi.findConfigFile(
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-dts/src/isolated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type PreparedDtsContext,
prepareDtsContext,
} from './dts';
import type { DtsGenOptions } from './index';
import type { DtsGenOptions } from './types/internal';
import { color, processDtsFiles, rewriteDtsExtensions } from './utils';

export type IsolatedDtsContext = Omit<DtsGenOptions, 'dtsExtension'> &
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Program,
System,
WatchStatusReporter,
} from 'typescript';
} from 'typescript6-api';
import type { EmitDtsOptions } from './dts';
import {
color,
Expand All @@ -16,8 +16,8 @@ import {
processDtsFiles,
renameDtsFile,
updateDeclarationMapContent,
type CompilerApiTsconfigResultForApi,
} from './utils';
import type { CompilerApiTsconfigResultForApi } from './types/internal';

const logPrefixTsc = color.dim('[tsc]');
/*
Expand Down
Loading