Skip to content

Commit fabca7b

Browse files
committed
fix: reduce initial load size of prettier worker
1 parent bcc914b commit fabca7b

1 file changed

Lines changed: 52 additions & 16 deletions

File tree

sites/twind.run/src/lib/prettier.api.ts

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,68 @@
1+
import type { Options } from 'prettier'
12
import type { Prettier } from './prettier'
23

34
import prettier from 'prettier/esm/standalone.mjs'
4-
import parserBabel from 'prettier/esm/parser-babel.mjs'
5-
import parserHtml from 'prettier/esm/parser-html.mjs'
6-
import parserMarkdown from 'prettier/esm/parser-markdown.mjs'
7-
import parserPostcss from 'prettier/esm/parser-postcss.mjs'
8-
import parserTypescript from 'prettier/esm/parser-typescript.mjs'
5+
// import parserBabel from 'prettier/esm/parser-babel.mjs'
6+
// import parserHtml from 'prettier/esm/parser-html.mjs'
7+
// import parserMarkdown from 'prettier/esm/parser-markdown.mjs'
8+
// import parserPostcss from 'prettier/esm/parser-postcss.mjs'
9+
// import parserTypescript from 'prettier/esm/parser-typescript.mjs'
910

1011
import { Layer } from '../../../../packages/twind/src/internal/precedence'
1112

1213
const defaults = { semi: false, trailingComma: 'all' }
13-
const plugins = [parserBabel, parserHtml, parserMarkdown, parserPostcss, parserTypescript]
14+
15+
const plugins = [
16+
{
17+
detect: (parser: string) =>
18+
/^[mc]?jsx?$/.test(parser)
19+
? 'babel'
20+
: /^[mc]?tsx?$/.test(parser)
21+
? 'babel-ts'
22+
: /^json5?$/.test(parser) && parser,
23+
load: () => import('prettier/esm/parser-babel.mjs').then((m) => m.default),
24+
},
25+
{
26+
detect: (parser: string) => /^html?$/.test(parser) && 'html',
27+
load: () => import('prettier/esm/parser-html.mjs').then((m) => m.default),
28+
},
29+
{
30+
detect: (parser: string) => /^(le|s?c)ss$/.test(parser) && parser,
31+
load: () => import('prettier/esm/parser-postcss.mjs').then((m) => m.default),
32+
},
33+
]
34+
35+
async function getOptions(options?: Options) {
36+
let parser = options?.parser || /(?:\.([^.]+))?$/.exec(options?.filepath || '')?.[1]
37+
38+
if (typeof parser === 'string') {
39+
for (const plugin of plugins) {
40+
const found = plugin.detect(parser)
41+
if (found) {
42+
return {
43+
...defaults,
44+
...options,
45+
parser: found,
46+
plugins: [await plugin.load()],
47+
}
48+
}
49+
}
50+
}
51+
52+
return {
53+
...defaults,
54+
...options,
55+
plugins: Promise.all(plugins.map((plugin) => plugin.load())),
56+
}
57+
}
1458

1559
const api: Prettier = {
1660
async format(source, options) {
17-
return prettier.format(source, {
18-
...defaults,
19-
...options,
20-
plugins,
21-
})
61+
return prettier.format(source, await getOptions(options))
2262
},
2363

2464
async formatWithCursor(source, options) {
25-
return prettier.formatWithCursor(source, {
26-
...defaults,
27-
...options,
28-
plugins,
29-
})
65+
return prettier.formatWithCursor(source, await getOptions(options))
3066
},
3167

3268
async formatPreviewCSS(rules) {

0 commit comments

Comments
 (0)