Skip to content

Commit 88d5510

Browse files
committed
wip(dom2): 优化错误日志与警告日志交叉
1 parent dc26fe5 commit 88d5510

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

packages/uni-cli-shared/src/logs/index.ts

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,59 @@ export interface CompileLogOptions {
3939
column?: number
4040
}
4141

42+
const suppressedCompileLogs: Array<() => void> = []
43+
44+
let isSuppressed = false
45+
46+
export function suppressVueCompileLog(suppressed: boolean) {
47+
isSuppressed = suppressed
48+
if (!suppressed) {
49+
suppressedCompileLogs.forEach((fn) => fn())
50+
suppressedCompileLogs.length = 0
51+
}
52+
}
53+
4254
export function onCompileLog(
4355
type: 'warn' | 'error',
4456
error: CompileLogError,
4557
code: string,
4658
relativeFileName: string,
4759
options?: CompileLogOptions
4860
) {
49-
const char =
50-
type === 'warn' ? SPECIAL_CHARS.WARN_BLOCK : SPECIAL_CHARS.ERROR_BLOCK
51-
if (options?.plugin) {
52-
// CSS 插件格式
53-
const colorFn = type === 'warn' ? colors.yellow : (s: string) => s
54-
console[type](char + colorFn(`[plugin:${options.plugin}] ${error.message}`))
55-
let msg = formatAtFilename(relativeFileName, options.line, options.column)
56-
if (options.line && options.column) {
57-
msg += `\n${generateCodeFrame(code, {
58-
line: options.line,
59-
column: options.column,
60-
}).replace(/\t/g, ' ')}\n`
61-
}
62-
console.log(msg + char)
63-
} else {
64-
console[type](char + type + ': ' + error.message + (error.loc ? '' : char))
65-
if (error.loc) {
66-
const start = error.loc.start
67-
console.log(
68-
'at ' + relativeFileName + ':' + start.line + ':' + start.column
61+
if (isSuppressed) {
62+
// 不会导致内存泄漏吧?
63+
suppressedCompileLogs.push(print)
64+
return
65+
}
66+
print()
67+
function print() {
68+
const char =
69+
type === 'warn' ? SPECIAL_CHARS.WARN_BLOCK : SPECIAL_CHARS.ERROR_BLOCK
70+
if (options?.plugin) {
71+
// CSS 插件格式
72+
const colorFn = type === 'warn' ? colors.yellow : (s: string) => s
73+
console[type](
74+
char + colorFn(`[plugin:${options.plugin}] ${error.message}`)
75+
)
76+
let msg = formatAtFilename(relativeFileName, options.line, options.column)
77+
if (options.line && options.column) {
78+
msg += `\n${generateCodeFrame(code, {
79+
line: options.line,
80+
column: options.column,
81+
}).replace(/\t/g, ' ')}\n`
82+
}
83+
console.log(msg + char)
84+
} else {
85+
console[type](
86+
char + type + ': ' + error.message + (error.loc ? '' : char)
6987
)
70-
console.log(generateCodeFrameColumns(code, error.loc) + char)
88+
if (error.loc) {
89+
const start = error.loc.start
90+
console.log(
91+
'at ' + relativeFileName + ':' + start.line + ':' + start.column
92+
)
93+
console.log(generateCodeFrameColumns(code, error.loc) + char)
94+
}
7195
}
7296
}
7397
}

packages/vite-plugin-uni/src/cli/action.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isInHBuilderX,
1111
output,
1212
runByHBuilderX,
13+
suppressVueCompileLog,
1314
} from '@dcloudio/uni-cli-shared'
1415
import type { CliOptions } from '.'
1516
import { build, buildSSR } from './build'
@@ -52,8 +53,12 @@ export async function runDev(options: CliOptions & ServerOptions) {
5253
initEasycom()
5354
let isFirstStart = true
5455
let isFirstEnd = true
56+
const isDom2 = process.env.UNI_APP_X_DOM2 === 'true'
5557
await build(options, async (event) => {
5658
if (event.code === 'BUNDLE_START') {
59+
if (isDom2) {
60+
suppressVueCompileLog(false)
61+
}
5762
if (isFirstStart) {
5863
isFirstStart = false
5964
return
@@ -146,6 +151,9 @@ export async function runDev(options: CliOptions & ServerOptions) {
146151
}, 2000)
147152
}
148153
} else if (event.code === 'ERROR') {
154+
if (isDom2) {
155+
suppressVueCompileLog(true)
156+
}
149157
if (runByHBuilderX()) {
150158
setTimeout(() => {
151159
console.error(`Build failed with errors.`)

0 commit comments

Comments
 (0)