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 bddb41f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 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

0 comments on commit bddb41f

Please sign in to comment.