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
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ const composeDtsConfig = async (
// Only setting ⁠dts.bundle to true will generate the bundled d.ts.
bundle: dts?.bundle ?? false,
distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
build: dts?.build ?? false,
abortOnError: dts?.abortOnError ?? true,
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
autoExternal,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export type Syntax =
| string[];

export type Dts =
| (Pick<PluginDtsOptions, 'bundle' | 'distPath' | 'abortOnError'> & {
| (Pick<
PluginDtsOptions,
'bundle' | 'distPath' | 'abortOnError' | 'build'
> & {
autoExtension?: boolean;
})
| boolean;
Expand Down
39 changes: 38 additions & 1 deletion packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from 'node:fs';
import { basename, dirname, isAbsolute, join, relative } from 'node:path';
import {
basename,
dirname,
isAbsolute,
join,
normalize,
relative,
resolve,
} from 'node:path';
import { logger } from '@rsbuild/core';
import color from 'picocolors';
import ts from 'typescript';
Expand Down Expand Up @@ -110,6 +118,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
tsconfigPath,
name,
cwd,
build,
isWatch,
dtsExtension = '.d.ts',
autoExternal = true,
Expand All @@ -133,6 +142,33 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
? distPath
: rawCompilerOptions.declarationDir || './dist';

if (build) {
// do not allow to use bundle DTS when 'build: true' since temp declarationDir should be set by user in tsconfig
if (bundle) {
throw Error(`Can not set "dts.bundle: true" when "dts.build: true"`);
}

// can not set '--declarationDir' or '--outDir' when 'build: true'.
if (
(!rawCompilerOptions.outDir ||
normalize(rawCompilerOptions.outDir) !==
normalize(resolve(dirname(configPath), outDir))) &&
(!rawCompilerOptions.declarationDir ||
normalize(rawCompilerOptions.declarationDir) !==
normalize(resolve(dirname(configPath), outDir)))
) {
const info =
rawCompilerOptions.outDir && !rawCompilerOptions.declarationDir
? 'outDir'
: 'declarationDir';
throw Error(
`Please set ${info}: "${outDir}" in ${color.underline(
configPath,
)} to keep it same as "dts.distPath" or "output.distPath" field in lib config.`,
);
}
}

const getDeclarationDir = (bundle: boolean, distPath?: string) => {
if (bundle) {
return ensureTempDeclarationDir(cwd);
Expand Down Expand Up @@ -204,6 +240,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
onComplete,
bundle,
isWatch,
build,
);

if (!isWatch) {
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const __dirname = dirname(__filename);
export type PluginDtsOptions = {
bundle?: boolean;
distPath?: string;
build?: boolean;
abortOnError?: boolean;
dtsExtension?: string;
autoExternal?:
Expand All @@ -33,6 +34,7 @@ export type DtsGenOptions = PluginDtsOptions & {
cwd: string;
isWatch: boolean;
dtsEntry: DtsEntry;
build?: boolean;
tsconfigPath?: string;
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
};
Expand All @@ -46,14 +48,14 @@ export const PLUGIN_DTS_NAME = 'rsbuild:dts';

// use ts compiler API to generate bundleless dts
// use ts compiler API and api-extractor to generate dts bundle
// TODO: support incremental build, to build one or more projects and their dependencies
// TODO: deal alias in dts
export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
name: PLUGIN_DTS_NAME,

setup(api) {
options.bundle = options.bundle ?? false;
options.abortOnError = options.abortOnError ?? true;
options.build = options.build ?? false;

const dtsPromises: Promise<TaskResult>[] = [];
let promisesResult: TaskResult[] = [];
Expand Down
Loading
Loading