@@ -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+
4254export 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}
0 commit comments