forked from surmon-china/surmon.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
141 lines (136 loc) · 4 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
/**
* @file vite config
* @module vite.config
* @author Surmon <https://github.com/surmon-china>
*/
import path from 'path'
import { loadEnv, defineConfig } from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import UnheadVite from '@unhead/addons/vite'
import packageJSON from './package.json'
const CWD = process.cwd()
const BASE_ENV_CONFIG = loadEnv('', CWD)
// https://vitejs.dev/config/#conditional-config
export default defineConfig(({ command, mode }) => {
const TARGET_ENV_CONFIG = loadEnv(mode, CWD)
console.info('vite config', {
command,
mode,
TARGET_ENV_CONFIG
})
return {
plugins: [vuePlugin(), UnheadVite()],
root: path.resolve(__dirname),
publicDir: 'public',
resolve: {
alias: {
'/@': path.resolve(__dirname, 'src')
}
},
define: {
__APP_VERSION__: JSON.stringify(packageJSON.version)
},
experimental: {
// CDN urls have more layers of subdirectories that need to be added at build time
// https://vitejs.dev/guide/build.html#advanced-base-options
renderBuiltUrl(filename: string, { hostType, type }) {
if (type === 'public' && hostType === 'css') {
// ref: transforms / url.ts -> CDNPrefix
return '/assets/' + filename
} else {
return '/' + filename
}
}
},
server: {
open: true,
port: 3000,
proxy: {
[BASE_ENV_CONFIG.VITE_API_PROXY_URL]: {
target: BASE_ENV_CONFIG.VITE_API_ONLINE_URL,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
},
css: {
preprocessorOptions: {
scss: {
charset: false,
additionalData: `$source-url: '${TARGET_ENV_CONFIG.VITE_FE_URL}';`
}
}
},
optimizeDeps: {
include: [
'highlight.js/lib/core',
'highlight.js/lib/languages/go',
'highlight.js/lib/languages/css',
'highlight.js/lib/languages/sql',
'highlight.js/lib/languages/php',
'highlight.js/lib/languages/xml',
'highlight.js/lib/languages/json',
'highlight.js/lib/languages/bash',
'highlight.js/lib/languages/less',
'highlight.js/lib/languages/scss',
'highlight.js/lib/languages/yaml',
'highlight.js/lib/languages/rust',
'highlight.js/lib/languages/shell',
'highlight.js/lib/languages/nginx',
'highlight.js/lib/languages/stylus',
'highlight.js/lib/languages/python',
'highlight.js/lib/languages/javascript',
'highlight.js/lib/languages/typescript'
],
exclude: [
// browser
'intersection-observer',
// Node.js
'express',
'lru-cache',
'node-schedule',
'cookie-parser',
'serialize-javascript',
'wonderful-bing-wallpaper'
]
},
build: {
sourcemap: true,
manifest: true,
rollupOptions: {
// disable vite warning
// https://github.com/vitejs/vite/pull/10331
// https://github.com/vitejs/vite/issues/10766
external: [/^\/images\/(?:[^/]+\/)*[^/]+$/],
output: {
entryFileNames: '[name]-[hash].js',
chunkFileNames: '[name]-[hash].js',
assetFileNames: '[name]-[hash].[ext]',
manualChunks(id) {
if (id.includes('node_modules')) {
const basics = [
'swiper',
'amplitude',
'emoji-233333',
'lozad',
'marked',
'@braintree/sanitize-url',
'serialize-javascript',
'highlight.js',
'ua-parse-js',
'intersection-observer'
]
if (basics.some((exp) => id.includes(`node_modules/${exp}`))) {
return 'basic'
} else if (id.includes('mapbox-gl')) {
return 'mapbox-gl'
} else {
return 'vendor'
}
}
}
}
}
}
}
})