-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvite.config.ts
174 lines (162 loc) · 4.52 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
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
import { defineConfig, Plugin } from "vite"
import react from "@vitejs/plugin-react"
import wasm from "vite-plugin-wasm"
import svgr from "vite-plugin-svgr"
import tsconfigPaths from "vite-tsconfig-paths"
import fs from "fs/promises"
import { resolve } from "node:path"
import { exec } from "child_process"
import Unfonts from "unplugin-fonts/vite"
import * as child from "child_process"
type Metadata = {
title?: string
description?: string
image?: string
}
type MetadataMap = Record<string, Metadata>
export const SEO_METADATA = {
index: {
title: "Hydration - An Ocean of Liquidity",
description:
"Hydration is a next-gen DeFi protocol which is designed to bring an ocean of liquidity to Polkadot. Our tool for the job the Hydration - an innovative Automated Market Maker (AMM) which unlocks unparalleled efficiencies by combining all assets in a single trading pool.",
image: "https://hydration.net/twitter-image.png",
},
referrals: {
image: "https://hydration.net/opengraph-image-ref.jpg",
},
} satisfies MetadataMap
const commitHash = child
.execSync("git rev-parse --short HEAD")
.toString()
.trim()
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
return {
define: {
"import.meta.env.VITE_COMMIT_HASH": JSON.stringify(commitHash),
},
build: {
target: "esnext",
outDir: "build",
},
optimizeDeps: {
esbuildOptions: {
target: "esnext",
},
},
esbuild: {
logOverride: { "this-is-undefined-in-esm": "silent" },
},
plugins: [
tsconfigPaths(),
react({
jsxImportSource: "@basilisk/jsx",
babel: {
plugins: ["@emotion/babel-plugin"],
},
}),
wasm(),
svgr(),
Unfonts({
custom: {
display: "swap",
prefetch: true,
injectTo: "head",
families: [
{
name: "Geist",
local: "Geist",
src: "./src/assets/fonts/Geist/Geist-Regular.ttf",
},
{
name: "GeistMedium",
local: "GeistMedium",
src: "./src/assets/fonts/Geist/Geist-Medium.ttf",
},
{
name: "GeistSemiBold",
local: "GeistSemiBold",
src: "./src/assets/fonts/Geist/Geist-SemiBold.ttf",
},
{
name: "GeistMono",
local: "GeistMono",
src: "./src/assets/fonts/GeistMono/GeistMono-Regular.otf",
},
{
name: "GeistMonoSemiBold",
local: "GeistMonoSemiBold",
src: "./src/assets/fonts/GeistMono/GeistMono-SemiBold.otf",
},
],
},
}),
transformIndexHtml(),
],
}
})
function transformIndexHtml(
options: {
templatePath?: string
indexFileName?: string
} = {},
): Plugin {
const { templatePath, indexFileName } = Object.assign(
{
indexFileName: "index.html",
templatePath: "./index.template.html",
},
options,
)
return {
name: "transform-index-html",
apply: "build",
config: async () => {
const template = await fs.readFile(resolve(__dirname, templatePath))
const { index, ...rest } = SEO_METADATA
const processFiles = Object.keys(SEO_METADATA).map(async (path) => {
const pageMeta = rest[path]
const metadata = {
...index,
...pageMeta,
}
const pagePath = resolve(
__dirname,
`pages/${path.replace("index", "")}`,
)
const filePath = `${pagePath}/${indexFileName}`
await fs.mkdir(pagePath, { recursive: true })
return fs.writeFile(
filePath,
template
.toString()
.replace(/<%=\s*(\w+)\s*%>/gi, (_match, p1) => metadata[p1] || ""),
)
})
await Promise.all(processFiles)
return {
build: {
rollupOptions: {
input: Object.fromEntries(
Object.entries(SEO_METADATA).map(([path]) => {
const entries = [
path,
resolve(
__dirname,
path === "index"
? `pages/${indexFileName}`
: `pages/${path}/${indexFileName}`,
),
]
return entries
}),
),
},
},
}
},
closeBundle: () => {
exec(`mv -f ./build/pages/* ./build && rm -rf ./pages`)
},
}
}