diff --git a/packages/core/rslib.config.ts b/packages/core/rslib.config.ts index 089e34f73..e48d52b08 100644 --- a/packages/core/rslib.config.ts +++ b/packages/core/rslib.config.ts @@ -48,6 +48,7 @@ export default defineConfig({ RSLIB_VERSION: JSON.stringify(require('./package.json').version), }, }, + // externalize pre-bundled dependencies output: { externals: { picocolors: '../compiled/picocolors/index.js', diff --git a/packages/plugin-dts/package.json b/packages/plugin-dts/package.json index e0752e9c5..c8e70eb5b 100644 --- a/packages/plugin-dts/package.json +++ b/packages/plugin-dts/package.json @@ -22,27 +22,30 @@ "main": "./dist/index.js", "types": "./dist/index.d.ts", "files": [ - "dist" + "dist", + "compiled" ], "scripts": { "build": "rslib build", "dev": "rslib build --watch", + "prebundle": "prebundle", "test": "rstest" }, "dependencies": { - "@ast-grep/napi": "0.37.0", - "magic-string": "^0.30.19", - "picocolors": "1.1.1", - "tinyglobby": "0.2.14", - "tsconfig-paths": "^4.2.0" + "@ast-grep/napi": "0.37.0" }, "devDependencies": { "@microsoft/api-extractor": "^7.52.13", "@rsbuild/core": "~1.5.7", "@rslib/tsconfig": "workspace:*", "@typescript/native-preview": "7.0.0-dev.20250915.1", + "magic-string": "^0.30.19", + "picocolors": "1.1.1", + "prebundle": "1.4.2", "rsbuild-plugin-publint": "^0.3.3", "rslib": "npm:@rslib/core@0.13.2", + "tinyglobby": "0.2.14", + "tsconfig-paths": "^4.2.0", "typescript": "^5.9.2" }, "peerDependencies": { diff --git a/packages/plugin-dts/prebundle.config.mjs b/packages/plugin-dts/prebundle.config.mjs new file mode 100644 index 000000000..0057b0592 --- /dev/null +++ b/packages/plugin-dts/prebundle.config.mjs @@ -0,0 +1,29 @@ +// @ts-check +import { join } from 'node:path'; +import fs from 'fs-extra'; + +/** + * Tip: please add the prebundled packages to `tsconfig.json#paths`. + */ + +/** @type {import('prebundle').Config} */ +export default { + externals: { + typescript: 'typescript', + }, + dependencies: [ + 'tinyglobby', + 'magic-string', + 'tsconfig-paths', + { + name: 'picocolors', + beforeBundle({ depPath }) { + const typesFile = join(depPath, 'types.ts'); + // Fix type bundle + if (fs.existsSync(typesFile)) { + fs.renameSync(typesFile, join(depPath, 'types.d.ts')); + } + }, + }, + ], +}; diff --git a/packages/plugin-dts/rslib.config.ts b/packages/plugin-dts/rslib.config.ts index 447a611e0..51b31f26d 100644 --- a/packages/plugin-dts/rslib.config.ts +++ b/packages/plugin-dts/rslib.config.ts @@ -19,5 +19,14 @@ export default defineConfig({ index: ['./src/**'], }, }, + // externalize pre-bundled dependencies + output: { + externals: { + 'magic-string': '../compiled/magic-string/index.js', + picocolors: '../compiled/picocolors/index.js', + tinyglobby: '../compiled/tinyglobby/index.js', + 'tsconfig-paths': 'node-commonjs ../compiled/tsconfig-paths/index.js', + }, + }, plugins: [pluginPublint()], }); diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index c08c2b136..d35e0928c 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -18,7 +18,8 @@ import { logger, type RsbuildConfig } from '@rsbuild/core'; import MagicString from 'magic-string'; import color from 'picocolors'; import { convertPathToPattern, glob } from 'tinyglobby'; -import { createMatchPath, loadConfig, type MatchPath } from 'tsconfig-paths'; +import type { MatchPath } from 'tsconfig-paths'; +import * as tsconfigPaths from 'tsconfig-paths'; import type { CompilerOptions, Diagnostic, @@ -26,6 +27,7 @@ import type { } from 'typescript'; import type { DtsEntry, DtsRedirect } from './index'; +const { createMatchPath, loadConfig } = tsconfigPaths; const __filename = fileURLToPath(import.meta.url); const require = createRequire(__filename); diff --git a/packages/plugin-dts/tsconfig.json b/packages/plugin-dts/tsconfig.json index ea221a691..954e69b40 100644 --- a/packages/plugin-dts/tsconfig.json +++ b/packages/plugin-dts/tsconfig.json @@ -7,7 +7,13 @@ "isolatedDeclarations": true, "composite": true, "module": "ESNext", - "moduleResolution": "Bundler" + "moduleResolution": "Bundler", + "paths": { + "magic-string": ["./compiled/magic-string"], + "picocolors": ["./compiled/picocolors"], + "tinyglobby": ["./compiled/tinyglobby"], + "tsconfig-paths": ["./compiled/tsconfig-paths"] + } }, "include": ["src"], "exclude": ["**/node_modules"] diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e3aa2d3fc..4a0bcd04e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -428,18 +428,6 @@ importers: '@ast-grep/napi': specifier: 0.37.0 version: 0.37.0 - magic-string: - specifier: ^0.30.19 - version: 0.30.19 - picocolors: - specifier: 1.1.1 - version: 1.1.1 - tinyglobby: - specifier: 0.2.14 - version: 0.2.14 - tsconfig-paths: - specifier: ^4.2.0 - version: 4.2.0 devDependencies: '@microsoft/api-extractor': specifier: ^7.52.13 @@ -453,12 +441,27 @@ importers: '@typescript/native-preview': specifier: 7.0.0-dev.20250915.1 version: 7.0.0-dev.20250915.1 + magic-string: + specifier: ^0.30.19 + version: 0.30.19 + picocolors: + specifier: 1.1.1 + version: 1.1.1 + prebundle: + specifier: 1.4.2 + version: 1.4.2(typescript@5.9.2) rsbuild-plugin-publint: specifier: ^0.3.3 version: 0.3.3(@rsbuild/core@1.5.7) rslib: specifier: npm:@rslib/core@0.13.2 version: '@rslib/core@0.13.2(@microsoft/api-extractor@7.52.13(@types/node@22.18.4))(@typescript/native-preview@7.0.0-dev.20250915.1)(typescript@5.9.2)' + tinyglobby: + specifier: 0.2.14 + version: 0.2.14 + tsconfig-paths: + specifier: ^4.2.0 + version: 4.2.0 typescript: specifier: ^5.9.2 version: 5.9.2 diff --git a/tests/scripts/helper.ts b/tests/scripts/helper.ts index 92f237a01..bed692b27 100644 --- a/tests/scripts/helper.ts +++ b/tests/scripts/helper.ts @@ -72,6 +72,7 @@ export const expectPoll: (fn: () => boolean) => ReturnType = (fn) => { return expect.poll(fn, { intervals: [20, 30, 40, 50, 60, 70, 80, 90, 100], + timeout: process.env.CI ? 10_000 : 5_000, }); };