增加容错处理,避免代码出现语法错误时构建工具崩溃退出的问题

pull/1/head 0.7.2
yutent 2023-04-26 17:16:43 +08:00
parent a0f06b0171
commit dc1fea1bbb
2 changed files with 22 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import { createHash } from 'crypto'
import Es from 'esbuild' import Es from 'esbuild'
import { join } from 'path' import { join } from 'path'
import { compile } from '@vue/compiler-dom' import { compile } from '@vue/compiler-dom'
import { red, cyan, blue } from 'kolorist'
import { import {
JS_EXP, JS_EXP,
@ -103,7 +104,8 @@ export function parseJs(
code = '', code = '',
imports, imports,
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {}, { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {},
isBuild isBuild,
filename
) { ) {
let fixedStyle = '\n\n' let fixedStyle = '\n\n'
let ASSETS_DIR = '/@/' let ASSETS_DIR = '/@/'
@ -111,7 +113,23 @@ export function parseJs(
if (isBuild) { if (isBuild) {
ASSETS_DIR = '/assets/' // + (IS_MPA ? 'pages/' : '') ASSETS_DIR = '/assets/' // + (IS_MPA ? 'pages/' : '')
} }
try {
code = Es.transformSync(code).code || '' 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)
)
console.log(
' @ %s:%d:%d',
blue(filename),
err.location.line,
err.location.column
)
}
return ( return (
code code
@ -275,7 +293,7 @@ export function compileVue(file, imports, options = {}, isBuild) {
CACHE[file] = { changed: false, js, html } CACHE[file] = { changed: false, js, html }
} }
output += parseJs(js, imports, options, isBuild).replace( output += parseJs(js, imports, options, isBuild, file).replace(
'export default {', 'export default {',
'const __sfc__ = {\n render,\n' 'const __sfc__ = {\n render,\n'
) )

View File

@ -1,7 +1,7 @@
{ {
"name": "fite", "name": "fite",
"type": "module", "type": "module",
"version": "0.7.1", "version": "0.7.2",
"bin": { "bin": {
"fite": "index.js" "fite": "index.js"
}, },