Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support set dts: true and dts default to bundleless #325

Merged
merged 2 commits into from
Oct 23, 2024
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
14 changes: 12 additions & 2 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,15 +906,25 @@ const composeDtsConfig = async (
libConfig: LibConfig,
dtsExtension: string,
): Promise<RsbuildConfig> => {
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',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type Dts =
| (Pick<PluginDtsOptions, 'bundle' | 'distPath' | 'abortOnError'> & {
autoExtension?: boolean;
})
| false;
| boolean;

export type AutoExternal =
| boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
{
format: 'esm',
syntax: 'es2021',
dts: {},
dts: true,
},
{
format: 'cjs',
Expand Down
2 changes: 1 addition & 1 deletion packages/create-rslib/template-node-esm-ts/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
{
format: 'esm',
syntax: 'es2021',
dts: {},
dts: true,
},
],
output: { target: 'node' },
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/dts/bundle-false/true/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-true-bundle-false-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
18 changes: 18 additions & 0 deletions tests/integration/dts/bundle-false/true/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -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',
},
});
6 changes: 6 additions & 0 deletions tests/integration/dts/bundle/true/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-true-bundle-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
17 changes: 17 additions & 0 deletions tests/integration/dts/bundle/true/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -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',
},
});
95 changes: 76 additions & 19 deletions tests/integration/dts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
[
"<ROOT>/tests/integration/dts/bundle-false/true/dist/esm/index.d.ts",
"<ROOT>/tests/integration/dts/bundle-false/true/dist/esm/sum.d.ts",
"<ROOT>/tests/integration/dts/bundle-false/true/dist/esm/utils/numbers.d.ts",
"<ROOT>/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' });
Expand Down Expand Up @@ -80,41 +94,72 @@ 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(
`"<ROOT>/tests/integration/dts/bundle/basic/dist/esm/index.d.ts"`,
expect(files.esm).toMatchInlineSnapshot(
`
[
"<ROOT>/tests/integration/dts/bundle/basic/dist/esm/index.d.ts",
]
`,
);

expect(entryFiles.cjs).toMatchInlineSnapshot(
`"<ROOT>/tests/integration/dts/bundle/basic/dist/cjs/index.d.ts"`,
expect(files.cjs).toMatchInlineSnapshot(
`
[
"<ROOT>/tests/integration/dts/bundle/basic/dist/cjs/index.d.ts",
]
`,
);

expect(entries).toMatchSnapshot();
});

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(
`
[
"<ROOT>/tests/integration/dts/bundle/true/dist/esm/index.d.ts",
"<ROOT>/tests/integration/dts/bundle/true/dist/esm/sum.d.ts",
"<ROOT>/tests/integration/dts/bundle/true/dist/esm/utils/numbers.d.ts",
"<ROOT>/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(
`"<ROOT>/tests/integration/dts/bundle/dist-path/dist/custom/index.d.ts"`,
expect(files.esm).toMatchInlineSnapshot(
`
[
"<ROOT>/tests/integration/dts/bundle/dist-path/dist/custom/index.d.ts",
]
`,
);
});

Expand All @@ -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(
`"<ROOT>/tests/integration/dts/bundle/auto-extension/dist/cjs/index.d.cts"`,
expect(files.cjs).toMatchInlineSnapshot(
`
[
"<ROOT>/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(
`"<ROOT>/tests/integration/dts/bundle/bundle-name/dist/esm/bundleName.d.ts"`,
expect(files.esm).toMatchInlineSnapshot(
`
[
"<ROOT>/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(
`"<ROOT>/tests/integration/dts/bundle/absolute-entry/dist/esm/index.d.ts"`,
expect(files.esm).toMatchInlineSnapshot(
`
[
"<ROOT>/tests/integration/dts/bundle/absolute-entry/dist/esm/index.d.ts",
]
`,
);
});
});
5 changes: 4 additions & 1 deletion tests/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export async function getResults(
esm: 0,
cjs: 0,
umd: 0,
mf: 0,
};
let key = '';

Expand All @@ -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;
Expand Down
Loading