From 7879b472a5c5e093d61690f3a6fed0089a9afe31 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 1 Aug 2024 15:37:50 +0800 Subject: [PATCH] chore: enhance log --- packages/plugin-dts/src/apiExtractor.ts | 10 +- packages/plugin-dts/src/tsc.ts | 154 +++++++++--------------- packages/plugin-dts/src/utils.ts | 12 ++ 3 files changed, 72 insertions(+), 104 deletions(-) diff --git a/packages/plugin-dts/src/apiExtractor.ts b/packages/plugin-dts/src/apiExtractor.ts index e6fa4f808..3a53628c5 100644 --- a/packages/plugin-dts/src/apiExtractor.ts +++ b/packages/plugin-dts/src/apiExtractor.ts @@ -20,13 +20,14 @@ export function bundleDts(options: BundleOptions) { entry = 'index.d.ts', tsconfigPath = 'tsconfig.json', } = options; + const untrimmedFilePath = join(cwd, relative(cwd, outDir), 'index.d.ts'); const internalConfig = { mainEntryPointFilePath: entry, // TODO: use !externals // bundledPackages: [], dtsRollup: { enabled: true, - untrimmedFilePath: join(cwd, relative(cwd, outDir), 'index.d.ts'), + untrimmedFilePath, }, compiler: { tsconfigFilePath: join(cwd, tsconfigPath), @@ -42,12 +43,13 @@ export function bundleDts(options: BundleOptions) { const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, { localBuild: true, - showVerboseMessages: true, }); if (!extractorResult.succeeded) { - throw new Error('API Extractor error'); + throw new Error('API Extractor rollup error'); } - logger.info('API Extractor rollup succeeded\n'); + logger.info( + `API Extractor writing package typings succeeded: ${untrimmedFilePath}`, + ); } diff --git a/packages/plugin-dts/src/tsc.ts b/packages/plugin-dts/src/tsc.ts index 88060fc2d..fc718e4ed 100644 --- a/packages/plugin-dts/src/tsc.ts +++ b/packages/plugin-dts/src/tsc.ts @@ -1,6 +1,6 @@ import { logger } from '@rsbuild/core'; import * as ts from 'typescript'; -import { loadTsconfig } from './utils'; +import { getFileLoc, loadTsconfig } from './utils'; export type emitDtsOptions = { cwd: string; @@ -43,31 +43,21 @@ export function emitDts( const diagnosticMessages: string[] = []; for (const diagnostic of allDiagnostics) { - if (diagnostic.file) { - const { line, character } = ts.getLineAndCharacterOfPosition( - diagnostic.file, - diagnostic.start!, - ); - const message = ts.flattenDiagnosticMessageText( - diagnostic.messageText, - '\n', - ); - diagnosticMessages.push( - `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`, - ); - } else { - const message = ts.flattenDiagnosticMessageText( - diagnostic.messageText, - '\n', - ); - diagnosticMessages.push(message); - } + const fileLoc = getFileLoc(diagnostic); + const message = `${fileLoc} error TS${diagnostic.code}: ${ts.flattenDiagnosticMessageText( + diagnostic.messageText, + host.getNewLine(), + )}`; + diagnosticMessages.push(message); } if (diagnosticMessages.length) { - logger.error( - `Failed to emit declaration files.\n${diagnosticMessages.join('\n')}\n`, - ); + logger.error('Failed to emit declaration files.'); + + for (const message of diagnosticMessages) { + logger.error(message); + } + throw new Error('TypeScript compilation failed'); } @@ -80,30 +70,49 @@ export function emitDts( getNewLine: () => ts.sys.newLine, }; - const reportDiagnostic = (_diagnostic: ts.Diagnostic) => { - // console.error( - // 'Error', - // diagnostic.code, - // ':', - // ts.flattenDiagnosticMessageText( - // diagnostic.messageText, - // formatHost.getNewLine(), - // ), - // ); + const reportDiagnostic = (diagnostic: ts.Diagnostic) => { + const fileLoc = getFileLoc(diagnostic); + + logger.error( + `${fileLoc} error TS${diagnostic.code}:`, + ts.flattenDiagnosticMessageText( + diagnostic.messageText, + formatHost.getNewLine(), + ), + ); }; - const reportWatchStatusChanged = (diagnostic: ts.Diagnostic) => { - const originMessage = ts.formatDiagnostic(diagnostic, formatHost); - const message = originMessage - .replace('message ', '') - // TS6031: Starting compilation in watch mode... - .replace('TS6031: ', '') - // TS6194: Found 0 errors. Watching for file changes. - .replace('TS6194: ', '') - // TS6032: File change detected. Starting incremental compilation... - .replace('TS6032: ', ''); - - logger.info(message); + const reportWatchStatusChanged: ts.WatchStatusReporter = ( + diagnostic: ts.Diagnostic, + _newLine: string, + _options: ts.CompilerOptions, + errorCount?: number, + ) => { + const message = ts.flattenDiagnosticMessageText( + diagnostic.messageText, + formatHost.getNewLine(), + ); + + // 6031: File change detected. Starting incremental compilation... + // 6032: Starting compilation in watch mode... + if (diagnostic.code === 6031 || diagnostic.code === 6032) { + logger.info(message); + } + + // 6194: 0 errors or 2+ errors! + if (diagnostic.code === 6194) { + if (errorCount === 0) { + logger.info(message); + onComplete(true); + } else { + logger.error(message); + } + } + + // 6193: 1 error + if (diagnostic.code === 6193) { + logger.error(message); + } }; const system = { ...ts.sys }; @@ -117,61 +126,6 @@ export function emitDts( reportWatchStatusChanged, ); - const origCreateProgram = host.createProgram; - host.createProgram = (rootNames, options, host, oldProgram) => { - return origCreateProgram(rootNames, options, host, oldProgram); - }; - - const origPostProgramCreate = host.afterProgramCreate; - host.afterProgramCreate = (program) => { - origPostProgramCreate!(program); - - const errors = program - .getSyntacticDiagnostics() - .filter((diag) => diag.category === ts.DiagnosticCategory.Error); - errors.push( - ...program - .getSemanticDiagnostics() - .filter((diag) => diag.category === ts.DiagnosticCategory.Error), - ); - - if (errors.length > 0) { - for (const diagnostic of errors) { - let fileLoc: string; - if (diagnostic.file) { - const { line, character } = ts.getLineAndCharacterOfPosition( - diagnostic.file, - diagnostic.start!, - ); - fileLoc = `${diagnostic.file.fileName}:${line + 1}:${character + 1} - `; - } else { - fileLoc = ''; - } - console.error( - `${fileLoc}error TS${diagnostic.code}:`, - ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'), - ); - } - console.error( - `Compilation failed (${errors.length} ${ - errors.length > 1 ? 'errors' : 'error' - }).\n`, - ); - } - }; - - const originWatchStatusChange = host.onWatchStatusChange; - host.onWatchStatusChange = ( - diagnostic: ts.Diagnostic, - newLine: string, - options: ts.CompilerOptions, - errorCount?: number, - ) => { - originWatchStatusChange!(diagnostic, newLine, options, errorCount); - // TS6194: Found 0 errors. Watching for file changes. - onComplete(diagnostic.code === 6194); - }; - ts.createWatchProgram(host); } } diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index 9834026ba..9a1fb6193 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -35,3 +35,15 @@ export function ensureTempDeclarationDir(): string { return dirPath; } + +export function getFileLoc(diagnostic: ts.Diagnostic): string { + if (diagnostic.file) { + const { line, character } = ts.getLineAndCharacterOfPosition( + diagnostic.file, + diagnostic.start!, + ); + return `${diagnostic.file.fileName}:${line + 1}:${character + 1} - `; + } + + return ''; +}