Skip to content

Commit

Permalink
Fix svelte packages being required all the time (#327)
Browse files Browse the repository at this point in the history
* update example

* Fix svelte packages being required all the time
  • Loading branch information
byara authored Dec 6, 2024
1 parent 2e67c0d commit c3c6ce0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
5 changes: 3 additions & 2 deletions examples/.prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": true,
"jsxBracketSameLine": true,
"bracketSameLine": true,
"semi": true,
"importOrder": ["^@server/(.*)$", "^@core/(.*)$", "^@ui/(.*)$", "^[./]"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true
"importOrderSortSpecifiers": true,
"plugins": ["../lib/src/index.js"]
}
48 changes: 48 additions & 0 deletions examples/example.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script>
import { onMount } from 'svelte';
// I am top level comment in this file.
// I am second line of top level comment in this file.
import React from 'react';
import thirdParty from 'third-party';
import something from '@server/something';
import component from '@ui/hello';
import fourLevelRelativePath from '../../../../fourLevelRelativePath';
import threeLevelRelativePath from '../../../threeLevelRelativePath';
import twoLevelRelativePath from '../../twoLevelRelativePath';
import oneLevelRelativePath from '../oneLevelRelativePath';
import sameLevelRelativePath from './sameLevelRelativePath';
import otherthing from '@core/otherthing';
let count = 0;
function increment() {
count += 1;
}
</script>

<main>
<h1>Hello Svelte!</h1>
<p>The count is {count}</p>
<button on:click={increment}>Increment</button>
</main>

<style>
main {
text-align: center;
padding: 1em;
max-width: 240px;
margin: 0 auto;
}
h1 {
color: #ff3e00;
}
button {
font-size: 1.2em;
}
</style>
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { parsers as typescriptParsers } from 'prettier/plugins/typescript';
import { defaultPreprocessor } from './preprocessors/default-processor';
import { sveltePreprocessor } from './preprocessors/svelte-preprocessor';
import { vuePreprocessor } from './preprocessors/vue-preprocessor';
import type { Options } from 'prettier';
import { createSvelteParsers } from './utils/create-svelte-parsers';

const { parsers: svelteParsers } = require('prettier-plugin-svelte');
const svelteParsers = createSvelteParsers();

const options = {
const options: Options = {
importOrder: {
type: 'path',
category: 'Global',
Expand Down Expand Up @@ -62,7 +64,7 @@ const options = {
category: 'Global',
default: 'with',
description: 'Provide a keyword for import attributes',
}
},
};

module.exports = {
Expand All @@ -84,7 +86,7 @@ module.exports = {
preprocess: vuePreprocessor,
},
svelte: {
...svelteParsers.svelte,
...svelteParsers.parsers.svelte,
preprocess: sveltePreprocessor,
},
},
Expand Down
8 changes: 8 additions & 0 deletions src/utils/create-svelte-parsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function createSvelteParsers() {
try {
var { parsers } = require('prettier-plugin-svelte');
} catch {
return {};
}
return { parsers };
}

0 comments on commit c3c6ce0

Please sign in to comment.