-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathvite.config.mts
More file actions
170 lines (149 loc) · 4.97 KB
/
vite.config.mts
File metadata and controls
170 lines (149 loc) · 4.97 KB
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
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import babel from '@rolldown/plugin-babel'
import tailwindcss from '@tailwindcss/vite'
import react, { reactCompilerPreset } from '@vitejs/plugin-react'
import { codeInspectorPlugin } from 'code-inspector-plugin'
import type { PluginOption } from 'vite'
import { loadEnv } from 'vite'
import { checker } from 'vite-plugin-checker'
import { defineConfig } from 'vitest/config'
import PKG from './package.json'
import { adminRoutes } from './vite-plugins/admin-routes'
import { esToolkitCompatShim } from './vite-plugins/es-toolkit-compat-shim'
const __dirname = dirname(fileURLToPath(import.meta.url))
// dns.setDefaultResultOrder('verbatim')
export default ({ mode }) => {
const env = loadEnv(mode, process.cwd())
const { VITE_APP_PUBLIC_URL } = env
const isDev = mode === 'development'
return defineConfig({
plugins: [
// mkcert(),
esToolkitCompatShim(),
codeInspectorPlugin({ bundler: 'vite' }),
adminRoutes({ viewsDir: resolve(__dirname, 'src/views') }),
tailwindcss(),
react(),
babel({ presets: [reactCompilerPreset()] }),
checker({
enableBuild: true,
}),
htmlPlugin(env),
// nodePolyfills({
// // To exclude specific polyfills, add them to this list.
// exclude: [
// 'fs', // Excludes the polyfill for `fs` and `node:fs`.
// ],
// // Whether to polyfill `node:` protocol imports.
// protocolImports: true,
// }),
],
resolve: {
tsconfigPaths: true,
alias: {
path: 'path-browserify',
os: 'os-browserify',
'node-fetch': 'isomorphic-fetch',
buffer: 'buffer',
},
},
build: {
chunkSizeWarningLimit: 2500,
target: 'esnext',
// sourcemap: true,
rollupOptions: {
output: {
chunkFileNames: `js/[name]-[hash].js`,
entryFileNames: `js/[name]-[hash].js`,
manualChunks(id: string) {
if (!id.includes('node_modules')) return
const normalized = id.replaceAll('\\', '/')
if (normalized.includes('/react-dom/')) return 'vendor-react-dom'
if (
normalized.includes('/react/') ||
normalized.includes('/scheduler/')
) {
return 'vendor-react'
}
if (normalized.includes('/@tanstack/')) return 'vendor-tanstack'
if (normalized.includes('/@base-ui-components/')) {
return 'vendor-base-ui'
}
if (normalized.includes('/lucide-react/')) return 'vendor-icons'
if (
normalized.includes('/@lexical/') ||
normalized.includes('/lexical/')
) {
return 'editor-lexical'
}
if (normalized.includes('/katex/')) return 'editor-katex'
if (normalized.includes('/cytoscape/')) return 'editor-graph'
if (normalized.includes('/elkjs/')) return 'editor-elk'
if (normalized.includes('/roughjs/')) return 'editor-rough'
const haklexChunk = getScopedPackageChunk(
normalized,
'@haklex/',
'haklex',
)
if (haklexChunk) return haklexChunk
if (normalized.includes('/monaco-editor/')) return 'editor-monaco'
if (normalized.includes('/@antv/')) return 'vendor-charts'
},
},
},
},
optimizeDeps: {
exclude: ['@huacnlee/autocorrect', '@dqbd/tiktoken'],
},
define: {
__DEV__: isDev,
},
base: !isDev ? VITE_APP_PUBLIC_URL || '' : '',
server: {
// https: true,
port: 9528,
},
oxc: {
jsx: {
runtime: 'automatic',
importSource: 'react',
},
},
test: {
environment: 'happy-dom',
setupFiles: [resolve(__dirname, 'test/setup.ts')],
},
})
}
const htmlPlugin: (env: any) => PluginOption = (env) => {
return {
name: 'html-transform',
enforce: 'post',
transformIndexHtml(html) {
return html
.replace(
'<!-- MX SPACE ADMIN DASHBOARD VERSION INJECT -->',
`<script>window.version = '${PKG.version}';</script>`,
)
.replaceAll('@gh-pages', `@page_v${PKG.version}`)
.replace(
'<!-- ENV INJECT -->',
`<script id="env_injection">window.injectData = {WEB_URL:'${
env.VITE_APP_WEB_URL || ''
}', GATEWAY: '${env.VITE_APP_GATEWAY || ''}',BASE_API: '${
env.VITE_APP_BASE_API || ''
}'}</script>`,
)
},
}
}
function getScopedPackageChunk(id: string, scope: string, prefix: string) {
const marker = `/node_modules/${scope}`
const markerIndex = id.lastIndexOf(marker)
if (markerIndex === -1) return
const packagePath = id.slice(markerIndex + marker.length)
const packageName = packagePath.split('/')[0]
if (!packageName) return
return `${prefix}-${packageName.replaceAll(/[^\w-]/g, '-')}`
}