forked from motoko-bootcamp/awesome-motoko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
77 lines (71 loc) · 2.04 KB
/
vite.config.js
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 { svelte } from "@sveltejs/vite-plugin-svelte";
import { defineConfig } from "vite";
import path from "path";
import fs from "fs";
export default ({ mode }) => {
const isDev = mode === "development";
let canisterIds;
try {
canisterIds = JSON.parse(
fs
.readFileSync(
isDev ? ".dfx/local/canister_ids.json" : "./canister_ids.json",
)
.toString(),
);
} catch (e) {}
// Generate canister ids, required by the generated canister code in .dfx/local/canisters/*
// This strange way of JSON.stringifying the value is required by vite
const canisterDefinitions = Object.entries(canisterIds).reduce(
(acc, [key, val]) => ({
...acc,
[`process.env.${key.toUpperCase()}_CANISTER_ID`]: isDev
? JSON.stringify(val.local)
: JSON.stringify(val.ic),
}),
{},
);
let dfxJson;
try {
dfxJson = JSON.parse(fs.readFileSync("./dfx.json").toString());
} catch (e) {}
// Gets the port dfx is running on from dfx.json
const DFX_PORT = dfxJson.networks.local.bind.split(":")[1];
return defineConfig({
define: {
// Here we can define global constants
// This is required for now because the code generated by dfx relies on process.env being set
...canisterDefinitions,
"process.env.NODE_ENV": JSON.stringify(
isDev ? "development" : "production",
),
},
optimizeDeps: {
exclude: ["tinro"],
},
plugins: [svelte()],
resolve: {
alias: {
// Here we tell Vite the "fake" modules that we want to define
$canisters: path.resolve("./src/declarations"),
$lib: path.resolve("./src/lib"),
},
},
server: {
fs: {
allow: ["."],
},
proxy: {
// This proxies all http requests made to /api to our running dfx instance
"/api": {
target: `http://localhost:${DFX_PORT}`,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, "/api"),
},
},
},
build: {
target: ["es2020"],
},
});
};