-
Notifications
You must be signed in to change notification settings - Fork 43
/
vite.config.ts
77 lines (75 loc) · 2.7 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
import { defineConfig, UserConfigExport, loadEnv } from 'vite'
import externalGlobals from "rollup-plugin-external-globals";
import react from '@vitejs/plugin-react'
import { createHtmlPlugin } from 'vite-plugin-html'
const genScripts = ({ VITE_GA_TRACKING_ID, VITE_BAIDU_TRACKING_ID }: Record<string, string>) => {
let scripts = `
<script src="https://cdn.jsdelivr.net/npm/axios@0.27.2/dist/axios.min.js" integrity="sha256-43O3ClFnSFxzomVCG8/NH93brknJxRYF5tKRij3krg0=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/react@18.2.0/umd/react.production.min.js" integrity="sha256-S0lp+k7zWUMk2ixteM6HZvu8L9Eh//OVrt+ZfbCpmgY=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom@18.2.0/umd/react-dom.production.min.js" integrity="sha256-IXWO0ITNDjfnNXIu5POVfqlgYoop36bDzhodR6LW5Pc=" crossorigin="anonymous"></script>
`
if (VITE_BAIDU_TRACKING_ID) {
scripts += `
<script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?${VITE_BAIDU_TRACKING_ID}"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>
`
}
if (VITE_GA_TRACKING_ID) {
scripts += `
<script async src="https://www.googletagmanager.com/gtag/js?id=${VITE_GA_TRACKING_ID}"></script>
<script>window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', '${VITE_GA_TRACKING_ID}');</script>
`
}
return scripts
}
// https://vitejs.dev/config/
// https://github.com/vitejs/vite/issues/1930#issuecomment-783747858
export default ({ mode }) => {
const {
VITE_BASE_URL,
VITE_GA_TRACKING_ID,
VITE_BAIDU_TRACKING_ID
} = loadEnv(mode, process.cwd())
return defineConfig(
mode === 'production' ?
{
base: VITE_BASE_URL,
build: {
rollupOptions: {
external: ['react', 'react-dom'],
plugins: [
// https://github.com/vitejs/vite/issues/4398
externalGlobals({
react: 'React',
'react-dom': 'ReactDOM',
}),
],
}
},
plugins: [
react(),
createHtmlPlugin({
inject: {
data: {
injectScript: genScripts({ VITE_GA_TRACKING_ID, VITE_BAIDU_TRACKING_ID })
}
}
})
]
} :
{
server: {
port: 8080,
},
plugins: [
react(),
createHtmlPlugin({
inject: {
data: {
injectScript: ``
}
}
})
],
}
)
}