From 7b7f5913ec40d2b914ca8540bf6ab23ce675508c Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Wed, 31 Jul 2024 14:46:37 +0800 Subject: [PATCH] feat: use fork to run --- e2e/cases/dts/bundle-false/rslib.config.ts | 3 - e2e/cases/dts/bundle/rslib.config.ts | 6 +- e2e/cases/dts/index.test.ts | 3 - packages/core/src/config.ts | 21 ++++-- packages/plugin-dts/modern.config.ts | 6 +- packages/plugin-dts/src/dts.ts | 85 ++++++++++++++++++++++ packages/plugin-dts/src/index.ts | 85 ++++------------------ 7 files changed, 121 insertions(+), 88 deletions(-) create mode 100644 packages/plugin-dts/src/dts.ts diff --git a/e2e/cases/dts/bundle-false/rslib.config.ts b/e2e/cases/dts/bundle-false/rslib.config.ts index 42d99d4a..6126019a 100644 --- a/e2e/cases/dts/bundle-false/rslib.config.ts +++ b/e2e/cases/dts/bundle-false/rslib.config.ts @@ -11,9 +11,6 @@ export default defineConfig({ }), generateBundleCjsConfig(__dirname, { bundle: false, - dts: { - bundle: false, - }, }), ], source: { diff --git a/e2e/cases/dts/bundle/rslib.config.ts b/e2e/cases/dts/bundle/rslib.config.ts index ae05de12..0b314814 100644 --- a/e2e/cases/dts/bundle/rslib.config.ts +++ b/e2e/cases/dts/bundle/rslib.config.ts @@ -11,11 +11,7 @@ export default defineConfig({ bundle: true, }, }), - generateBundleCjsConfig(__dirname, { - dts: { - bundle: true, - }, - }), + generateBundleCjsConfig(__dirname), ], source: { entry: { diff --git a/e2e/cases/dts/index.test.ts b/e2e/cases/dts/index.test.ts index 31b392cd..3a7f5c3a 100644 --- a/e2e/cases/dts/index.test.ts +++ b/e2e/cases/dts/index.test.ts @@ -7,9 +7,7 @@ test('dts when bundle: false', async () => { const { files } = await buildAndGetResults(fixturePath, 'dts'); expect(files.esm?.length).toBe(4); - expect(files.cjs?.length).toBe(4); expect(files.esm?.[0]!.endsWith('.d.ts')).toEqual(true); - expect(files.cjs?.[0]!.endsWith('.d.ts')).toEqual(true); }); test('dts when bundle: true', async () => { @@ -17,5 +15,4 @@ test('dts when bundle: true', async () => { const { entryFiles } = await buildAndGetResults(fixturePath, 'dts'); expect(entryFiles.esm!.endsWith('index.d.ts')).toEqual(true); - expect(entryFiles.cjs!.endsWith('index.d.ts')).toEqual(true); }); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index c450e1d4..c885c2a3 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -297,6 +297,7 @@ const getBundleConfig = (bundle = true): RsbuildConfig => { const getDefaultDtsConfig = ( libConfig: LibConfig, + entryConfig: RsbuildConfig, isWatch = false, ): RsbuildConfig => { const { dts, bundle, output } = libConfig; @@ -309,6 +310,8 @@ const getDefaultDtsConfig = ( bundle: dts?.bundle ?? bundle, distPath: dts?.distPath ?? output?.distPath?.root ?? './dist', tsconfigPath: dts?.tsconfigPath ?? 'tsconfig.json', + // TODO: temporarily use main as dts entry + entryPath: entryConfig.source?.entry?.main as string, isWatch, }), ], @@ -318,7 +321,6 @@ const getDefaultDtsConfig = ( export function convertLibConfigToRsbuildConfig( libConfig: LibConfig, configPath: string, - options?: BuildOptions, ): RsbuildConfig { const { format, autoExtension = false } = libConfig; @@ -330,14 +332,12 @@ export function convertLibConfigToRsbuildConfig( ); const syntaxConfig = getDefaultSyntaxConfig(libConfig.output?.syntax); const bundleConfig = getBundleConfig(libConfig.bundle); - const dtsConfig = getDefaultDtsConfig(libConfig, options?.watch); return mergeRsbuildConfig( formatConfig, autoExtensionConfig, syntaxConfig, bundleConfig, - dtsConfig, ); } @@ -345,6 +345,7 @@ async function postUpdateRsbuildConfig( libConfig: LibConfig, rsbuildConfig: RsbuildConfig, configPath: string, + options?: BuildOptions, ) { const defaultTargetConfig = getDefaultTargetConfig( rsbuildConfig.output?.target ?? 'web', @@ -356,7 +357,17 @@ async function postUpdateRsbuildConfig( dirname(configPath), ); - return mergeRsbuildConfig(defaultTargetConfig, defaultEntryConfig); + const defaultDtsConfig = getDefaultDtsConfig( + libConfig, + defaultEntryConfig, + options?.watch, + ); + + return mergeRsbuildConfig( + defaultTargetConfig, + defaultEntryConfig, + defaultDtsConfig, + ); } const getDefaultTargetConfig = (target: string): RsbuildConfig => { @@ -402,7 +413,6 @@ export async function composeCreateRsbuildConfig( const libConvertedRsbuildConfig = convertLibConfigToRsbuildConfig( libConfig, configPath, - options, ); const mergedRsbuildConfig = mergeRsbuildConfig( @@ -417,6 +427,7 @@ export async function composeCreateRsbuildConfig( libConfig, mergedRsbuildConfig, configPath, + options, ); // Reset some fields as they will be totally overridden by the following merge diff --git a/packages/plugin-dts/modern.config.ts b/packages/plugin-dts/modern.config.ts index 6ebe5f69..e51db3d4 100644 --- a/packages/plugin-dts/modern.config.ts +++ b/packages/plugin-dts/modern.config.ts @@ -11,19 +11,21 @@ export default defineConfig({ { format: 'cjs', target: 'es2020', - buildType: 'bundle', + buildType: 'bundleless', autoExtension: true, externals, dts: false, + shims: true, define, }, { format: 'esm', target: 'es2020', - buildType: 'bundle', + buildType: 'bundleless', autoExtension: true, externals, dts: false, + shims: true, define, }, { diff --git a/packages/plugin-dts/src/dts.ts b/packages/plugin-dts/src/dts.ts new file mode 100644 index 00000000..08889b1e --- /dev/null +++ b/packages/plugin-dts/src/dts.ts @@ -0,0 +1,85 @@ +import { basename, dirname, join, relative } from 'node:path'; +import { logger } from '@rsbuild/core'; +import type { sendData } from 'src'; +import { emitDts } from './tsc'; +import { ensureTempDeclarationDir, loadTsconfig } from './utils'; + +export async function generateDts(data: sendData) { + logger.info('Generating DTS...'); + const { options: pluginOptions } = data; + const { tsconfigPath, distPath, bundle, entryPath, isWatch } = pluginOptions; + const cwd = process.cwd(); + const configPath = tsconfigPath + ? join(cwd, tsconfigPath) + : join(cwd, 'tsconfig.json'); + const { options: rawCompilerOptions } = loadTsconfig(configPath); + const rootDir = rawCompilerOptions.rootDir ?? 'src'; + const outDir = distPath + ? distPath + : rawCompilerOptions.declarationDir || './dist'; + + const getDeclarationDir = (bundle: boolean, distPath?: string) => { + if (bundle) { + return ensureTempDeclarationDir(); + } + return distPath ? distPath : rawCompilerOptions.declarationDir; + }; + + const declarationDir = getDeclarationDir(bundle, distPath) || './dist'; + let entry = ''; + + if (bundle === true && entryPath) { + const entrySourcePath = join(cwd, entryPath); + const relativePath = relative(rootDir, dirname(entrySourcePath)); + entry = join( + declarationDir!, + relativePath, + basename(entrySourcePath), + ).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts'); + } + + const onComplete = async (isSuccess: boolean) => { + if (isSuccess && bundle === true) { + const { bundleDts } = await import('./apiExtractor'); + bundleDts({ + cwd, + outDir, + entry, + tsconfigPath, + }); + } + }; + + emitDts( + { + cwd, + configPath, + rootDir, + declarationDir, + }, + onComplete, + isWatch, + ); + + if (bundle === true && !isWatch) { + const { bundleDts } = await import('./apiExtractor'); + bundleDts({ + cwd, + outDir, + entry, + tsconfigPath, + }); + } +} + +process.on('message', async (data: sendData) => { + if (!data.options) { + return; + } + + await generateDts(data); + + if (!data.options.isWatch) { + process.exit(); + } +}); diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 1b13ab17..e137baf6 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -1,16 +1,19 @@ -import { basename, dirname, join, relative } from 'node:path'; +import { fork } from 'node:child_process'; +import { join } from 'node:path'; import type { RsbuildPlugin } from '@rsbuild/core'; -import { bundleDts } from './apiExtractor'; -import { emitDts } from './tsc'; -import { ensureTempDeclarationDir, loadTsconfig } from './utils'; export type pluginDtsOptions = { bundle: boolean; distPath?: string; tsconfigPath?: string; + entryPath?: string; isWatch?: boolean; }; +export type sendData = { + options: pluginDtsOptions; +}; + export const PLUGIN_DTS_NAME = 'rsbuild:dts'; // use ts compiler API to generate bundleless dts @@ -23,73 +26,15 @@ export const pluginDts = ( ): RsbuildPlugin => ({ name: PLUGIN_DTS_NAME, - setup(api) { - const { tsconfigPath, distPath, bundle, isWatch } = options; - - api.onBeforeBuild(async ({ environments }) => { - const cwd = process.cwd(); - const configPath = tsconfigPath - ? join(cwd, tsconfigPath) - : join(cwd, 'tsconfig.json'); - const { options: rawCompilerOptions } = loadTsconfig(configPath); - const rootDir = rawCompilerOptions.rootDir ?? 'src'; - const outDir = distPath - ? distPath - : rawCompilerOptions.declarationDir || './dist'; - - const getDeclarationDir = (bundle: boolean, distPath?: string) => { - if (bundle) { - return ensureTempDeclarationDir(); - } - return distPath ? distPath : rawCompilerOptions.declarationDir; - }; - - const declarationDir = getDeclarationDir(bundle, distPath) || './dist'; - let entry = ''; - - if (bundle === true) { - const entrySourcePath = join( - cwd, - environments.esm?.config.source.entry?.main as string, - ); - const relativePath = relative(rootDir, dirname(entrySourcePath)); - entry = join( - declarationDir!, - relativePath, - basename(entrySourcePath), - ).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts'); - } - - const onComplete = (isSuccess: boolean) => { - if (isSuccess && bundle === true) { - bundleDts({ - cwd, - outDir, - entry, - tsconfigPath, - }); - } - }; + setup() { + const childProcess = fork(join(__dirname, './dts.js'), [], { + stdio: 'inherit', + }); - emitDts( - { - cwd, - configPath, - rootDir, - declarationDir, - }, - onComplete, - isWatch, - ); + const sendData = { + options, + }; - if (bundle === true && !isWatch) { - bundleDts({ - cwd, - outDir, - entry, - tsconfigPath, - }); - } - }); + childProcess.send(sendData); }, });