-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
155 lines (138 loc) · 4.05 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import vue from '@vitejs/plugin-vue'
import fs from 'node:fs'
import path from 'node:path'
import { defineConfig, Plugin } from 'vite'
function css(): Plugin {
return {
name: 'vite-plugin-css-inject',
apply: 'build',
enforce: 'post',
generateBundle(_, bundle) {
const cssCode: string[] = []
for (const key in bundle) {
if (Object.prototype.hasOwnProperty.call(bundle, key)) {
const chunk = bundle[key]
if (chunk.type === 'asset' && chunk.fileName.endsWith('.css')) {
cssCode.push(<string>chunk.source)
delete bundle[key]
}
}
}
for (const key in bundle) {
if (Object.prototype.hasOwnProperty.call(bundle, key)) {
const chunk = bundle[key]
if (chunk.type === 'chunk' && /index-.*\.js$/.test(chunk.fileName)) {
const originalCode = chunk.code
const styleId = 'comfyui-image-browsing'
chunk.code = '(function(){var s=document.createElement("style");'
chunk.code += `s.type="text/css",s.dataset.styleId="${styleId}",`
chunk.code += 's.appendChild(document.createTextNode('
chunk.code += JSON.stringify(cssCode.join(''))
chunk.code += ')),document.head.appendChild(s);})();'
chunk.code += originalCode
}
}
}
},
}
}
function output(): Plugin {
return {
name: 'vite-plugin-output-fix',
apply: 'build',
enforce: 'post',
generateBundle(_, bundle) {
for (const key in bundle) {
const chunk = bundle[key]
if (chunk.type === 'asset') {
if (chunk.fileName === 'index.html') {
delete bundle[key]
}
}
if (chunk.fileName.startsWith('assets/')) {
chunk.fileName = chunk.fileName.replace('assets/', '')
}
}
},
}
}
function dev(): Plugin {
return {
name: 'vite-plugin-dev-fix',
apply: 'serve',
enforce: 'post',
configureServer(server) {
server.httpServer?.on('listening', () => {
const rootDir = server.config.root
const outDir = server.config.build.outDir
const outDirPath = path.join(rootDir, outDir)
if (fs.existsSync(outDirPath)) {
fs.rmSync(outDirPath, { recursive: true })
}
fs.mkdirSync(outDirPath)
const port = server.config.server.port
const content = `import "http://127.0.0.1:${port}/src/main.ts";`
fs.writeFileSync(path.join(outDirPath, 'manager-dev.js'), content)
})
},
}
}
function createWebVersion(): Plugin {
return {
name: 'vite-plugin-web-version',
apply: 'build',
enforce: 'post',
writeBundle() {
const pyProjectContent = fs.readFileSync('pyproject.toml', 'utf8')
const [, version] = pyProjectContent.match(/version = "(.*)"/) ?? []
const metadata = [
`version: ${version}`,
`build_time: ${new Date().toISOString()}`,
'',
].join('\n')
const metadataFilePath = path.join(__dirname, 'web', 'version.yaml')
fs.writeFileSync(metadataFilePath, metadata, 'utf-8')
},
}
}
export default defineConfig({
plugins: [vue(), css(), output(), dev(), createWebVersion()],
build: {
outDir: 'web',
minify: 'esbuild',
target: 'es2022',
sourcemap: true,
rollupOptions: {
// Disabling tree-shaking
// Prevent vite remove unused exports
treeshake: true,
output: {
manualChunks(id) {
if (id.includes('primevue')) {
return 'primevue'
}
},
},
},
chunkSizeWarningLimit: 1024,
},
resolve: {
alias: {
src: resolvePath('src'),
components: resolvePath('src/components'),
hooks: resolvePath('src/hooks'),
scripts: resolvePath('src/scripts'),
types: resolvePath('src/types'),
utils: resolvePath('src/utils'),
},
},
esbuild: {
minifyIdentifiers: false,
keepNames: true,
minifySyntax: true,
minifyWhitespace: true,
},
})
function resolvePath(str: string) {
return path.resolve(__dirname, str)
}