优化js解析,更准确的把报错信息正确的输出
parent
f4a3ff6355
commit
1b3425196e
|
@ -111,7 +111,8 @@ export function parseJs(
|
||||||
code = '',
|
code = '',
|
||||||
imports,
|
imports,
|
||||||
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {},
|
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {},
|
||||||
filename
|
filename,
|
||||||
|
linePatch = 0
|
||||||
) {
|
) {
|
||||||
let fixedStyle = ''
|
let fixedStyle = ''
|
||||||
let ASSETS_DIR = '/@/'
|
let ASSETS_DIR = '/@/'
|
||||||
|
@ -125,16 +126,21 @@ export function parseJs(
|
||||||
code = Es.transformSync(code).code || ''
|
code = Es.transformSync(code).code || ''
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
let err = e.errors.pop()
|
let err = e.errors.pop()
|
||||||
console.log('%s: %s', red('Uncaught SyntaxError'), err.text)
|
let lines = code.split('\n')
|
||||||
console.log(
|
|
||||||
' @ line %d: %s',
|
console.log('%s: %s', red('Uncaught SyntaxError'), red(err.text))
|
||||||
err.location.line,
|
// 将上下文几行都打印出来
|
||||||
cyan(err.location.lineText)
|
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(
|
console.log(
|
||||||
' @ %s:%d:%d',
|
' @ %s:%d:%d',
|
||||||
blue(filename),
|
blue(filename),
|
||||||
err.location.line,
|
err.location.line + linePatch - 1,
|
||||||
err.location.column
|
err.location.column
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -283,6 +289,7 @@ export function compileVue(file, imports, options = {}) {
|
||||||
let js = code.match(JS_EXP)
|
let js = code.match(JS_EXP)
|
||||||
let scss = [...code.matchAll(STYLE_EXP)]
|
let scss = [...code.matchAll(STYLE_EXP)]
|
||||||
let html = code.match(HTML_EXP) || ['', '']
|
let html = code.match(HTML_EXP) || ['', '']
|
||||||
|
let linePatch = code.slice(0, js.index).split('\n').length // js起始行数修正
|
||||||
|
|
||||||
let hash = md5(file)
|
let hash = md5(file)
|
||||||
let scopeId = 'data-' + hash
|
let scopeId = 'data-' + hash
|
||||||
|
@ -317,9 +324,8 @@ export function compileVue(file, imports, options = {}) {
|
||||||
isCustomElement
|
isCustomElement
|
||||||
}).code.replace('export function render', 'function render')
|
}).code.replace('export function render', 'function render')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// console.log(err)
|
let lines = html[1].split('\n')
|
||||||
let tmp = html[1].split('\n')
|
let line = lines[err.loc?.start.line - 1]
|
||||||
let line = tmp[err.loc?.start.line - 1]
|
|
||||||
|
|
||||||
console.log('%s: %s', red('SyntaxError'), red(err.message))
|
console.log('%s: %s', red('SyntaxError'), red(err.message))
|
||||||
console.log(
|
console.log(
|
||||||
|
@ -360,7 +366,7 @@ function render(_ctx, _cache) {
|
||||||
CACHE[file] = { changed: false, js, html }
|
CACHE[file] = { changed: false, js, html }
|
||||||
}
|
}
|
||||||
|
|
||||||
output += parseJs(js, imports, options, file).replace(
|
output += parseJs(js, imports, options, file, linePatch).replace(
|
||||||
'export default {',
|
'export default {',
|
||||||
`\n${
|
`\n${
|
||||||
options.LEGACY_MODE ? '' : SHEETS_DEF
|
options.LEGACY_MODE ? '' : SHEETS_DEF
|
||||||
|
|
Loading…
Reference in New Issue