|
| 1 | +import type { Options } from 'prettier' |
1 | 2 | import type { Prettier } from './prettier' |
2 | 3 |
|
3 | 4 | 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' |
9 | 10 |
|
10 | 11 | import { Layer } from '../../../../packages/twind/src/internal/precedence' |
11 | 12 |
|
12 | 13 | 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 | +} |
14 | 58 |
|
15 | 59 | const api: Prettier = { |
16 | 60 | async format(source, options) { |
17 | | - return prettier.format(source, { |
18 | | - ...defaults, |
19 | | - ...options, |
20 | | - plugins, |
21 | | - }) |
| 61 | + return prettier.format(source, await getOptions(options)) |
22 | 62 | }, |
23 | 63 |
|
24 | 64 | async formatWithCursor(source, options) { |
25 | | - return prettier.formatWithCursor(source, { |
26 | | - ...defaults, |
27 | | - ...options, |
28 | | - plugins, |
29 | | - }) |
| 65 | + return prettier.formatWithCursor(source, await getOptions(options)) |
30 | 66 | }, |
31 | 67 |
|
32 | 68 | async formatPreviewCSS(rules) { |
|
0 commit comments