Skip to content

Commit aa2558d

Browse files
committed
fix: don't prettify if prettier 3 is detected
1 parent ed0d284 commit aa2558d

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

docs/options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ When both options are specified, enables file-system-based template compilation
9090
## prettify
9191

9292
- type: `boolean`
93-
- default: `true`
93+
- default: `true` if prettier v1 or v2 is installed, `false` otherwise
9494

9595
In development mode, we use [prettier](https://prettier.io/) to format the compiled render function for ease of debugging by default. However, if you encounter any obscure bug of prettier, such as [exponential compilation time for deeply nested functions](https://github.com/prettier/prettier/issues/4672), you can disable this option to circumvent it.
9696

docs/zh/options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ sidebar: auto
9090
## prettify
9191

9292
- 类型:`boolean`
93-
- 默认值:`true`
93+
- 默认值:当检测到 prettier v1 或 v2 时为 `true`,否则为 `false`
9494

9595
在开发环境下,我们默认使用 [prettier](https://prettier.io/) 格式化编译后的模板渲染代码,以方便调试。然而,如果你开发时碰到了 prettier 的某些罕见 bug,比如[格式化多层嵌套的函数时运行时间过长](https://github.com/prettier/prettier/issues/4672),你可以通过禁用这个选项来绕开。
9696

lib/loaders/templateLoader.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ module.exports = function (source) {
4141
const descriptor = getDescriptor(filename, options, loaderContext)
4242
const script = resolveScript(descriptor, id, options, loaderContext)
4343

44+
// Prettier 3 APIs are all async
45+
// but `vue/compiler-sfc` and `@vue/component-compiler-utils` can only use sync function to format code
46+
let hasCompatiblePrettier = false
47+
try {
48+
const prettier = require('prettier/package.json')
49+
const major = parseInt(prettier.version.split('.')[0], 10)
50+
if (major === 1 || major === 2) {
51+
hasCompatiblePrettier = true
52+
}
53+
} catch (e) {}
54+
4455
// for vue/compiler-sfc OR @vue/component-compiler-utils
4556
const finalOptions = {
4657
source,
@@ -53,7 +64,7 @@ module.exports = function (source) {
5364
isProduction,
5465
isFunctional,
5566
optimizeSSR: isServer && options.optimizeSSR !== false,
56-
prettify: options.prettify,
67+
prettify: options.prettify === undefined ? hasCompatiblePrettier : options.prettify,
5768
bindings: script ? script.bindings : undefined
5869
}
5970

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
},
4747
"vue-template-compiler": {
4848
"optional": true
49+
},
50+
"prettier": {
51+
"optional": true
4952
}
5053
},
5154
"dependencies": {

0 commit comments

Comments
 (0)