-
Notifications
You must be signed in to change notification settings - Fork 22
/
vite.config.ts
163 lines (153 loc) · 4.21 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
import { fileURLToPath, URL } from "url";
import { resolve } from "path";
import { readFileSync, existsSync } from "node:fs";
import {
defineConfig,
loadEnv,
Plugin,
createFilter,
transformWithEsbuild,
} from "vite";
import react from "@vitejs/plugin-react";
import setupProxy from "./src/setupProxy";
import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";
import environment from "vite-plugin-environment";
import dotenv from "dotenv";
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
return {
build: {
outDir: "build",
emptyOutDir: true,
rollupOptions: {
output: {
manualChunks: {
vendor: [
"react",
"react-dom",
"react-markdown",
"prettier-plugin-motoko",
],
xterm: ["@xterm/xterm", "@xterm/addon-fit"],
agent: [
"@dfinity/agent",
"@dfinity/candid",
"@dfinity/principal",
"@dfinity/identity",
],
},
},
},
},
plugins: [
react(),
environment("all", { prefix: "CANISTER_" }),
environment("all", { prefix: "DFX_" }),
wasm(),
topLevelAwait(),
devServerPlugin(),
svgrPlugin(),
setupProxyPlugin(),
],
define: {
global: "globalThis",
},
worker: {
format: "es",
},
publicDir: "public",
resolve: {
alias: [
{
find: "declarations",
replacement: fileURLToPath(
new URL("./src/declarations", import.meta.url),
),
},
],
},
};
});
// Setup HOST, SSL, PORT
// Migration guide: Follow the guides below
// https://vitejs.dev/config/server-options.html#server-host
// https://vitejs.dev/config/server-options.html#server-https
// https://vitejs.dev/config/server-options.html#server-port
function devServerPlugin(): Plugin {
return {
name: "dev-server-plugin",
config(_, { mode }) {
const { HOST, PORT, HTTPS, SSL_CRT_FILE, SSL_KEY_FILE } = loadEnv(
mode,
".",
["HOST", "PORT", "HTTPS", "SSL_CRT_FILE", "SSL_KEY_FILE"],
);
const https = HTTPS === "true";
return {
server: {
host: HOST || "0.0.0.0",
port: parseInt(PORT || "3000", 10),
open: true,
...(https &&
SSL_CRT_FILE &&
SSL_KEY_FILE && {
https: {
cert: readFileSync(resolve(SSL_CRT_FILE)),
key: readFileSync(resolve(SSL_KEY_FILE)),
},
}),
},
};
},
};
}
// In Create React App, SVGs can be imported directly as React components. This is achieved by svgr libraries.
// https://create-react-app.dev/docs/adding-images-fonts-and-files/#adding-svgs
function svgrPlugin(): Plugin {
const filter = createFilter("**/*.svg");
const postfixRE = /[?#].*$/s;
return {
name: "svgr-plugin",
async transform(code, id) {
if (filter(id)) {
const { transform } = await import("@svgr/core");
const { default: jsx } = await import("@svgr/plugin-jsx");
const filePath = id.replace(postfixRE, "");
const svgCode = readFileSync(filePath, "utf8");
const componentCode = await transform(svgCode, undefined, {
filePath,
caller: {
previousExport: code,
defaultPlugins: [jsx],
},
});
const res = await transformWithEsbuild(componentCode, id, {
loader: "jsx",
});
return {
code: res.code,
map: null,
};
}
},
};
}
// Configuring the Proxy Manually
// https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually
// https://vitejs.dev/guide/api-plugin.html#configureserver
// Migration guide: Follow the guide below and remove src/setupProxy
// https://vitejs.dev/config/server-options.html#server-proxy
function setupProxyPlugin(): Plugin {
return {
name: "setup-proxy-plugin",
config() {
return {
server: { proxy: {} },
};
},
configureServer(server) {
setupProxy(server.middlewares);
},
};
}