forked from Comfy-Org/ComfyUI_frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.mts
180 lines (151 loc) · 4.37 KB
/
vite.config.mts
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { defineConfig, Plugin } from 'vite'
import type { UserConfigExport } from 'vitest/config'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import dotenv from "dotenv"
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Components from 'unplugin-vue-components/vite'
dotenv.config()
const IS_DEV = process.env.NODE_ENV === 'development'
const SHOULD_MINIFY = process.env.ENABLE_MINIFY === 'true'
interface ShimResult {
code: string
exports: string[]
}
function isLegacyFile(id: string): boolean {
return id.endsWith('.ts') && (
id.includes("src/extensions/core") ||
id.includes("src/scripts")
)
}
function comfyAPIPlugin(): Plugin {
return {
name: 'comfy-api-plugin',
transform(code: string, id: string) {
if (IS_DEV) return null
if (isLegacyFile(id)) {
const result = transformExports(code, id)
if (result.exports.length > 0) {
const projectRoot = process.cwd()
const relativePath = path.relative(path.join(projectRoot, 'src'), id)
const shimFileName = relativePath.replace(/\.ts$/, '.js')
const shimComment = `// Shim for ${relativePath}\n`
this.emitFile({
type: "asset",
fileName: shimFileName,
source: shimComment + result.exports.join("")
})
}
return {
code: result.code,
map: null // If you're not modifying the source map, return null
}
}
}
}
}
function transformExports(code: string, id: string): ShimResult {
const moduleName = getModuleName(id)
const exports: string[] = []
let newCode = code
// Regex to match different types of exports
const regex = /export\s+(const|let|var|function|class|async function)\s+([a-zA-Z$_][a-zA-Z\d$_]*)(\s|\()/g
let match
while ((match = regex.exec(code)) !== null) {
const name = match[2]
// All exports should be bind to the window object as new API endpoint.
if (exports.length == 0) {
newCode += `\nwindow.comfyAPI = window.comfyAPI || {};`
newCode += `\nwindow.comfyAPI.${moduleName} = window.comfyAPI.${moduleName} || {};`
}
newCode += `\nwindow.comfyAPI.${moduleName}.${name} = ${name};`
exports.push(`export const ${name} = window.comfyAPI.${moduleName}.${name};\n`)
}
return {
code: newCode,
exports
}
}
function getModuleName(id: string): string {
// Simple example to derive a module name from the file path
const parts = id.split('/')
const fileName = parts[parts.length - 1]
return fileName.replace(/\.\w+$/, '') // Remove file extension
}
const DEV_SERVER_COMFYUI_URL = process.env.DEV_SERVER_COMFYUI_URL || 'http://127.0.0.1:8188'
export default defineConfig({
base: '',
server: {
proxy: {
'/internal': {
target: DEV_SERVER_COMFYUI_URL,
},
'/api': {
target: DEV_SERVER_COMFYUI_URL,
// Return empty array for extensions API as these modules
// are not on vite's dev server.
bypass: (req, res, options) => {
if (req.url === '/api/extensions') {
res.end(JSON.stringify([]))
}
return null
},
},
'/ws': {
target: DEV_SERVER_COMFYUI_URL,
ws: true
},
'/testsubrouteindex': {
target: 'http://localhost:5173',
rewrite: (path) => path.substring('/testsubrouteindex'.length)
}
}
},
plugins: [
vue(),
comfyAPIPlugin(),
Icons({
'compiler': 'vue3'
}),
Components({
dts: true,
resolvers: [IconsResolver()],
dirs: ['src/components', 'src/layout', 'src/views'],
deep: true,
extensions: ['vue']
})
],
build: {
minify: SHOULD_MINIFY ? 'esbuild' : false,
target: 'es2022',
sourcemap: true,
rollupOptions: {
// Disabling tree-shaking
// Prevent vite remove unused exports
treeshake: false
}
},
esbuild: {
minifyIdentifiers: false,
keepNames: true,
minifySyntax: SHOULD_MINIFY,
minifyWhitespace: SHOULD_MINIFY
},
test: {
globals: true,
environment: 'happy-dom',
setupFiles: ['./vitest.setup.ts']
},
define: {
'__COMFYUI_FRONTEND_VERSION__': JSON.stringify(process.env.npm_package_version)
},
resolve: {
alias: {
'@': '/src'
}
},
optimizeDeps: {
exclude: ['@comfyorg/litegraph']
}
}) as UserConfigExport