-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
127 lines (117 loc) · 3.03 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
import yaml from "@rollup/plugin-yaml"
import react from "@vitejs/plugin-react"
import { exec } from "node:child_process"
import { promisify } from "node:util"
import license from "rollup-plugin-license"
import { visualizer } from "rollup-plugin-visualizer"
import { defineConfig, PluginOption } from "vite"
import svg from "vite-plugin-svgr"
import tsConfigPaths from "vite-plugin-tsconfig-paths"
import tailwindConfig from "./tailwind.config"
const target = `http://${process.env.API_ENTRY}`
function gitcommit(): PluginOption {
return {
name: "git-commit",
enforce: "post",
async buildEnd(err) {
if (process.env.NODE_ENV !== "production") {
return
}
if (err != null) {
return
}
let tracked = false
try {
await promisify(exec)("git diff-index --quiet HEAD")
tracked = true
} catch {}
let matchLatestTag = false
if (tracked) {
try {
const latestCommit = (await promisify(exec)("git rev-parse HEAD")).stdout.trim()
const latestTag = (
await promisify(exec)("git rev-list --max-count=1 $(git describe --abbrev=0)")
).stdout.trim()
matchLatestTag = latestCommit === latestTag
} catch {}
}
try {
let version = "unknown"
if (!tracked) {
version = "untracked." + (await promisify(exec)("git rev-parse --verify HEAD")).stdout.trim()
} else if (matchLatestTag) {
version = (await promisify(exec)("git describe --abbrev")).stdout.trim()
} else {
const branch = (await promisify(exec)("git rev-parse --abbrev-ref HEAD")).stdout.trim()
const hash = (await promisify(exec)("git rev-parse --verify HEAD")).stdout.trim()
version = `${branch}.${hash}`
}
this.emitFile({
type: "asset",
name: "version",
fileName: "version",
source: version + "\n",
})
} catch (err) {
console.error(err)
}
},
}
}
function enableCrossOriginIsolated(): PluginOption {
return {
name: "configure-server",
configureServer(server) {
server.middlewares.use((_req, res, next) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin")
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp")
next()
})
},
}
}
export default defineConfig({
base: "",
build: {
sourcemap: true,
chunkSizeWarningLimit: 4 << 10,
},
plugins: [
enableCrossOriginIsolated(),
gitcommit(),
visualizer(),
yaml(),
svg({ include: "**/*.svg" }),
tsConfigPaths(),
react({
jsxImportSource: "@emotion/react",
babel: {
plugins: [["twobj", { tailwindConfig, throwError: true }], "@emotion"],
},
}),
license({
thirdParty: {
output: "dist/assets/vendor.LICENSE.txt",
},
}),
],
esbuild: {
logOverride: { "this-is-undefined-in-esm": "silent" },
banner: "/*! licenses: /assets/vendor.LICENSE.txt */",
legalComments: "none",
},
server: {
host: "0.0.0.0",
proxy: {
"^/apis/stream$": {
target,
configure(proxy, _) {
proxy.on("proxyReq", (proxyReq, request, response) => {
response.on("close", () => proxyReq.destroy())
})
},
},
"^/apis.*": target,
},
},
})