本地老工程 vue2.7.x+webpack4 在降级 webpack5 的时候遇启动和打包报错:
Syntax Error: SyntaxError: /xxxxx.vue Unexpected token, expected "," (1:8)
> 1 | [object Promise]
| ^
2 | export {render, staticRenderFns}
最初才发现是 prettier 导致的。
举荐看看 stackoverflow 下面这个答复。Update Nodejs 14->18 — webpack have the same syntaxError: /…/xxx.vue: Unexpected token, expected “,” (1:8) for ALL Vue 2 components in the project
导致起因:
在 prettier v3.0.0 中,默认值从 es5 更改为 all
Default value changed from es5 to all in v3.0.0
Print trailing commas wherever possible in multi-line comma-separated syntactic structures. (A single-line array, for example, never gets trailing commas.)
Valid options:
"all" - Trailing commas wherever possible (including function parameters and calls). To run, JavaScript code formatted this way needs an engine that supports ES2017 (Node.js 8+ or a modern browser) or downlevel compilation. This also enables trailing commas in type parameters in TypeScript (supported since TypeScript 2.7 released in January 2018).
"es5" - Trailing commas where valid in ES5 (objects, arrays, etc.). No trailing commas in type parameters in TypeScript.
"none" - No trailing commas.
解决办法 1
prettier 插件版本回退到 v2.8.8
"prettier":"v2.8.8"
解决办法 2
vue-loader 配置 prettify: false。
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
// vue loader 在解决.vue 模板时默认不必 prettier 格式化
prettify: false,
},
},