From 26fb65b5cfe4b0b896404415177c465eb661525f Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 6 Aug 2024 14:46:47 +0800 Subject: [PATCH 1/4] chore: dtsPromise is only registered once --- e2e/package.json | 2 +- packages/core/package.json | 2 +- packages/plugin-dts/package.json | 5 ++- packages/plugin-dts/src/dts.ts | 6 ++-- packages/plugin-dts/src/index.ts | 54 ++++++++++++++++++++------------ packages/plugin-dts/src/tsc.ts | 20 +++++++----- packages/plugin-dts/src/utils.ts | 2 +- pnpm-lock.yaml | 46 ++++++++++++++------------- 8 files changed, 83 insertions(+), 54 deletions(-) diff --git a/e2e/package.json b/e2e/package.json index 90c2e2032..090b6587d 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -11,7 +11,7 @@ "devDependencies": { "@e2e/helper": "workspace:*", "@playwright/test": "1.43.1", - "@rsbuild/core": "1.0.1-beta.8", + "@rsbuild/core": "1.0.1-beta.10", "@rslib/core": "workspace:*", "@rslib/tsconfig": "workspace:*", "@types/fs-extra": "^11.0.4", diff --git a/packages/core/package.json b/packages/core/package.json index 4ec182d95..eb0d52e27 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -38,7 +38,7 @@ "prebundle": "prebundle" }, "dependencies": { - "@rsbuild/core": "1.0.1-beta.8", + "@rsbuild/core": "1.0.1-beta.10", "rsbuild-plugin-dts": "workspace:*" }, "devDependencies": { diff --git a/packages/plugin-dts/package.json b/packages/plugin-dts/package.json index a28fe67c2..389f1cbd2 100644 --- a/packages/plugin-dts/package.json +++ b/packages/plugin-dts/package.json @@ -29,9 +29,12 @@ "build": "modern build", "dev": "modern build --watch" }, + "dependencies": { + "picocolors": "^1.0.1" + }, "devDependencies": { "@microsoft/api-extractor": "^7.47.4", - "@rsbuild/core": "1.0.1-beta.8", + "@rsbuild/core": "1.0.1-beta.10", "@rslib/tsconfig": "workspace:*", "typescript": "^5.5.3" }, diff --git a/packages/plugin-dts/src/dts.ts b/packages/plugin-dts/src/dts.ts index 7ccc5aa05..f41f6bc77 100644 --- a/packages/plugin-dts/src/dts.ts +++ b/packages/plugin-dts/src/dts.ts @@ -1,13 +1,14 @@ import { basename, dirname, join, relative } from 'node:path'; import { logger } from '@rsbuild/core'; +import color from 'picocolors'; import type { DtsGenOptions } from 'src'; import * as ts from 'typescript'; import { emitDts } from './tsc'; import { ensureTempDeclarationDir, loadTsconfig } from './utils'; export async function generateDts(data: DtsGenOptions) { - logger.start('Generating DTS...'); - const { options: pluginOptions, cwd, isWatch } = data; + const { options: pluginOptions, cwd, isWatch, name } = data; + logger.start(`Generating DTS... ${color.gray(`(${name})`)}`); const { tsconfigPath, distPath, bundle, entryPath } = pluginOptions; const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath); if (!configPath) { @@ -60,6 +61,7 @@ export async function generateDts(data: DtsGenOptions) { emitDts( { + name, cwd, configPath, rootDir, diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 8361f133b..d5bbc9dcc 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -10,6 +10,7 @@ export type pluginDtsOptions = { }; export type DtsGenOptions = { + name: string; options: pluginDtsOptions; cwd: string; isWatch: boolean; @@ -32,37 +33,50 @@ export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({ options.distPath = options.distPath ?? config.output?.distPath?.root ?? 'dist'; - const dtsPromises: Promise[] = []; + let dtsPromise: Promise; - api.onBeforeBuild(({ isWatch }) => { - const jsExtension = extname(__filename); - const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], { - stdio: 'inherit', - }); + api.onBeforeEnvironmentCompile( + ({ isWatch, isFirstCompile, environment }) => { + if (!isFirstCompile) { + return; + } - const dtsGenOptions = { - options, - cwd: api.context.rootPath, - isWatch, - }; + const jsExtension = extname(__filename); + const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], { + stdio: 'inherit', + }); - childProcess.send(dtsGenOptions); + const dtsGenOptions = { + name: environment.name, + options, + cwd: api.context.rootPath, + isWatch, + }; - dtsPromises.push( - new Promise((resolve, reject) => { + childProcess.send(dtsGenOptions); + + dtsPromise = new Promise((resolve, reject) => { childProcess.on('message', (message) => { if (message === 'success') { resolve(); } else if (message === 'error') { - reject(new Error('Error occurred in dts generation')); + reject( + new Error( + `Error occurred in ${environment.name} dts generation`, + ), + ); } }); - }), - ); - }); + }); + }, + ); + + api.onAfterBuild(async ({ isFirstCompile }) => { + if (!isFirstCompile) { + return; + } - api.onAfterBuild(async () => { - await Promise.all(dtsPromises); + await dtsPromise; }); }, }); diff --git a/packages/plugin-dts/src/tsc.ts b/packages/plugin-dts/src/tsc.ts index 289da7b50..cec5b6dfe 100644 --- a/packages/plugin-dts/src/tsc.ts +++ b/packages/plugin-dts/src/tsc.ts @@ -1,8 +1,10 @@ import { logger } from '@rsbuild/core'; +import color from 'picocolors'; import * as ts from 'typescript'; import { getFileLoc, loadTsconfig } from './utils'; export type emitDtsOptions = { + name: string; cwd: string; configPath: string; rootDir: string; @@ -18,7 +20,7 @@ export function emitDts( const getTimeCost = () => { return `${Math.floor(Date.now() - start)}ms`; }; - const { configPath, declarationDir } = options; + const { configPath, declarationDir, name } = options; const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath); const compilerOptions = { @@ -48,7 +50,7 @@ export function emitDts( for (const diagnostic of allDiagnostics) { const fileLoc = getFileLoc(diagnostic); - const message = `${fileLoc} error TS${diagnostic.code}: ${ts.flattenDiagnosticMessageText( + const message = `${fileLoc} \nerror TS${diagnostic.code}: ${ts.flattenDiagnosticMessageText( diagnostic.messageText, host.getNewLine(), )}`; @@ -56,7 +58,9 @@ export function emitDts( } if (diagnosticMessages.length) { - logger.error('Failed to emit declaration files.'); + logger.error( + `Failed to emit declaration files. ${color.gray(`(${name})`)}`, + ); for (const message of diagnosticMessages) { logger.error(message); @@ -65,7 +69,9 @@ export function emitDts( throw new Error('DTS generation failed'); } - logger.info(`DTS generation succeeded in ${getTimeCost()}`); + logger.info( + `DTS generation succeeded in ${getTimeCost()} ${color.gray(`(${name})`)}`, + ); } else { const createProgram = ts.createSemanticDiagnosticsBuilderProgram; const formatHost: ts.FormatDiagnosticsHost = { @@ -78,7 +84,7 @@ export function emitDts( const fileLoc = getFileLoc(diagnostic); logger.error( - `${fileLoc} error TS${diagnostic.code}:`, + `${fileLoc} \nerror TS${diagnostic.code}:`, ts.flattenDiagnosticMessageText( diagnostic.messageText, formatHost.getNewLine(), @@ -92,10 +98,10 @@ export function emitDts( _options: ts.CompilerOptions, errorCount?: number, ) => { - const message = ts.flattenDiagnosticMessageText( + const message = `${ts.flattenDiagnosticMessageText( diagnostic.messageText, formatHost.getNewLine(), - ); + )} ${color.gray(`(${name})`)}`; // 6031: File change detected. Starting incremental compilation... // 6032: Starting compilation in watch mode... diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index 5e4a34185..358eeaf7f 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -37,7 +37,7 @@ export function getFileLoc(diagnostic: ts.Diagnostic): string { diagnostic.file, diagnostic.start!, ); - return `${diagnostic.file.fileName}:${line + 1}:${character + 1} - `; + return `${diagnostic.file.fileName}:${line + 1}:${character + 1} `; } return ''; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d87e80650..b8af762e6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -70,8 +70,8 @@ importers: specifier: 1.43.1 version: 1.43.1 '@rsbuild/core': - specifier: 1.0.1-beta.8 - version: 1.0.1-beta.8 + specifier: 1.0.1-beta.10 + version: 1.0.1-beta.10 '@rslib/core': specifier: workspace:* version: link:../packages/core @@ -121,7 +121,7 @@ importers: devDependencies: '@rsbuild/plugin-react': specifier: 1.0.1-beta.9 - version: 1.0.1-beta.9(@rsbuild/core@1.0.1-beta.8) + version: 1.0.1-beta.9(@rsbuild/core@1.0.1-beta.10) '@rslib/core': specifier: workspace:* version: link:../../packages/core @@ -138,8 +138,8 @@ importers: specifier: ^7 version: 7.47.2(@types/node@18.19.39) '@rsbuild/core': - specifier: 1.0.1-beta.8 - version: 1.0.1-beta.8 + specifier: 1.0.1-beta.10 + version: 1.0.1-beta.10 rsbuild-plugin-dts: specifier: workspace:* version: link:../plugin-dts @@ -173,13 +173,17 @@ importers: version: 5.5.3 packages/plugin-dts: + dependencies: + picocolors: + specifier: ^1.0.1 + version: 1.0.1 devDependencies: '@microsoft/api-extractor': specifier: ^7.47.4 version: 7.47.4(@types/node@18.19.39) '@rsbuild/core': - specifier: 1.0.1-beta.8 - version: 1.0.1-beta.8 + specifier: 1.0.1-beta.10 + version: 1.0.1-beta.10 '@rslib/tsconfig': specifier: workspace:* version: link:../../scripts/tsconfig @@ -1192,8 +1196,8 @@ packages: cpu: [x64] os: [win32] - '@rsbuild/core@1.0.1-beta.8': - resolution: {integrity: sha512-SERxv+2eyS6ihZZ+QqhFaMXVkf3cTNdn2H58AyHLxrx9+T/1DZP2LRLWAcbfgjr8lNTkv9nWcxk0d97pPThLGg==} + '@rsbuild/core@1.0.1-beta.10': + resolution: {integrity: sha512-F2xVOiUXmv9PhtgXnxtelE2Ay6gwsU2MjLur+PNF63QJB4VhV1KMWedt0cEw6m3KR6/n6laWY3PXv8TxGA99iA==} engines: {node: '>=16.7.0'} hasBin: true @@ -1263,8 +1267,8 @@ packages: resolution: {integrity: sha512-emikuiIbELsdO28IxMgjkcw8sovk9/BF+L7V3Ix75NLFc2+5MZ8LGteUFrhxfuXr/7eFY1eje858ZmeRPNr4fw==} engines: {node: '>=16.0.0'} - '@rspack/lite-tapable@1.0.0-beta.1': - resolution: {integrity: sha512-r4xtbJp6QhW6A1twkgTP0UQkPC9cOT3sFjjjlx22j/q669HJRz+CVTlVcNxPomK7Q3Kg6dVsyv16MjGRl/fl5g==} + '@rspack/lite-tapable@1.0.0-beta.2': + resolution: {integrity: sha512-pDcrOH7tejguIxzcMUnCb5FCrz9GWtAnS3dVG6O/P4l7OWtcOBI3F1TQUsEy1u2WpMpH9ruPHRCSIliBfUymLg==} engines: {node: '>=16.0.0'} '@rspack/plugin-react-refresh@1.0.0-beta.1': @@ -1561,8 +1565,8 @@ packages: caniuse-lite@1.0.30001641: resolution: {integrity: sha512-Phv5thgl67bHYo1TtMY/MurjkHhV4EDaCosezRXgZ8jzA/Ub+wjxAvbGvjoFENStinwi5kCyOYV3mi5tOGykwA==} - caniuse-lite@1.0.30001643: - resolution: {integrity: sha512-ERgWGNleEilSrHM6iUz/zJNSQTP8Mr21wDWpdgvRwcTXGAq6jMtOUPP4dqFPTdKqZ2wKTdtB+uucZ3MRpAUSmg==} + caniuse-lite@1.0.30001649: + resolution: {integrity: sha512-fJegqZZ0ZX8HOWr6rcafGr72+xcgJKI9oWfDW5DrD7ExUtgZC7a7R7ZYmZqplh7XDocFdGeIFn7roAxhOeYrPQ==} chai@5.1.1: resolution: {integrity: sha512-pT1ZgP8rPNqUgieVaEY+ryQr6Q4HXNg8Ei9UnLUrjN4IA7dvQC5JB+/kxVcPNDHyBcc/26CXPkbNzq3qwrOEKA==} @@ -4085,19 +4089,19 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.18.1': optional: true - '@rsbuild/core@1.0.1-beta.8': + '@rsbuild/core@1.0.1-beta.10': dependencies: '@rspack/core': '@rspack/core-canary@1.0.0-canary-338cfbe-20240731183605(@swc/helpers@0.5.11)' - '@rspack/lite-tapable': 1.0.0-beta.1 + '@rspack/lite-tapable': 1.0.0-beta.2 '@swc/helpers': 0.5.11 - caniuse-lite: 1.0.30001643 + caniuse-lite: 1.0.30001649 core-js: 3.37.1 optionalDependencies: fsevents: 2.3.3 - '@rsbuild/plugin-react@1.0.1-beta.9(@rsbuild/core@1.0.1-beta.8)': + '@rsbuild/plugin-react@1.0.1-beta.9(@rsbuild/core@1.0.1-beta.10)': dependencies: - '@rsbuild/core': 1.0.1-beta.8 + '@rsbuild/core': 1.0.1-beta.10 '@rspack/plugin-react-refresh': 1.0.0-beta.1(react-refresh@0.14.2) react-refresh: 0.14.2 @@ -4145,13 +4149,13 @@ snapshots: '@module-federation/runtime-tools': 0.2.3 '@rspack/binding': '@rspack/binding-canary@1.0.0-canary-338cfbe-20240731183605' '@rspack/lite-tapable': '@rspack/lite-tapable-canary@1.0.0-canary-338cfbe-20240731183605' - caniuse-lite: 1.0.30001643 + caniuse-lite: 1.0.30001649 optionalDependencies: '@swc/helpers': 0.5.11 '@rspack/lite-tapable-canary@1.0.0-canary-338cfbe-20240731183605': {} - '@rspack/lite-tapable@1.0.0-beta.1': {} + '@rspack/lite-tapable@1.0.0-beta.2': {} '@rspack/plugin-react-refresh@1.0.0-beta.1(react-refresh@0.14.2)': dependencies: @@ -4506,7 +4510,7 @@ snapshots: caniuse-lite@1.0.30001641: {} - caniuse-lite@1.0.30001643: {} + caniuse-lite@1.0.30001649: {} chai@5.1.1: dependencies: From bb85962b349a89f675c5dca02b3ffac4ee20d009 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 6 Aug 2024 14:50:14 +0800 Subject: [PATCH 2/4] fix: update --- packages/plugin-dts/src/tsc.ts | 4 ++-- packages/plugin-dts/src/utils.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/plugin-dts/src/tsc.ts b/packages/plugin-dts/src/tsc.ts index cec5b6dfe..7278c50e8 100644 --- a/packages/plugin-dts/src/tsc.ts +++ b/packages/plugin-dts/src/tsc.ts @@ -50,7 +50,7 @@ export function emitDts( for (const diagnostic of allDiagnostics) { const fileLoc = getFileLoc(diagnostic); - const message = `${fileLoc} \nerror TS${diagnostic.code}: ${ts.flattenDiagnosticMessageText( + const message = `${fileLoc} error TS${diagnostic.code}: ${ts.flattenDiagnosticMessageText( diagnostic.messageText, host.getNewLine(), )}`; @@ -84,7 +84,7 @@ export function emitDts( const fileLoc = getFileLoc(diagnostic); logger.error( - `${fileLoc} \nerror TS${diagnostic.code}:`, + `${fileLoc} error TS${diagnostic.code}:`, ts.flattenDiagnosticMessageText( diagnostic.messageText, formatHost.getNewLine(), diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index 358eeaf7f..5e4a34185 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -37,7 +37,7 @@ export function getFileLoc(diagnostic: ts.Diagnostic): string { diagnostic.file, diagnostic.start!, ); - return `${diagnostic.file.fileName}:${line + 1}:${character + 1} `; + return `${diagnostic.file.fileName}:${line + 1}:${character + 1} - `; } return ''; From 5ac93085048ecc036135a904ba3d1ee30221cd1f Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 6 Aug 2024 15:00:02 +0800 Subject: [PATCH 3/4] fix: deps --- packages/plugin-dts/package.json | 2 +- pnpm-lock.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugin-dts/package.json b/packages/plugin-dts/package.json index 389f1cbd2..7e3523e03 100644 --- a/packages/plugin-dts/package.json +++ b/packages/plugin-dts/package.json @@ -30,7 +30,7 @@ "dev": "modern build --watch" }, "dependencies": { - "picocolors": "^1.0.1" + "picocolors": "1.0.1" }, "devDependencies": { "@microsoft/api-extractor": "^7.47.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b8af762e6..6f1baefb9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -175,7 +175,7 @@ importers: packages/plugin-dts: dependencies: picocolors: - specifier: ^1.0.1 + specifier: 1.0.1 version: 1.0.1 devDependencies: '@microsoft/api-extractor': From f34a8f6a0ab2f41d39540bbd7de44cd0a8429c66 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 6 Aug 2024 15:25:23 +0800 Subject: [PATCH 4/4] fix: support multiple environment --- packages/plugin-dts/src/index.ts | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index d5bbc9dcc..92ee586e0 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -33,7 +33,7 @@ export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({ options.distPath = options.distPath ?? config.output?.distPath?.root ?? 'dist'; - let dtsPromise: Promise; + const dtsPromises: Promise[] = []; api.onBeforeEnvironmentCompile( ({ isWatch, isFirstCompile, environment }) => { @@ -55,19 +55,21 @@ export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({ childProcess.send(dtsGenOptions); - dtsPromise = new Promise((resolve, reject) => { - childProcess.on('message', (message) => { - if (message === 'success') { - resolve(); - } else if (message === 'error') { - reject( - new Error( - `Error occurred in ${environment.name} dts generation`, - ), - ); - } - }); - }); + dtsPromises.push( + new Promise((resolve, reject) => { + childProcess.on('message', (message) => { + if (message === 'success') { + resolve(); + } else if (message === 'error') { + reject( + new Error( + `Error occurred in ${environment.name} dts generation`, + ), + ); + } + }); + }), + ); }, ); @@ -76,7 +78,7 @@ export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({ return; } - await dtsPromise; + await Promise.all(dtsPromises); }); }, });