Skip to content

Commit

Permalink
Support vue 2.7 (#156)
Browse files Browse the repository at this point in the history
Pulls in
trivago/prettier-plugin-sort-imports#215 from
upstream, to add support for Vue 2.7, which as far as I can tell still
has ~1 million weekly downloads.

Co-authored-by: Aleksei Nagovitsyn <al3x.nag@gmail.com>
  • Loading branch information
IanVS and nag5000 committed Mar 12, 2024
1 parent 03b05ff commit f5dfa5f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -76,7 +76,7 @@
"vite": "^4.0.0"
},
"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
@@ -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 f5dfa5f

Please sign in to comment.