-
Notifications
You must be signed in to change notification settings - Fork 43
/
vite.config.ts
116 lines (108 loc) · 2.95 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import type { Plugin } from 'vite'
import { resolveConfig, format } from 'prettier'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
import fs from 'node:fs/promises'
import path from 'node:path'
let getAllFiles = async (directory: string): Promise<string[]> => {
let files = await fs.readdir(directory)
files = (await Promise.all(
files.map(async file => {
let filePath = path.join(directory, file)
let stats = await fs.stat(filePath)
if (stats.isDirectory()) {
return getAllFiles(filePath)
} else if (stats.isFile()) {
return filePath
}
return null
}),
)) as string[]
return files
.reduce((all: string[], folderContents) => [...all, folderContents], [])
.flat()
}
let prettierPlugin = (): Plugin => {
let outputDirectory: string = 'dist'
return {
closeBundle: async () => {
let resolvedOutputDirectory = path.resolve(outputDirectory)
if (!resolvedOutputDirectory) {
console.warn(
'Output directory or file is not specified in the bundle options.',
)
return
}
let files = await getAllFiles(resolvedOutputDirectory)
await Promise.all(
files.map(async file => {
let fileContent = await fs.readFile(file, 'utf8')
let prettierConfig = await resolveConfig(file)
let formattedContent = await format(fileContent, {
...prettierConfig,
filepath: file,
})
await fs.writeFile(file, formattedContent, 'utf8')
}),
)
},
configResolved: config => {
outputDirectory = config.build.outDir
},
name: 'vite-plugin-prettier',
}
}
export default defineConfig({
build: {
rollupOptions: {
onwarn: (warning, warn) => {
let suppressedCodes = ['MIXED_EXPORTS']
if (!suppressedCodes.includes(warning.code ?? '')) {
warn(warning)
}
},
output: {
preserveModules: true,
exports: 'auto',
},
external: (id: string) => !id.startsWith('.') && !path.isAbsolute(id),
},
lib: {
fileName: (_format, entryName) => `${entryName}.js`,
entry: path.resolve(__dirname, 'index.ts'),
name: 'eslint-plugin-perfectionist',
formats: ['cjs'],
},
minify: false,
},
plugins: [
dts({
afterBuild: async () => {
await fs.writeFile(
'dist/index.d.ts',
`${(await fs.readFile('dist/index.d.ts'))
.toString()
.replace(/\nexport .+/u, '')}export = _default`,
)
},
include: [
path.join(__dirname, 'index.ts'),
path.join(__dirname, 'typings'),
path.join(__dirname, 'rules'),
path.join(__dirname, 'utils'),
],
insertTypesEntry: true,
strictOutput: true,
rollupTypes: true,
}),
prettierPlugin(),
],
test: {
coverage: {
thresholds: {
100: true,
},
all: false,
},
},
})