diff --git a/e2e/cases/dts/__snapshots__/index.test.ts.snap b/e2e/cases/dts/__snapshots__/index.test.ts.snap index b8d780689..099f93cc9 100644 --- a/e2e/cases/dts/__snapshots__/index.test.ts.snap +++ b/e2e/cases/dts/__snapshots__/index.test.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`dts when bundle: false > basic - bundleless dts 1`] = ` +exports[`dts when bundle: false > basic 1`] = ` { "./dist/esm/index.d.ts": "export * from './utils/numbers'; export * from './utils/strings'; @@ -20,7 +20,7 @@ export declare const str3 = "str3"; } `; -exports[`dts when bundle: true > basic - bundle dts 1`] = ` +exports[`dts when bundle: true > basic 1`] = ` { "esm": "export declare const num1 = 1; diff --git a/e2e/cases/dts/bundle-false/rslib.distpath.config.ts b/e2e/cases/dts/bundle-false/distPath.config.ts similarity index 100% rename from e2e/cases/dts/bundle-false/rslib.distpath.config.ts rename to e2e/cases/dts/bundle-false/distPath.config.ts diff --git a/e2e/cases/dts/bundle-false/rslib.false.config.ts b/e2e/cases/dts/bundle-false/dtsFalse.config.ts similarity index 100% rename from e2e/cases/dts/bundle-false/rslib.false.config.ts rename to e2e/cases/dts/bundle-false/dtsFalse.config.ts diff --git a/e2e/cases/dts/bundle/rslib.distpath.config.ts b/e2e/cases/dts/bundle/distPath.config.ts similarity index 100% rename from e2e/cases/dts/bundle/rslib.distpath.config.ts rename to e2e/cases/dts/bundle/distPath.config.ts diff --git a/e2e/cases/dts/bundle/rslib.false.config.ts b/e2e/cases/dts/bundle/dtsFalse.config.ts similarity index 100% rename from e2e/cases/dts/bundle/rslib.false.config.ts rename to e2e/cases/dts/bundle/dtsFalse.config.ts diff --git a/e2e/cases/dts/index.test.ts b/e2e/cases/dts/index.test.ts index 4699cd9da..252918414 100644 --- a/e2e/cases/dts/index.test.ts +++ b/e2e/cases/dts/index.test.ts @@ -3,7 +3,7 @@ import { buildAndGetResults } from '@e2e/helper'; import { describe, expect, test } from 'vitest'; describe('dts when bundle: false', () => { - test('basic - bundleless dts', async () => { + test('basic', async () => { const fixturePath = join(__dirname, 'bundle-false'); const { files, contents } = await buildAndGetResults( fixturePath, @@ -16,22 +16,22 @@ describe('dts when bundle: false', () => { expect(contents.esm).toMatchSnapshot(); }); - test('dts false - bundleless dts', async () => { + test('dts false', async () => { const fixturePath = join(__dirname, 'bundle-false'); const { files } = await buildAndGetResults( fixturePath, - 'rslib.false.config.ts', + 'dtsFalse.config.ts', 'dts', ); expect(files.esm).toBe(undefined); }); - test('distPath - bundleless dts', async () => { + test('distPath', async () => { const fixturePath = join(__dirname, 'bundle-false'); const { files } = await buildAndGetResults( fixturePath, - 'rslib.distpath.config.ts', + 'distPath.config.ts', 'dts', ); expect(files.esm?.length).toBe(4); @@ -40,7 +40,7 @@ describe('dts when bundle: false', () => { }); describe('dts when bundle: true', () => { - test('basic - bundle dts', async () => { + test('basic', async () => { const fixturePath = join(__dirname, 'bundle'); const { entryFiles, entries } = await buildAndGetResults( fixturePath, @@ -52,22 +52,22 @@ describe('dts when bundle: true', () => { expect(entries).toMatchSnapshot(); }); - test('dts false - bundle dts', async () => { + test('dts false', async () => { const fixturePath = join(__dirname, 'bundle'); const { entryFiles } = await buildAndGetResults( fixturePath, - 'rslib.false.config.ts', + 'dtsFalse.config.ts', 'dts', ); expect(entryFiles.esm).toEqual(undefined); }); - test('distPath - bundle dts', async () => { + test('distPath', async () => { const fixturePath = join(__dirname, 'bundle'); const { entryFiles } = await buildAndGetResults( fixturePath, - 'rslib.distpath.config.ts', + 'distPath.config.ts', 'dts', ); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index def114dd7..404975cd3 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -325,7 +325,6 @@ const composeBundleConfig = (bundle = true): RsbuildConfig => { const composeDtsConfig = async ( libConfig: LibConfig, - entryConfig: RsbuildConfig, ): Promise => { const { dts, bundle, output } = libConfig; @@ -337,9 +336,6 @@ const composeDtsConfig = async ( pluginDts({ bundle: dts?.bundle ?? bundle, distPath: dts?.distPath ?? output?.distPath?.root ?? './dist', - tsconfigPath: dts?.tsconfigPath, - // TODO: temporarily use main as dts entry - entryPath: entryConfig.source?.entry?.main as string, }), ], }; @@ -408,8 +404,7 @@ async function composeLibRsbuildConfig( config.bundle, dirname(configPath), ); - - const dtsConfig = await composeDtsConfig(config, entryConfig); + const dtsConfig = await composeDtsConfig(config); return mergeRsbuildConfig( formatConfig, diff --git a/packages/core/src/types/config/index.ts b/packages/core/src/types/config/index.ts index 7451ef723..a04315901 100644 --- a/packages/core/src/types/config/index.ts +++ b/packages/core/src/types/config/index.ts @@ -27,7 +27,6 @@ export type Dts = | { bundle: boolean; distPath?: string; - tsconfigPath?: string; } | false; diff --git a/packages/plugin-dts/src/apiExtractor.ts b/packages/plugin-dts/src/apiExtractor.ts index 04a031c29..58a5ec4bd 100644 --- a/packages/plugin-dts/src/apiExtractor.ts +++ b/packages/plugin-dts/src/apiExtractor.ts @@ -20,36 +20,43 @@ export function bundleDts(options: BundleOptions): void { entry = 'index.d.ts', tsconfigPath = 'tsconfig.json', } = options; - const untrimmedFilePath = join(cwd, relative(cwd, outDir), 'index.d.ts'); - const internalConfig = { - mainEntryPointFilePath: entry, - // TODO: use !externals - // bundledPackages: [], - dtsRollup: { - enabled: true, - untrimmedFilePath, - }, - compiler: { - tsconfigFilePath: join(cwd, tsconfigPath), - }, - projectFolder: cwd, - }; + try { + const untrimmedFilePath = join(cwd, relative(cwd, outDir), 'index.d.ts'); + const internalConfig = { + mainEntryPointFilePath: entry, + // TODO: use !externals + // bundledPackages: [], + dtsRollup: { + enabled: true, + untrimmedFilePath, + }, + compiler: { + tsconfigFilePath: tsconfigPath.includes(cwd) + ? tsconfigPath + : join(cwd, tsconfigPath), + }, + projectFolder: cwd, + }; - const extractorConfig = ExtractorConfig.prepare({ - configObject: internalConfig, - configObjectFullPath: undefined, - packageJsonFullPath: join(cwd, 'package.json'), - }); + const extractorConfig = ExtractorConfig.prepare({ + configObject: internalConfig, + configObjectFullPath: undefined, + packageJsonFullPath: join(cwd, 'package.json'), + }); - const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, { - localBuild: true, - }); + const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, { + localBuild: true, + }); - if (!extractorResult.succeeded) { + if (!extractorResult.succeeded) { + throw new Error('API Extractor rollup error'); + } + + logger.info( + `API Extractor writing package typings succeeded: ${untrimmedFilePath}`, + ); + } catch (e) { + logger.error('API Extractor', e); throw new Error('API Extractor rollup error'); } - - logger.info( - `API Extractor writing package typings succeeded: ${untrimmedFilePath}`, - ); } diff --git a/packages/plugin-dts/src/dts.ts b/packages/plugin-dts/src/dts.ts index b5aeea427..aa4cb59d3 100644 --- a/packages/plugin-dts/src/dts.ts +++ b/packages/plugin-dts/src/dts.ts @@ -7,9 +7,9 @@ import { emitDts } from './tsc'; import { ensureTempDeclarationDir, loadTsconfig } from './utils'; export async function generateDts(data: DtsGenOptions): Promise { - const { options: pluginOptions, cwd, isWatch, name } = data; + const { tsconfigPath, distPath, bundle, entryPath, cwd, isWatch, name } = + data; logger.start(`Generating DTS... ${color.gray(`(${name})`)}`); - const { tsconfigPath, distPath, bundle, entryPath } = pluginOptions; const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath); if (!configPath) { logger.error(`tsconfig.json not found in ${cwd}`); @@ -38,7 +38,10 @@ export async function generateDts(data: DtsGenOptions): Promise { declarationDir!, relativePath, basename(entrySourcePath), - ).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts'); + ).replace( + /\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/, + '.d.ts', + ); } const bundleDtsIfNeeded = async () => { @@ -77,7 +80,7 @@ export async function generateDts(data: DtsGenOptions): Promise { } process.on('message', async (data: DtsGenOptions) => { - if (!data.options) { + if (!data.cwd) { return; } diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 92ee586e0..1d5615e58 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -2,18 +2,17 @@ import { fork } from 'node:child_process'; import { extname, join } from 'node:path'; import type { RsbuildPlugin } from '@rsbuild/core'; -export type pluginDtsOptions = { +export type PluginDtsOptions = { bundle?: boolean; distPath?: string; - tsconfigPath?: string; - entryPath?: string; }; -export type DtsGenOptions = { +export type DtsGenOptions = PluginDtsOptions & { name: string; - options: pluginDtsOptions; cwd: string; isWatch: boolean; + entryPath?: string; + tsconfigPath?: string; }; export const PLUGIN_DTS_NAME = 'rsbuild:dts'; @@ -23,16 +22,10 @@ export const PLUGIN_DTS_NAME = 'rsbuild:dts'; // TODO: support incremental build, to build one or more projects and their dependencies // TODO: support autoExtension for dts files // TODO: deal alias in dts -export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({ +export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({ name: PLUGIN_DTS_NAME, setup(api) { - const config = api.getRsbuildConfig(); - - options.bundle = options.bundle ?? false; - options.distPath = - options.distPath ?? config.output?.distPath?.root ?? 'dist'; - const dtsPromises: Promise[] = []; api.onBeforeEnvironmentCompile( @@ -41,14 +34,22 @@ export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({ return; } + const { config } = environment; + + options.bundle = options.bundle ?? false; + options.distPath = options.distPath ?? config.output?.distPath?.root; + const jsExtension = extname(__filename); const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], { stdio: 'inherit', }); - const dtsGenOptions = { + const dtsGenOptions: DtsGenOptions = { + ...options, + // TODO: temporarily use main as dts entry, only accept single entry + entryPath: config.source?.entry?.main as string, + tsconfigPath: config.source.tsconfigPath, name: environment.name, - options, cwd: api.context.rootPath, isWatch, }; diff --git a/scripts/dictionary.txt b/scripts/dictionary.txt index 422cbc5c2..e17e9e4e0 100644 --- a/scripts/dictionary.txt +++ b/scripts/dictionary.txt @@ -10,6 +10,7 @@ bundleless caniuse chunkhash Chunktmp +cjsx classname codegen codesandbox @@ -20,6 +21,7 @@ corejs corepack craco crossorigin +ctsx datauri deepmerge distpath @@ -54,7 +56,9 @@ menlo metafile microfrontend microfrontends +mjsx modularly +mtsx napi nolyfill ntqry