Skip to content

Commit

Permalink
enhance: improve error logging (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Jan 25, 2025
1 parent 93c1904 commit 58a1b86
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 14 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pnpm-lock.yaml
README.md
test/integration/errors
15 changes: 9 additions & 6 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
19 changes: 12 additions & 7 deletions src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/lib/normalize-error.ts
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 7 additions & 1 deletion src/rollup-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
BundleJobOptions,
} from './types'
import { removeOutputDir } from './utils'
import { normalizeError } from './lib/normalize-error'

export async function createAssetRollupJobs(
options: BundleConfig,
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ type BundleConfig = {
* @experimental
*/
onBuildEnd?: (assetJobs: any) => void

/*
* This hook is called when the build errors
* @experimental
*/
onBuildError?: (assetJob: any) => void
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/integration/errors/compile-error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "compile-error",
"main": "./dist/index.js"
}
3 changes: 3 additions & 0 deletions test/integration/errors/compile-error/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
expor t functi on functi on App() {
return <div>App</di v>;
}

0 comments on commit 58a1b86

Please sign in to comment.