Skip to content

Commit 1cfc8b9

Browse files
feat: support set dts: true and dts default to bundleless (#325)
Co-authored-by: Wei <[email protected]>
1 parent c977b98 commit 1cfc8b9

File tree

11 files changed

+146
-25
lines changed

11 files changed

+146
-25
lines changed

packages/core/src/config.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,15 +906,25 @@ const composeDtsConfig = async (
906906
libConfig: LibConfig,
907907
dtsExtension: string,
908908
): Promise<RsbuildConfig> => {
909-
const { dts, bundle, output, autoExternal, banner, footer } = libConfig;
909+
const { output, autoExternal, banner, footer } = libConfig;
910+
911+
let { dts } = libConfig;
910912

911913
if (dts === false || dts === undefined) return {};
912914

915+
// DTS default to bundleless whether js is bundle or not
916+
if (dts === true) {
917+
dts = {
918+
bundle: false,
919+
};
920+
}
921+
913922
const { pluginDts } = await import('rsbuild-plugin-dts');
914923
return {
915924
plugins: [
916925
pluginDts({
917-
bundle: dts?.bundle ?? bundle,
926+
// Only setting ⁠dts.bundle to true will generate the bundled d.ts.
927+
bundle: dts?.bundle ?? false,
918928
distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
919929
abortOnError: dts?.abortOnError ?? true,
920930
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',

packages/core/src/types/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type Dts =
3232
| (Pick<PluginDtsOptions, 'bundle' | 'distPath' | 'abortOnError'> & {
3333
autoExtension?: boolean;
3434
})
35-
| false;
35+
| boolean;
3636

3737
export type AutoExternal =
3838
| boolean

packages/create-rslib/template-node-dual-ts/rslib.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig({
55
{
66
format: 'esm',
77
syntax: 'es2021',
8-
dts: {},
8+
dts: true,
99
},
1010
{
1111
format: 'cjs',

packages/create-rslib/template-node-esm-ts/rslib.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig({
55
{
66
format: 'esm',
77
syntax: 'es2021',
8-
dts: {},
8+
dts: true,
99
},
1010
],
1111
output: { target: 'node' },

pnpm-lock.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dts-true-bundle-false-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
bundle: false,
8+
dts: true,
9+
}),
10+
generateBundleCjsConfig(),
11+
],
12+
source: {
13+
entry: {
14+
index: '../__fixtures__/src/index.ts',
15+
},
16+
tsconfigPath: '../__fixtures__/tsconfig.json',
17+
},
18+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dts-true-bundle-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
dts: true,
8+
}),
9+
generateBundleCjsConfig(),
10+
],
11+
source: {
12+
entry: {
13+
index: '../__fixtures__/src/index.ts',
14+
},
15+
tsconfigPath: '../__fixtures__/tsconfig.json',
16+
},
17+
});

tests/integration/dts/index.test.ts

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ describe('dts when bundle: false', () => {
3838
expect(files.esm).toMatchInlineSnapshot('undefined');
3939
});
4040

41+
test('dts true', async () => {
42+
const fixturePath = join(__dirname, 'bundle-false', 'true');
43+
const { files } = await buildAndGetResults({ fixturePath, type: 'dts' });
44+
45+
expect(files.esm).toMatchInlineSnapshot(`
46+
[
47+
"<ROOT>/tests/integration/dts/bundle-false/true/dist/esm/index.d.ts",
48+
"<ROOT>/tests/integration/dts/bundle-false/true/dist/esm/sum.d.ts",
49+
"<ROOT>/tests/integration/dts/bundle-false/true/dist/esm/utils/numbers.d.ts",
50+
"<ROOT>/tests/integration/dts/bundle-false/true/dist/esm/utils/strings.d.ts",
51+
]
52+
`);
53+
});
54+
4155
test('distPath', async () => {
4256
const fixturePath = join(__dirname, 'bundle-false', 'dist-path');
4357
const { files } = await buildAndGetResults({ fixturePath, type: 'dts' });
@@ -80,41 +94,72 @@ describe('dts when bundle: false', () => {
8094
describe('dts when bundle: true', () => {
8195
test('basic', async () => {
8296
const fixturePath = join(__dirname, 'bundle', 'basic');
83-
const { entryFiles, entries } = await buildAndGetResults({
97+
const { files, entries } = await buildAndGetResults({
8498
fixturePath,
8599
type: 'dts',
86100
});
87101

88-
expect(entryFiles.esm).toMatchInlineSnapshot(
89-
`"<ROOT>/tests/integration/dts/bundle/basic/dist/esm/index.d.ts"`,
102+
expect(files.esm).toMatchInlineSnapshot(
103+
`
104+
[
105+
"<ROOT>/tests/integration/dts/bundle/basic/dist/esm/index.d.ts",
106+
]
107+
`,
90108
);
91109

92-
expect(entryFiles.cjs).toMatchInlineSnapshot(
93-
`"<ROOT>/tests/integration/dts/bundle/basic/dist/cjs/index.d.ts"`,
110+
expect(files.cjs).toMatchInlineSnapshot(
111+
`
112+
[
113+
"<ROOT>/tests/integration/dts/bundle/basic/dist/cjs/index.d.ts",
114+
]
115+
`,
94116
);
95117

96118
expect(entries).toMatchSnapshot();
97119
});
98120

99121
test('dts false', async () => {
100122
const fixturePath = join(__dirname, 'bundle', 'false');
101-
const { entryFiles } = await buildAndGetResults({
123+
const { files } = await buildAndGetResults({
124+
fixturePath,
125+
type: 'dts',
126+
});
127+
128+
expect(files.esm).toMatchInlineSnapshot('undefined');
129+
});
130+
131+
test('dts true', async () => {
132+
const fixturePath = join(__dirname, 'bundle', 'true');
133+
const { files } = await buildAndGetResults({
102134
fixturePath,
103135
type: 'dts',
104136
});
105137

106-
expect(entryFiles.esm).toMatchInlineSnapshot('undefined');
138+
expect(files.esm).toMatchInlineSnapshot(
139+
`
140+
[
141+
"<ROOT>/tests/integration/dts/bundle/true/dist/esm/index.d.ts",
142+
"<ROOT>/tests/integration/dts/bundle/true/dist/esm/sum.d.ts",
143+
"<ROOT>/tests/integration/dts/bundle/true/dist/esm/utils/numbers.d.ts",
144+
"<ROOT>/tests/integration/dts/bundle/true/dist/esm/utils/strings.d.ts",
145+
]
146+
`,
147+
);
107148
});
108149

109150
test('distPath', async () => {
110151
const fixturePath = join(__dirname, 'bundle', 'dist-path');
111-
const { entryFiles } = await buildAndGetResults({
152+
const { files } = await buildAndGetResults({
112153
fixturePath,
113154
type: 'dts',
114155
});
115156

116-
expect(entryFiles.esm).toMatchInlineSnapshot(
117-
`"<ROOT>/tests/integration/dts/bundle/dist-path/dist/custom/index.d.ts"`,
157+
expect(files.esm).toMatchInlineSnapshot(
158+
`
159+
[
160+
"<ROOT>/tests/integration/dts/bundle/dist-path/dist/custom/index.d.ts",
161+
]
162+
`,
118163
);
119164
});
120165

@@ -130,37 +175,49 @@ describe('dts when bundle: true', () => {
130175

131176
test('autoExtension: true', async () => {
132177
const fixturePath = join(__dirname, 'bundle', 'auto-extension');
133-
const { entryFiles } = await buildAndGetResults({
178+
const { files } = await buildAndGetResults({
134179
fixturePath,
135180
type: 'dts',
136181
});
137182

138-
expect(entryFiles.cjs).toMatchInlineSnapshot(
139-
`"<ROOT>/tests/integration/dts/bundle/auto-extension/dist/cjs/index.d.cts"`,
183+
expect(files.cjs).toMatchInlineSnapshot(
184+
`
185+
[
186+
"<ROOT>/tests/integration/dts/bundle/auto-extension/dist/cjs/index.d.cts",
187+
]
188+
`,
140189
);
141190
});
142191

143192
test('bundleName -- set source.entry', async () => {
144193
const fixturePath = join(__dirname, 'bundle', 'bundle-name');
145-
const { entryFiles } = await buildAndGetResults({
194+
const { files } = await buildAndGetResults({
146195
fixturePath,
147196
type: 'dts',
148197
});
149198

150-
expect(entryFiles.esm).toMatchInlineSnapshot(
151-
`"<ROOT>/tests/integration/dts/bundle/bundle-name/dist/esm/bundleName.d.ts"`,
199+
expect(files.esm).toMatchInlineSnapshot(
200+
`
201+
[
202+
"<ROOT>/tests/integration/dts/bundle/bundle-name/dist/esm/bundleName.d.ts",
203+
]
204+
`,
152205
);
153206
});
154207

155208
test('entry is an absolute path', async () => {
156209
const fixturePath = join(__dirname, 'bundle', 'absolute-entry');
157-
const { entryFiles } = await buildAndGetResults({
210+
const { files } = await buildAndGetResults({
158211
fixturePath,
159212
type: 'dts',
160213
});
161214

162-
expect(entryFiles.esm).toMatchInlineSnapshot(
163-
`"<ROOT>/tests/integration/dts/bundle/absolute-entry/dist/esm/index.d.ts"`,
215+
expect(files.esm).toMatchInlineSnapshot(
216+
`
217+
[
218+
"<ROOT>/tests/integration/dts/bundle/absolute-entry/dist/esm/index.d.ts",
219+
]
220+
`,
164221
);
165222
});
166223
});

0 commit comments

Comments
 (0)