diff --git a/packages/uni-cli-shared/src/hbx/index.ts b/packages/uni-cli-shared/src/hbx/index.ts index def50dd176b3613024d4fc963a4a1fa0736f3d49..401ed8e512bb4dc36b38fb387bcb1b80facbc12b 100644 --- a/packages/uni-cli-shared/src/hbx/index.ts +++ b/packages/uni-cli-shared/src/hbx/index.ts @@ -2,6 +2,8 @@ import path from 'path' import { normalizePath } from '../utils' import { uniConsolePlugin } from '../vite/plugins/console' +export { formatAtFilename } from './log' + export * from './env' export { initModuleAlias, diff --git a/packages/uni-cli-shared/src/hbx/log.ts b/packages/uni-cli-shared/src/hbx/log.ts index 5c7674e137e52118e47900356fb464e420f47b94..64a796c101a6a2665e03b6fe7942285f6c379e75 100644 --- a/packages/uni-cli-shared/src/hbx/log.ts +++ b/packages/uni-cli-shared/src/hbx/log.ts @@ -10,6 +10,22 @@ const SIGNAL_H5_NETWORK = ' > Network:' const networkLogs: string[] = [] +export function formatAtFilename( + filename: string, + line?: number, + column?: number +) { + return `at ${colors.cyan( + normalizePath( + path.relative(process.env.UNI_INPUT_DIR, filename.split('?')[0]) + ) + + ':' + + (line || 1) + + ':' + + (column || 0) + )}` +} + export const h5ServeFormatter: Formatter = { test(msg) { return msg.includes(SIGNAL_H5_LOCAL) || msg.includes(SIGNAL_H5_NETWORK) @@ -83,17 +99,7 @@ function buildErrorMessage( args.push(colors.red(err.message)) } if (err.id) { - args.push( - `at ${colors.cyan( - normalizePath( - path.relative(process.env.UNI_INPUT_DIR, err.id.split('?')[0]) - ) + - ':' + - (err.loc?.line || 1) + - ':' + - (err.loc?.column || 0) - )}` - ) + args.push(formatAtFilename(err.id, err.loc?.line, err.loc?.column)) } if (err.frame) { args.push(colors.yellow(pad(err.frame)))