Skip to content

Commit

Permalink
chore: enhance log
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Aug 1, 2024
1 parent 939f262 commit 7879b47
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 104 deletions.
10 changes: 6 additions & 4 deletions packages/plugin-dts/src/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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}`,
);
}
154 changes: 54 additions & 100 deletions packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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');
}

Expand All @@ -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 };
Expand All @@ -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);
}
}
12 changes: 12 additions & 0 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}

0 comments on commit 7879b47

Please sign in to comment.