Skip to content

Commit

Permalink
Make vue dependency optional
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Oct 27, 2022
1 parent a2ffeb1 commit 512351f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -380,7 +380,7 @@ Having some trouble or an issue? You can check [FAQ / Troubleshooting section](.
| NodeJS with ES Modules | ✅ Everything | - |
| React | ✅ Everything | - |
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
| Vue | ✅ Everything | - |
| Vue | ✅ Everything | Peer dependency `@vue/compiler-sfc` is required |
| Svelte | ⚠️ Not yet | Contributions are welcome |

## Contribution
Expand Down
4 changes: 4 additions & 0 deletions docs/TROUBLESHOOTING.md
Expand Up @@ -107,3 +107,7 @@ module.exports = {
plugins: [require('@ianvs/prettier-plugin-sort-imports')],
};
```

#### Q. Why are my .vue sfc files not being formatted?

Be sure that you have `@vue/compiler-sfc` installed in your project, (run `npm ls @vue/compiler-sfc` to double-check). If it's not there, install it and try formatting again.
9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -52,8 +52,7 @@
"@babel/types": "^7.17.0",
"javascript-natural-sort": "0.7.1",
"lodash.clone": "^4.5.0",
"lodash.isequal": "^4.5.0",
"@vue/compiler-sfc": "3.2.40"
"lodash.isequal": "^4.5.0"
},
"devDependencies": {
"@types/jest": "^29.2.0",
Expand All @@ -66,6 +65,12 @@
"typescript": "4.8.4"
},
"peerDependencies": {
"@vue/compiler-sfc": "3.2.40",
"prettier": "2.x"
},
"peerDependenciesMeta": {
"@vue/compiler-sfc": {
"optional": true
}
}
}
20 changes: 14 additions & 6 deletions src/preprocessors/vue.ts
@@ -1,12 +1,20 @@
import { parse } from '@vue/compiler-sfc';

import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor';

export function vuePreprocessor(code: string, options: PrettierOptions) {
const { descriptor } = parse(code);
const content =
(descriptor.script ?? descriptor.scriptSetup)?.content ?? code;
try {
const { parse } = require('@vue/compiler-sfc');
const { descriptor } = parse(code);
const content =
(descriptor.script ?? descriptor.scriptSetup)?.content ?? code;

return code.replace(content, `\n${preprocessor(content, options)}\n`);
return code.replace(content, `\n${preprocessor(content, options)}\n`);
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {
console.warn(
'[@ianvs/prettier-plugin-sort-imports]: Could not process .vue file. Please be sure that "@vue/compiler-sfc" is installed in your project.',
);
throw err;
}
}
}
96 changes: 1 addition & 95 deletions yarn.lock
Expand Up @@ -304,7 +304,7 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.16.4", "@babel/parser@^7.16.7", "@babel/parser@^7.17.3", "@babel/parser@^7.17.7":
"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.16.7", "@babel/parser@^7.17.3", "@babel/parser@^7.17.7":
version "7.17.7"
resolved "https://registry.npmjs.org/@babel/parser/-/parser-7.17.7.tgz"
integrity sha512-bm3AQf45vR4gKggRfvJdYJ0gFLoCbsPxiFLSH6hTVYABptNHY6l9NrhnucVjQ/X+SPtLANT9lc0fFhikj+VBRA==
Expand Down Expand Up @@ -874,64 +874,6 @@
dependencies:
"@types/yargs-parser" "*"

"@vue/compiler-core@3.2.40":
version "3.2.40"
resolved "https://registry.npmjs.org/@vue/compiler-core/-/compiler-core-3.2.40.tgz"
integrity sha512-2Dc3Stk0J/VyQ4OUr2yEC53kU28614lZS+bnrCbFSAIftBJ40g/2yQzf4mPBiFuqguMB7hyHaujdgZAQ67kZYA==
dependencies:
"@babel/parser" "^7.16.4"
"@vue/shared" "3.2.40"
estree-walker "^2.0.2"
source-map "^0.6.1"

"@vue/compiler-dom@3.2.40":
version "3.2.40"
resolved "https://registry.npmjs.org/@vue/compiler-dom/-/compiler-dom-3.2.40.tgz"
integrity sha512-OZCNyYVC2LQJy4H7h0o28rtk+4v+HMQygRTpmibGoG9wZyomQiS5otU7qo3Wlq5UfHDw2RFwxb9BJgKjVpjrQw==
dependencies:
"@vue/compiler-core" "3.2.40"
"@vue/shared" "3.2.40"

"@vue/compiler-sfc@3.2.40":
version "3.2.40"
resolved "https://registry.npmjs.org/@vue/compiler-sfc/-/compiler-sfc-3.2.40.tgz"
integrity sha512-tzqwniIN1fu1PDHC3CpqY/dPCfN/RN1thpBC+g69kJcrl7mbGiHKNwbA6kJ3XKKy8R6JLKqcpVugqN4HkeBFFg==
dependencies:
"@babel/parser" "^7.16.4"
"@vue/compiler-core" "3.2.40"
"@vue/compiler-dom" "3.2.40"
"@vue/compiler-ssr" "3.2.40"
"@vue/reactivity-transform" "3.2.40"
"@vue/shared" "3.2.40"
estree-walker "^2.0.2"
magic-string "^0.25.7"
postcss "^8.1.10"
source-map "^0.6.1"

"@vue/compiler-ssr@3.2.40":
version "3.2.40"
resolved "https://registry.npmjs.org/@vue/compiler-ssr/-/compiler-ssr-3.2.40.tgz"
integrity sha512-80cQcgasKjrPPuKcxwuCx7feq+wC6oFl5YaKSee9pV3DNq+6fmCVwEEC3vvkf/E2aI76rIJSOYHsWSEIxK74oQ==
dependencies:
"@vue/compiler-dom" "3.2.40"
"@vue/shared" "3.2.40"

"@vue/reactivity-transform@3.2.40":
version "3.2.40"
resolved "https://registry.npmjs.org/@vue/reactivity-transform/-/reactivity-transform-3.2.40.tgz"
integrity sha512-HQUCVwEaacq6fGEsg2NUuGKIhUveMCjOk8jGHqLXPI2w6zFoPrlQhwWEaINTv5kkZDXKEnCijAp+4gNEHG03yw==
dependencies:
"@babel/parser" "^7.16.4"
"@vue/compiler-core" "3.2.40"
"@vue/shared" "3.2.40"
estree-walker "^2.0.2"
magic-string "^0.25.7"

"@vue/shared@3.2.40":
version "3.2.40"
resolved "https://registry.npmjs.org/@vue/shared/-/shared-3.2.40.tgz"
integrity sha512-0PLQ6RUtZM0vO3teRfzGi4ltLUO5aO+kLgwh4Um3THSR03rpQWLTuRCkuO5A41ITzwdWeKdPHtSARuPkoo5pCQ==

ansi-escapes@^4.2.1:
version "4.3.2"
resolved "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz"
Expand Down Expand Up @@ -1293,11 +1235,6 @@ esprima@^4.0.0:
resolved "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz"
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==

estree-walker@^2.0.2:
version "2.0.2"
resolved "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz"
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==

execa@^5.0.0:
version "5.1.1"
resolved "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz"
Expand Down Expand Up @@ -1990,13 +1927,6 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

magic-string@^0.25.7:
version "0.25.9"
resolved "https://registry.npmjs.org/magic-string/-/magic-string-0.25.9.tgz"
integrity sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==
dependencies:
sourcemap-codec "^1.4.8"

make-dir@^3.0.0:
version "3.1.0"
resolved "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz"
Expand Down Expand Up @@ -2051,11 +1981,6 @@ ms@2.1.2:
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

nanoid@^3.3.4:
version "3.3.4"
resolved "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz"
integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==

natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz"
Expand Down Expand Up @@ -2180,15 +2105,6 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

postcss@^8.1.10:
version "8.4.18"
resolved "https://registry.npmjs.org/postcss/-/postcss-8.4.18.tgz"
integrity sha512-Wi8mWhncLJm11GATDaQKobXSNEYGUHeQLiQqDFG1qQ5UTDPTEvKw0Xt5NsTpktGTwLps3ByrWsBrG0rB8YQ9oA==
dependencies:
nanoid "^3.3.4"
picocolors "^1.0.0"
source-map-js "^1.0.2"

prettier@2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
Expand Down Expand Up @@ -2298,11 +2214,6 @@ slash@^3.0.0:
resolved "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==

source-map-js@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz"
integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==

source-map-support@0.5.13:
version "0.5.13"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.13.tgz#31b24a9c2e73c2de85066c0feb7d44767ed52932"
Expand All @@ -2321,11 +2232,6 @@ source-map@^0.6.0, source-map@^0.6.1:
resolved "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

sourcemap-codec@^1.4.8:
version "1.4.8"
resolved "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz"
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==

sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz"
Expand Down

0 comments on commit 512351f

Please sign in to comment.