From 58a1b865e9b9b5460e83fd56ac3146f405a35817 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sat, 25 Jan 2025 22:14:02 +0100 Subject: [PATCH] enhance: improve error logging (#642) --- .prettierignore | 1 + src/bin/index.ts | 15 +++++++++------ src/bundle.ts | 19 ++++++++++++------- src/lib/normalize-error.ts | 9 +++++++++ src/rollup-job.ts | 8 +++++++- src/types.ts | 6 ++++++ .../errors/compile-error/package.json | 4 ++++ .../errors/compile-error/src/index.tsx | 3 +++ 8 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 src/lib/normalize-error.ts create mode 100644 test/integration/errors/compile-error/package.json create mode 100644 test/integration/errors/compile-error/src/index.tsx diff --git a/.prettierignore b/.prettierignore index 39cb3f67..a64e115d 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,3 @@ pnpm-lock.yaml README.md +test/integration/errors diff --git a/src/bin/index.ts b/src/bin/index.ts index a5c63692..638f7bcd 100644 --- a/src/bin/index.ts +++ b/src/bin/index.ts @@ -11,6 +11,7 @@ import { bundle } from '../../src/index' import { prepare } from '../prepare' import { RollupWatcher } from 'rollup' import { logOutputState } from '../plugins/output-state-plugin' +import { normalizeError } from '../lib/normalize-error' const helpMessage = ` Usage: bunchee [options] @@ -310,10 +311,15 @@ async function run(args: CliArgs) { } } + function onBuildError(error: Error) { + logError(error) + } + let buildError: any bundleConfig._callbacks = { onBuildStart, onBuildEnd, + onBuildError, } if (watch) { @@ -406,12 +412,9 @@ function logWatcherBuildTime(result: RollupWatcher[], spinner: Spinner) { }) } -function logError(error: any) { - if (!error) return - // logging source code in format - if (error.frame) { - process.stderr.write(error.frame + '\n') - } +function logError(err: unknown) { + const error = normalizeError(err) + logger.error(error) } main().catch(exit) diff --git a/src/bundle.ts b/src/bundle.ts index 14a767cf..e58aa9e1 100644 --- a/src/bundle.ts +++ b/src/bundle.ts @@ -173,13 +173,18 @@ async function bundle( const generateTypes = hasTsConfig && options.dts !== false const rollupJobsOptions: BundleJobOptions = { isFromCli, generateTypes } - const assetJobs = await createAssetRollupJobs( - options, - buildContext, - rollupJobsOptions, - ) - - options._callbacks?.onBuildEnd?.(assetJobs) + try { + const assetJobs = await createAssetRollupJobs( + options, + buildContext, + rollupJobsOptions, + ) + + options._callbacks?.onBuildEnd?.(assetJobs) + } catch (error) { + options._callbacks?.onBuildError?.(error) + return Promise.reject(error) + } } export default bundle diff --git a/src/lib/normalize-error.ts b/src/lib/normalize-error.ts new file mode 100644 index 00000000..370247cb --- /dev/null +++ b/src/lib/normalize-error.ts @@ -0,0 +1,9 @@ +export function normalizeError(error: any) { + // Remove the noise from rollup plugin error + if (error.code === 'PLUGIN_ERROR') { + const normalizedError = new Error(error.message) + normalizedError.stack = error.stack + error = normalizedError + } + return error +} diff --git a/src/rollup-job.ts b/src/rollup-job.ts index 8d1e735b..6ba1652c 100644 --- a/src/rollup-job.ts +++ b/src/rollup-job.ts @@ -15,6 +15,7 @@ import { BundleJobOptions, } from './types' import { removeOutputDir } from './utils' +import { normalizeError } from './lib/normalize-error' export async function createAssetRollupJobs( options: BundleConfig, @@ -44,7 +45,12 @@ export async function createAssetRollupJobs( bundleOrWatch(options, rollupConfig), ) - return await Promise.all(rollupJobs) + try { + return await Promise.all(rollupJobs) + } catch (err: unknown) { + const error = normalizeError(err) + throw error + } } async function bundleOrWatch( diff --git a/src/types.ts b/src/types.ts index 217011bb..6c64e0fe 100644 --- a/src/types.ts +++ b/src/types.ts @@ -58,6 +58,12 @@ type BundleConfig = { * @experimental */ onBuildEnd?: (assetJobs: any) => void + + /* + * This hook is called when the build errors + * @experimental + */ + onBuildError?: (assetJob: any) => void } } diff --git a/test/integration/errors/compile-error/package.json b/test/integration/errors/compile-error/package.json new file mode 100644 index 00000000..61ad7981 --- /dev/null +++ b/test/integration/errors/compile-error/package.json @@ -0,0 +1,4 @@ +{ + "name": "compile-error", + "main": "./dist/index.js" +} \ No newline at end of file diff --git a/test/integration/errors/compile-error/src/index.tsx b/test/integration/errors/compile-error/src/index.tsx new file mode 100644 index 00000000..7c4e1591 --- /dev/null +++ b/test/integration/errors/compile-error/src/index.tsx @@ -0,0 +1,3 @@ +expor t functi on functi on App() { + return
App; +}