Skip to content

Commit 4dfb2d1

Browse files
authored
chore(build): prebundle deps in rsbuild-plugin-dts (#1227)
1 parent ec94b6c commit 4dfb2d1

File tree

8 files changed

+74
-20
lines changed

8 files changed

+74
-20
lines changed

packages/core/rslib.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default defineConfig({
4848
RSLIB_VERSION: JSON.stringify(require('./package.json').version),
4949
},
5050
},
51+
// externalize pre-bundled dependencies
5152
output: {
5253
externals: {
5354
picocolors: '../compiled/picocolors/index.js',

packages/plugin-dts/package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,30 @@
2222
"main": "./dist/index.js",
2323
"types": "./dist/index.d.ts",
2424
"files": [
25-
"dist"
25+
"dist",
26+
"compiled"
2627
],
2728
"scripts": {
2829
"build": "rslib build",
2930
"dev": "rslib build --watch",
31+
"prebundle": "prebundle",
3032
"test": "rstest"
3133
},
3234
"dependencies": {
33-
"@ast-grep/napi": "0.37.0",
34-
"magic-string": "^0.30.19",
35-
"picocolors": "1.1.1",
36-
"tinyglobby": "0.2.14",
37-
"tsconfig-paths": "^4.2.0"
35+
"@ast-grep/napi": "0.37.0"
3836
},
3937
"devDependencies": {
4038
"@microsoft/api-extractor": "^7.52.13",
4139
"@rsbuild/core": "~1.5.7",
4240
"@rslib/tsconfig": "workspace:*",
4341
"@typescript/native-preview": "7.0.0-dev.20250915.1",
42+
"magic-string": "^0.30.19",
43+
"picocolors": "1.1.1",
44+
"prebundle": "1.4.2",
4445
"rsbuild-plugin-publint": "^0.3.3",
4546
"rslib": "npm:@rslib/[email protected]",
47+
"tinyglobby": "0.2.14",
48+
"tsconfig-paths": "^4.2.0",
4649
"typescript": "^5.9.2"
4750
},
4851
"peerDependencies": {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @ts-check
2+
import { join } from 'node:path';
3+
import fs from 'fs-extra';
4+
5+
/**
6+
* Tip: please add the prebundled packages to `tsconfig.json#paths`.
7+
*/
8+
9+
/** @type {import('prebundle').Config} */
10+
export default {
11+
externals: {
12+
typescript: 'typescript',
13+
},
14+
dependencies: [
15+
'tinyglobby',
16+
'magic-string',
17+
'tsconfig-paths',
18+
{
19+
name: 'picocolors',
20+
beforeBundle({ depPath }) {
21+
const typesFile = join(depPath, 'types.ts');
22+
// Fix type bundle
23+
if (fs.existsSync(typesFile)) {
24+
fs.renameSync(typesFile, join(depPath, 'types.d.ts'));
25+
}
26+
},
27+
},
28+
],
29+
};

packages/plugin-dts/rslib.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,14 @@ export default defineConfig({
1919
index: ['./src/**'],
2020
},
2121
},
22+
// externalize pre-bundled dependencies
23+
output: {
24+
externals: {
25+
'magic-string': '../compiled/magic-string/index.js',
26+
picocolors: '../compiled/picocolors/index.js',
27+
tinyglobby: '../compiled/tinyglobby/index.js',
28+
'tsconfig-paths': 'node-commonjs ../compiled/tsconfig-paths/index.js',
29+
},
30+
},
2231
plugins: [pluginPublint()],
2332
});

packages/plugin-dts/src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ import { logger, type RsbuildConfig } from '@rsbuild/core';
1818
import MagicString from 'magic-string';
1919
import color from 'picocolors';
2020
import { convertPathToPattern, glob } from 'tinyglobby';
21-
import { createMatchPath, loadConfig, type MatchPath } from 'tsconfig-paths';
21+
import type { MatchPath } from 'tsconfig-paths';
22+
import * as tsconfigPaths from 'tsconfig-paths';
2223
import type {
2324
CompilerOptions,
2425
Diagnostic,
2526
ParsedCommandLine,
2627
} from 'typescript';
2728
import type { DtsEntry, DtsRedirect } from './index';
2829

30+
const { createMatchPath, loadConfig } = tsconfigPaths;
2931
const __filename = fileURLToPath(import.meta.url);
3032
const require = createRequire(__filename);
3133

packages/plugin-dts/tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
"isolatedDeclarations": true,
88
"composite": true,
99
"module": "ESNext",
10-
"moduleResolution": "Bundler"
10+
"moduleResolution": "Bundler",
11+
"paths": {
12+
"magic-string": ["./compiled/magic-string"],
13+
"picocolors": ["./compiled/picocolors"],
14+
"tinyglobby": ["./compiled/tinyglobby"],
15+
"tsconfig-paths": ["./compiled/tsconfig-paths"]
16+
}
1117
},
1218
"include": ["src"],
1319
"exclude": ["**/node_modules"]

pnpm-lock.yaml

Lines changed: 15 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/scripts/helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const expectPoll: (fn: () => boolean) => ReturnType<typeof expect.poll> =
7272
(fn) => {
7373
return expect.poll(fn, {
7474
intervals: [20, 30, 40, 50, 60, 70, 80, 90, 100],
75+
timeout: process.env.CI ? 10_000 : 5_000,
7576
});
7677
};
7778

0 commit comments

Comments
 (0)