diff --git a/package.json b/package.json index f16f9b1..097a2cf 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "vitest": "^0.34.5" }, "peerDependencies": { - "@vue/compiler-sfc": ">=3.0.0", + "@vue/compiler-sfc": "2.7.x || 3.x", "prettier": "2 || 3" }, "peerDependenciesMeta": { diff --git a/src/preprocessors/vue.ts b/src/preprocessors/vue.ts index 23e8348..62435f2 100644 --- a/src/preprocessors/vue.ts +++ b/src/preprocessors/vue.ts @@ -1,4 +1,4 @@ -import type { parse as Parse } from '@vue/compiler-sfc'; +import type { parse as Parse, SFCDescriptor } from '@vue/compiler-sfc'; import { ImportOrderParserPlugin } from '../../types'; import { PrettierOptions } from '../types'; @@ -7,8 +7,12 @@ import { preprocessor } from './preprocessor'; export function vuePreprocessor(code: string, options: PrettierOptions) { try { - const { parse }: { parse: typeof Parse } = require('@vue/compiler-sfc'); - const { descriptor } = parse(code); + const { parse, transformRef } = require('@vue/compiler-sfc'); + const descriptor: SFCDescriptor = transformRef + ? // @vue/compiler-sfc 3.x + parse(code).descriptor + : // @vue/compiler-sfc 2.7.x + parse({ source: code }); // 1. Filter valid blocks. const blocks = [descriptor.script, descriptor.scriptSetup].filter( diff --git a/yarn.lock b/yarn.lock index d1c5d15..4a09c76 100644 --- a/yarn.lock +++ b/yarn.lock @@ -164,7 +164,7 @@ "@babel/parser" "^7.22.15" "@babel/types" "^7.22.15" -"@babel/traverse@^7.21.5", "@babel/traverse@^7.23.2": +"@babel/traverse@^7.23.2": version "7.23.2" resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.2.tgz#329c7a06735e144a506bdb2cad0268b7f46f4ad8" integrity sha512-azpe59SQ48qG6nu2CzcMLbxUudtN+dOM9kDbUqGq3HXUJRlo7i8fvPoxQUzYgLZ4cMVmuZgm8vvBpNeRhd6XSw==