Skip to content

Commit c3c6ce0

Browse files
authored
Fix svelte packages being required all the time (#327)
* update example * Fix svelte packages being required all the time
1 parent 2e67c0d commit c3c6ce0

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

examples/.prettierrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"tabWidth": 4,
44
"trailingComma": "all",
55
"singleQuote": true,
6-
"jsxBracketSameLine": true,
6+
"bracketSameLine": true,
77
"semi": true,
88
"importOrder": ["^@server/(.*)$", "^@core/(.*)$", "^@ui/(.*)$", "^[./]"],
99
"importOrderSeparation": true,
10-
"importOrderSortSpecifiers": true
10+
"importOrderSortSpecifiers": true,
11+
"plugins": ["../lib/src/index.js"]
1112
}

examples/example.svelte

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<script>
2+
import { onMount } from 'svelte';
3+
// I am top level comment in this file.
4+
// I am second line of top level comment in this file.
5+
import React from 'react';
6+
import thirdParty from 'third-party';
7+
8+
import something from '@server/something';
9+
10+
11+
import component from '@ui/hello';
12+
13+
import fourLevelRelativePath from '../../../../fourLevelRelativePath';
14+
import threeLevelRelativePath from '../../../threeLevelRelativePath';
15+
import twoLevelRelativePath from '../../twoLevelRelativePath';
16+
import oneLevelRelativePath from '../oneLevelRelativePath';
17+
import sameLevelRelativePath from './sameLevelRelativePath';
18+
19+
import otherthing from '@core/otherthing';
20+
let count = 0;
21+
22+
function increment() {
23+
count += 1;
24+
}
25+
</script>
26+
27+
<main>
28+
<h1>Hello Svelte!</h1>
29+
<p>The count is {count}</p>
30+
<button on:click={increment}>Increment</button>
31+
</main>
32+
33+
<style>
34+
main {
35+
text-align: center;
36+
padding: 1em;
37+
max-width: 240px;
38+
margin: 0 auto;
39+
}
40+
41+
h1 {
42+
color: #ff3e00;
43+
}
44+
45+
button {
46+
font-size: 1.2em;
47+
}
48+
</style>

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { parsers as typescriptParsers } from 'prettier/plugins/typescript';
66
import { defaultPreprocessor } from './preprocessors/default-processor';
77
import { sveltePreprocessor } from './preprocessors/svelte-preprocessor';
88
import { vuePreprocessor } from './preprocessors/vue-preprocessor';
9+
import type { Options } from 'prettier';
10+
import { createSvelteParsers } from './utils/create-svelte-parsers';
911

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

12-
const options = {
14+
const options: Options = {
1315
importOrder: {
1416
type: 'path',
1517
category: 'Global',
@@ -62,7 +64,7 @@ const options = {
6264
category: 'Global',
6365
default: 'with',
6466
description: 'Provide a keyword for import attributes',
65-
}
67+
},
6668
};
6769

6870
module.exports = {
@@ -84,7 +86,7 @@ module.exports = {
8486
preprocess: vuePreprocessor,
8587
},
8688
svelte: {
87-
...svelteParsers.svelte,
89+
...svelteParsers.parsers.svelte,
8890
preprocess: sveltePreprocessor,
8991
},
9092
},

src/utils/create-svelte-parsers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function createSvelteParsers() {
2+
try {
3+
var { parsers } = require('prettier-plugin-svelte');
4+
} catch {
5+
return {};
6+
}
7+
return { parsers };
8+
}

0 commit comments

Comments
 (0)