From 1b3425196e3c5527805e219d87f85990b2503f6c Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 21 Jun 2023 17:10:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96js=E8=A7=A3=E6=9E=90,?= =?UTF-8?q?=E6=9B=B4=E5=87=86=E7=A1=AE=E7=9A=84=E6=8A=8A=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=AD=A3=E7=A1=AE=E7=9A=84=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 844ca43..33097c5 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -111,7 +111,8 @@ export function parseJs( code = '', imports, { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {}, - filename + filename, + linePatch = 0 ) { let fixedStyle = '' let ASSETS_DIR = '/@/' @@ -125,16 +126,21 @@ export function parseJs( code = Es.transformSync(code).code || '' } catch (e) { let err = e.errors.pop() - console.log('%s: %s', red('Uncaught SyntaxError'), err.text) - console.log( - ' @ line %d: %s', - err.location.line, - cyan(err.location.lineText) - ) + let lines = code.split('\n') + + console.log('%s: %s', red('Uncaught SyntaxError'), red(err.text)) + // 将上下文几行都打印出来 + for (let i = err.location.line - 3; i <= err.location.line + 1; i++) { + console.log( + ' @ line %d: %s', + i + linePatch, + err.location.line === i + 1 ? red(lines[i]) : lines[i] + ) + } console.log( ' @ %s:%d:%d', blue(filename), - err.location.line, + err.location.line + linePatch - 1, err.location.column ) } @@ -283,6 +289,7 @@ export function compileVue(file, imports, options = {}) { let js = code.match(JS_EXP) let scss = [...code.matchAll(STYLE_EXP)] let html = code.match(HTML_EXP) || ['', ''] + let linePatch = code.slice(0, js.index).split('\n').length // js起始行数修正 let hash = md5(file) let scopeId = 'data-' + hash @@ -317,9 +324,8 @@ export function compileVue(file, imports, options = {}) { isCustomElement }).code.replace('export function render', 'function render') } catch (err) { - // console.log(err) - let tmp = html[1].split('\n') - let line = tmp[err.loc?.start.line - 1] + let lines = html[1].split('\n') + let line = lines[err.loc?.start.line - 1] console.log('%s: %s', red('SyntaxError'), red(err.message)) console.log( @@ -360,7 +366,7 @@ function render(_ctx, _cache) { CACHE[file] = { changed: false, js, html } } - output += parseJs(js, imports, options, file).replace( + output += parseJs(js, imports, options, file, linePatch).replace( 'export default {', `\n${ options.LEGACY_MODE ? '' : SHEETS_DEF -- 2.40.1 From 3404c87a7109f0057ae6c7328ac8a745bfb6c4a0 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 21 Jun 2023 19:06:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=B9=E9=94=99?= =?UTF-8?q?=E5=88=A4=E6=96=AD,=20=E9=81=BF=E5=85=8Dvscode=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=97=B6=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=8A=BD=E9=A3=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 33097c5..3fae656 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -289,7 +289,7 @@ export function compileVue(file, imports, options = {}) { let js = code.match(JS_EXP) let scss = [...code.matchAll(STYLE_EXP)] let html = code.match(HTML_EXP) || ['', ''] - let linePatch = code.slice(0, js.index).split('\n').length // js起始行数修正 + let linePatch = code.slice(0, js?.index || 0).split('\n').length // js起始行数修正 let hash = md5(file) let scopeId = 'data-' + hash -- 2.40.1