Skip to content

Commit 9d40d43

Browse files
authored
feat: support autoExternal for dts files (#80)
1 parent f533fbb commit 9d40d43

File tree

18 files changed

+458
-27
lines changed

18 files changed

+458
-27
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "auto-external-default-test",
33
"dependencies": {
4+
"ora": "8.0.1",
45
"react": "^18.3.1"
56
}
67
}

e2e/cases/auto-external/default/rslib.config.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@ import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
22
import { defineConfig } from '@rslib/core';
33

44
export default defineConfig({
5-
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
5+
lib: [
6+
generateBundleEsmConfig(__dirname, {
7+
dts: {
8+
bundle: true,
9+
},
10+
}),
11+
generateBundleCjsConfig(__dirname, {
12+
dts: {
13+
bundle: true,
14+
},
15+
}),
16+
],
617
source: {
718
entry: {
8-
main: '../__fixtures__/src/index.ts',
19+
main: './src/index.ts',
920
},
1021
},
1122
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { oraPromise } from 'ora';
2+
import React from 'react';
3+
4+
export type { oraPromise };
5+
export const foo = () => {
6+
return React.version;
7+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react-jsx",
4+
"strict": true,
5+
"esModuleInterop": true
6+
},
7+
"include": ["src/**/*"]
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "auto-external-false-test",
33
"dependencies": {
4+
"ora": "8.0.1",
45
"react": "^18.3.1"
56
}
67
}

e2e/cases/auto-external/false/rslib.config.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@ import { defineConfig } from '@rslib/core';
33

44
export default defineConfig({
55
lib: [
6-
{
7-
...generateBundleEsmConfig(__dirname),
6+
generateBundleEsmConfig(__dirname, {
87
autoExternal: false,
9-
},
10-
{
11-
...generateBundleCjsConfig(__dirname),
8+
dts: {
9+
bundle: true,
10+
},
11+
}),
12+
generateBundleCjsConfig(__dirname, {
1213
autoExternal: false,
13-
},
14+
dts: {
15+
bundle: true,
16+
},
17+
}),
1418
],
1519
source: {
1620
entry: {
17-
main: '../__fixtures__/src/index.ts',
21+
main: './src/index.ts',
1822
},
1923
},
2024
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { oraPromise } from 'ora';
2+
import React from 'react';
3+
4+
export type { oraPromise };
5+
export const foo = () => {
6+
return React.version;
7+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react-jsx",
4+
"strict": true,
5+
"esModuleInterop": true
6+
},
7+
"include": ["src/**/*"]
8+
}

e2e/cases/auto-external/index.test.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import { expect, test } from 'vitest';
44

55
test('auto external default should works', async () => {
66
const fixturePath = join(__dirname, 'default');
7-
const { entries } = await buildAndGetResults(fixturePath);
7+
const { js, dts } = await buildAndGetResults(fixturePath, 'all');
88

9-
expect(entries.esm).toContain(
9+
expect(js.entries.esm).toContain(
1010
'import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react"',
1111
);
1212

13-
expect(entries.cjs).toContain(
13+
expect(js!.entries.cjs).toContain(
1414
'var external_react_namespaceObject = require("react");',
1515
);
16+
17+
// dts should externalized
18+
expect(dts.entries.esm).toContain("import type { oraPromise } from 'ora';");
19+
expect(dts.entries.cjs).toContain("import type { oraPromise } from 'ora';");
1620
});
1721

1822
test('auto external sub path should works', async () => {
@@ -36,15 +40,20 @@ test('auto external sub path should works', async () => {
3640

3741
test('auto external false should works', async () => {
3842
const fixturePath = join(__dirname, 'false');
39-
const { entries } = await buildAndGetResults(fixturePath);
43+
const { js, dts } = await buildAndGetResults(fixturePath, 'all');
4044

41-
expect(entries.esm).not.toContain(
45+
expect(js.entries.esm).not.toContain(
4246
'import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react"',
4347
);
4448

45-
expect(entries.cjs).not.toContain(
49+
expect(js.entries.cjs).not.toContain(
4650
'var external_react_namespaceObject = require("react");',
4751
);
52+
53+
// dts should bundled
54+
expect(dts.entries.esm).toContain('export declare function oraPromise');
55+
56+
expect(dts.entries.cjs).toContain('export declare function oraPromise');
4857
});
4958

5059
test('externals should overrides auto external', async () => {

e2e/scripts/shared.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,61 @@ export async function getResults(
102102
};
103103
}
104104

105-
export const buildAndGetResults = async (
106-
fixturePath: string,
107-
type: 'js' | 'dts' = 'js',
108-
): Promise<{
105+
type BuildResult = {
109106
contents: Record<string, Record<string, string>>;
110107
files: Record<string, string[]>;
111108
entries: Record<string, string>;
112109
entryFiles: Record<string, string>;
113110
rspackConfig: InspectConfigResult['origin']['bundlerConfigs'];
114111
rsbuildConfig: InspectConfigResult['origin']['rsbuildConfig'];
115112
isSuccess: boolean;
116-
}> => {
113+
};
114+
115+
export async function buildAndGetResults(
116+
fixturePath: string,
117+
type: 'all',
118+
): Promise<{
119+
js: BuildResult;
120+
dts: BuildResult;
121+
}>;
122+
export async function buildAndGetResults(
123+
fixturePath: string,
124+
type?: 'js' | 'dts',
125+
): Promise<BuildResult>;
126+
export async function buildAndGetResults(
127+
fixturePath: string,
128+
type: 'js' | 'dts' | 'all' = 'js',
129+
) {
117130
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
118131
process.chdir(fixturePath);
119132
const rsbuildInstance = await build(rslibConfig);
120133
const {
121134
origin: { bundlerConfigs, rsbuildConfig },
122135
} = await rsbuildInstance.inspectConfig({ verbose: true });
136+
if (type === 'all') {
137+
const jsResults = await getResults(rslibConfig, fixturePath, 'js');
138+
const dtsResults = await getResults(rslibConfig, fixturePath, 'dts');
139+
return {
140+
js: {
141+
contents: jsResults.contents,
142+
files: jsResults.files,
143+
entries: jsResults.entries,
144+
entryFiles: jsResults.entryFiles,
145+
rspackConfig: bundlerConfigs,
146+
rsbuildConfig: rsbuildConfig,
147+
isSuccess: Boolean(rsbuildInstance),
148+
},
149+
dts: {
150+
contents: dtsResults.contents,
151+
files: dtsResults.files,
152+
entries: dtsResults.entries,
153+
entryFiles: dtsResults.entryFiles,
154+
rspackConfig: bundlerConfigs,
155+
rsbuildConfig: rsbuildConfig,
156+
isSuccess: Boolean(rsbuildInstance),
157+
},
158+
};
159+
}
123160

124161
const results = await getResults(rslibConfig, fixturePath, type);
125162
return {
@@ -131,4 +168,4 @@ export const buildAndGetResults = async (
131168
rsbuildConfig: rsbuildConfig,
132169
isSuccess: Boolean(rsbuildInstance),
133170
};
134-
};
171+
}

0 commit comments

Comments
 (0)