diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index cd9a880ac..b3cb7258a 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -906,15 +906,25 @@ const composeDtsConfig = async ( libConfig: LibConfig, dtsExtension: string, ): Promise => { - const { dts, bundle, output, autoExternal, banner, footer } = libConfig; + const { output, autoExternal, banner, footer } = libConfig; + + let { dts } = libConfig; if (dts === false || dts === undefined) return {}; + // DTS default to bundleless whether js is bundle or not + if (dts === true) { + dts = { + bundle: false, + }; + } + const { pluginDts } = await import('rsbuild-plugin-dts'); return { plugins: [ pluginDts({ - bundle: dts?.bundle ?? bundle, + // Only setting ⁠dts.bundle to true will generate the bundled d.ts. + bundle: dts?.bundle ?? false, distPath: dts?.distPath ?? output?.distPath?.root ?? './dist', abortOnError: dts?.abortOnError ?? true, dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts', diff --git a/packages/core/src/types/config/index.ts b/packages/core/src/types/config/index.ts index 35e9e5502..3f4247426 100644 --- a/packages/core/src/types/config/index.ts +++ b/packages/core/src/types/config/index.ts @@ -32,7 +32,7 @@ export type Dts = | (Pick & { autoExtension?: boolean; }) - | false; + | boolean; export type AutoExternal = | boolean diff --git a/packages/create-rslib/template-node-dual-ts/rslib.config.ts b/packages/create-rslib/template-node-dual-ts/rslib.config.ts index f1f926319..cfac2795e 100644 --- a/packages/create-rslib/template-node-dual-ts/rslib.config.ts +++ b/packages/create-rslib/template-node-dual-ts/rslib.config.ts @@ -5,7 +5,7 @@ export default defineConfig({ { format: 'esm', syntax: 'es2021', - dts: {}, + dts: true, }, { format: 'cjs', diff --git a/packages/create-rslib/template-node-esm-ts/rslib.config.ts b/packages/create-rslib/template-node-esm-ts/rslib.config.ts index db48f353d..88195290c 100644 --- a/packages/create-rslib/template-node-esm-ts/rslib.config.ts +++ b/packages/create-rslib/template-node-esm-ts/rslib.config.ts @@ -5,7 +5,7 @@ export default defineConfig({ { format: 'esm', syntax: 'es2021', - dts: {}, + dts: true, }, ], output: { target: 'node' }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 043b9e8b4..23f52785f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -521,6 +521,8 @@ importers: tests/integration/dts/bundle-false/false: {} + tests/integration/dts/bundle-false/true: {} + tests/integration/dts/bundle/abort-on-error: {} tests/integration/dts/bundle/absolute-entry: {} @@ -535,6 +537,8 @@ importers: tests/integration/dts/bundle/false: {} + tests/integration/dts/bundle/true: {} + tests/integration/entry/glob: {} tests/integration/entry/multiple: {} diff --git a/tests/integration/dts/bundle-false/true/package.json b/tests/integration/dts/bundle-false/true/package.json new file mode 100644 index 000000000..20cb0545d --- /dev/null +++ b/tests/integration/dts/bundle-false/true/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-true-bundle-false-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts/bundle-false/true/rslib.config.ts b/tests/integration/dts/bundle-false/true/rslib.config.ts new file mode 100644 index 000000000..53e68b366 --- /dev/null +++ b/tests/integration/dts/bundle-false/true/rslib.config.ts @@ -0,0 +1,18 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: true, + }), + generateBundleCjsConfig(), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts/bundle/true/package.json b/tests/integration/dts/bundle/true/package.json new file mode 100644 index 000000000..92aeb90f9 --- /dev/null +++ b/tests/integration/dts/bundle/true/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-true-bundle-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts/bundle/true/rslib.config.ts b/tests/integration/dts/bundle/true/rslib.config.ts new file mode 100644 index 000000000..6c76fc150 --- /dev/null +++ b/tests/integration/dts/bundle/true/rslib.config.ts @@ -0,0 +1,17 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: true, + }), + generateBundleCjsConfig(), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts/index.test.ts b/tests/integration/dts/index.test.ts index d5c04a02e..81446f37b 100644 --- a/tests/integration/dts/index.test.ts +++ b/tests/integration/dts/index.test.ts @@ -38,6 +38,20 @@ describe('dts when bundle: false', () => { expect(files.esm).toMatchInlineSnapshot('undefined'); }); + test('dts true', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'true'); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts/bundle-false/true/dist/esm/index.d.ts", + "/tests/integration/dts/bundle-false/true/dist/esm/sum.d.ts", + "/tests/integration/dts/bundle-false/true/dist/esm/utils/numbers.d.ts", + "/tests/integration/dts/bundle-false/true/dist/esm/utils/strings.d.ts", + ] + `); + }); + test('distPath', async () => { const fixturePath = join(__dirname, 'bundle-false', 'dist-path'); const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); @@ -80,17 +94,25 @@ describe('dts when bundle: false', () => { describe('dts when bundle: true', () => { test('basic', async () => { const fixturePath = join(__dirname, 'bundle', 'basic'); - const { entryFiles, entries } = await buildAndGetResults({ + const { files, entries } = await buildAndGetResults({ fixturePath, type: 'dts', }); - expect(entryFiles.esm).toMatchInlineSnapshot( - `"/tests/integration/dts/bundle/basic/dist/esm/index.d.ts"`, + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts/bundle/basic/dist/esm/index.d.ts", + ] + `, ); - expect(entryFiles.cjs).toMatchInlineSnapshot( - `"/tests/integration/dts/bundle/basic/dist/cjs/index.d.ts"`, + expect(files.cjs).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts/bundle/basic/dist/cjs/index.d.ts", + ] + `, ); expect(entries).toMatchSnapshot(); @@ -98,23 +120,46 @@ describe('dts when bundle: true', () => { test('dts false', async () => { const fixturePath = join(__dirname, 'bundle', 'false'); - const { entryFiles } = await buildAndGetResults({ + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot('undefined'); + }); + + test('dts true', async () => { + const fixturePath = join(__dirname, 'bundle', 'true'); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts', }); - expect(entryFiles.esm).toMatchInlineSnapshot('undefined'); + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts/bundle/true/dist/esm/index.d.ts", + "/tests/integration/dts/bundle/true/dist/esm/sum.d.ts", + "/tests/integration/dts/bundle/true/dist/esm/utils/numbers.d.ts", + "/tests/integration/dts/bundle/true/dist/esm/utils/strings.d.ts", + ] + `, + ); }); test('distPath', async () => { const fixturePath = join(__dirname, 'bundle', 'dist-path'); - const { entryFiles } = await buildAndGetResults({ + const { files } = await buildAndGetResults({ fixturePath, type: 'dts', }); - expect(entryFiles.esm).toMatchInlineSnapshot( - `"/tests/integration/dts/bundle/dist-path/dist/custom/index.d.ts"`, + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts/bundle/dist-path/dist/custom/index.d.ts", + ] + `, ); }); @@ -130,37 +175,49 @@ describe('dts when bundle: true', () => { test('autoExtension: true', async () => { const fixturePath = join(__dirname, 'bundle', 'auto-extension'); - const { entryFiles } = await buildAndGetResults({ + const { files } = await buildAndGetResults({ fixturePath, type: 'dts', }); - expect(entryFiles.cjs).toMatchInlineSnapshot( - `"/tests/integration/dts/bundle/auto-extension/dist/cjs/index.d.cts"`, + expect(files.cjs).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts/bundle/auto-extension/dist/cjs/index.d.cts", + ] + `, ); }); test('bundleName -- set source.entry', async () => { const fixturePath = join(__dirname, 'bundle', 'bundle-name'); - const { entryFiles } = await buildAndGetResults({ + const { files } = await buildAndGetResults({ fixturePath, type: 'dts', }); - expect(entryFiles.esm).toMatchInlineSnapshot( - `"/tests/integration/dts/bundle/bundle-name/dist/esm/bundleName.d.ts"`, + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts/bundle/bundle-name/dist/esm/bundleName.d.ts", + ] + `, ); }); test('entry is an absolute path', async () => { const fixturePath = join(__dirname, 'bundle', 'absolute-entry'); - const { entryFiles } = await buildAndGetResults({ + const { files } = await buildAndGetResults({ fixturePath, type: 'dts', }); - expect(entryFiles.esm).toMatchInlineSnapshot( - `"/tests/integration/dts/bundle/absolute-entry/dist/esm/index.d.ts"`, + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts/bundle/absolute-entry/dist/esm/index.d.ts", + ] + `, ); }); }); diff --git a/tests/scripts/shared.ts b/tests/scripts/shared.ts index bc4a389e6..1380703c7 100644 --- a/tests/scripts/shared.ts +++ b/tests/scripts/shared.ts @@ -82,6 +82,7 @@ export async function getResults( esm: 0, cjs: 0, umd: 0, + mf: 0, }; let key = ''; @@ -105,7 +106,9 @@ export async function getResults( globFolder = libConfig?.output?.distPath?.root!; } else if (type === 'dts' && libConfig.dts !== false) { globFolder = - libConfig.dts?.distPath! ?? libConfig?.output?.distPath?.root!; + libConfig.dts === true + ? libConfig?.output?.distPath?.root! + : (libConfig.dts?.distPath! ?? libConfig?.output?.distPath?.root!); } if (!globFolder) continue;