Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make vue as optional peer dependency #3

Merged
merged 1 commit into from Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/TROUBLESHOOTING.md
Expand Up @@ -110,3 +110,7 @@ module.exports = {
plugins: [require('@serverless-guru/prettier-plugin-import-order')],
};
```

#### 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.
12 changes: 9 additions & 3 deletions package.json
Expand Up @@ -37,8 +37,7 @@
"@babel/types": "~7.19.4",
"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": {
"@babel/preset-typescript": "7.18.6",
Expand All @@ -51,11 +50,18 @@
"jest": "29.2.1",
"prettier": "2.7.1",
"ts-jest": "29.0.3",
"typescript": "4.8.4"
"typescript": "4.8.4",
"@vue/compiler-sfc": "3.2.40"
},
"peerDependencies": {
"@vue/compiler-sfc": ">=3.0.0",
"prettier": "2.x"
},
"peerDependenciesMeta": {
"@vue/compiler-sfc": {
"optional": true
}
},
"publishConfig": {
"access": "public"
}
Expand Down
19 changes: 14 additions & 5 deletions src/preprocessors/vue-preprocessor.ts
@@ -1,11 +1,20 @@
import { parse } from '@vue/compiler-sfc';
import type { PrettierOptions } from '../types';

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(
'[@serverless-guru/prettier-plugin-import-order]: Could not process .vue file. Please be sure that "@vue/compiler-sfc" is installed in your project.',
);
throw err;
}
}
}