Skip to content

Commit

Permalink
Support vue 2.7
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksei Nagovitsyn <al3x.nag@gmail.com>
  • Loading branch information
IanVS and nag5000 committed Mar 11, 2024
1 parent 8541bb8 commit 005d4f8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 7 additions & 3 deletions src/preprocessors/vue.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 005d4f8

Please sign in to comment.