|
1 |
| -import type { Plugin } from 'vite' |
2 |
| - |
3 |
| -import { resolveConfig, format } from 'prettier' |
| 1 | +import { prettierFormat } from 'vite-plugin-prettier-format' |
4 | 2 | import { defineConfig } from 'vite'
|
5 | 3 | import dts from 'vite-plugin-dts'
|
6 | 4 | import fs from 'node:fs/promises'
|
7 | 5 | import path from 'node:path'
|
8 | 6 |
|
9 |
| -let getAllFiles = async (directory: string): Promise<string[]> => { |
10 |
| - let entries = await fs.readdir(directory, { withFileTypes: true }) |
11 |
| - |
12 |
| - let childPathsPromises = entries.map(async entry => { |
13 |
| - let filePath = path.join(directory, entry.name) |
14 |
| - if (entry.isDirectory()) { |
15 |
| - return getAllFiles(filePath) |
16 |
| - } else if (entry.isFile()) { |
17 |
| - return [filePath] |
18 |
| - } |
19 |
| - return [] |
20 |
| - }) |
21 |
| - |
22 |
| - let nestedPaths = await Promise.all(childPathsPromises) |
23 |
| - return nestedPaths.flat() |
24 |
| -} |
25 |
| - |
26 |
| -let prettierPlugin = (): Plugin => { |
27 |
| - let outputDirectory: string = 'dist' |
28 |
| - return { |
29 |
| - closeBundle: async () => { |
30 |
| - let resolvedOutputDirectory = path.resolve(outputDirectory) |
31 |
| - |
32 |
| - if (!resolvedOutputDirectory) { |
33 |
| - console.warn( |
34 |
| - 'Output directory or file is not specified in the bundle options.', |
35 |
| - ) |
36 |
| - return |
37 |
| - } |
38 |
| - |
39 |
| - let files = await getAllFiles(resolvedOutputDirectory) |
40 |
| - |
41 |
| - await Promise.all( |
42 |
| - files.map(async file => { |
43 |
| - let fileContent = await fs.readFile(file, 'utf8') |
44 |
| - let prettierConfig = await resolveConfig(file) |
45 |
| - let formattedContent = await format(fileContent, { |
46 |
| - ...prettierConfig, |
47 |
| - filepath: file, |
48 |
| - }) |
49 |
| - await fs.writeFile(file, formattedContent, 'utf8') |
50 |
| - }), |
51 |
| - ) |
52 |
| - }, |
53 |
| - configResolved: config => { |
54 |
| - outputDirectory = config.build.outDir |
55 |
| - }, |
56 |
| - name: 'vite-plugin-prettier', |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 | 7 | export default defineConfig({
|
61 | 8 | build: {
|
62 | 9 | rollupOptions: {
|
@@ -104,7 +51,7 @@ export default defineConfig({
|
104 | 51 | strictOutput: true,
|
105 | 52 | rollupTypes: true,
|
106 | 53 | }),
|
107 |
| - prettierPlugin(), |
| 54 | + prettierFormat(), |
108 | 55 | ],
|
109 | 56 | test: {
|
110 | 57 | coverage: {
|
|
0 commit comments