Skip to content

Commit

Permalink
feat: make vue as optional peer dependency (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharf5 committed Dec 24, 2022
1 parent 4d75570 commit 6fff44c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
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;
}
}
}

0 comments on commit 6fff44c

Please sign in to comment.