-
Notifications
You must be signed in to change notification settings - Fork 6
/
vite.config.ts
139 lines (124 loc) · 4.14 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
import {defineConfig} from "vite";
import EnvironmentPlugin from "vite-plugin-environment";
import path from "path";
import dfxJson from "./dfx.json";
let localCanisters, prodCanisters, canisters;
let localEnv = true;
let network = "local";
function initCanisterIds() {
try {
localCanisters = require(path.resolve(
".dfx",
"local",
"canister_ids.json"
));
} catch (error) {
console.log("No local canister_ids.json found. Continuing production");
}
try {
prodCanisters = require(path.resolve("canister_ids.json"));
localEnv = false;
} catch (error) {
console.log("No production canister_ids.json found. Continuing with local");
}
network = process.env.NODE_ENV === "production" && !localEnv ? "ic" : "local";
canisters = network === "local" || localEnv ? localCanisters : prodCanisters;
for (const canister in canisters) {
process.env[canister.toUpperCase() + "_CANISTER_ID"] =
canisters[canister][network];
}
}
const isDevelopment = process.env.NODE_ENV !== "production" || localEnv;
initCanisterIds();
// const asset_entry = path.join("src", "frontend", "assets", "frontend", "index.html");
// List of all aliases for canisters
// This will allow us to: import { canisterName } from "canisters/canisterName"
const aliases = Object.entries(dfxJson.canisters).reduce(
(acc, [name, _value]) => {
// Get the network name, or `local` by default.
const networkName = process.env["DFX_NETWORK"] || "local";
const outputRoot = path.join(
__dirname,
".dfx",
networkName,
"canisters",
name
);
return {
...acc,
["canisters/" + name]: path.join(outputRoot, "index" + ".js"),
};
},
{}
);
// 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(canisters).reduce(
(acc, [key, val]) => ({
...acc,
[`process.env.${key.toUpperCase()}_CANISTER_ID`]: isDevelopment
? JSON.stringify(val.local)
: JSON.stringify(val.ic),
}),
{}
);
export default defineConfig({
// test: {
// environment: 'jsdom',
// globals: true,
// threads: false,
// watch: true,
// "compilerOptions": {
// "types": ["vitest/globals"]
// },
// // include: ['**/__tests__/*.{js,tsx,ts}', "App.test.tsx"],
// // globalSetup: ["./src/frontend/tests/React/setup.ts"],
// setupFiles: './setup_tests.ts',
// // reporters: ["default", {
// // async onWatcherRerun() {
// // await teardown();
// // await setup();
// // }
// // }]
// setupFilesAfterEnv: [
// '@testing-library/jest-dom/extend-expect', // Ensure jest-dom extensions are loaded
// './src/frontend/setupTests.ts',
// ],
// },
build: {
outDir: "build",
},
resolve: {
alias: {
// Here we tell Vite the "fake" modules that we want to define
...aliases,
},
},
server: {
fs: {
allow: ["."],
},
proxy: {
// This proxies all http requests made to /api to our running dfx instance
"/api": {
target: "http://localhost:4943",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, "/api"),
},
},
},
define: {
// Here we can define global constants
// This is required for now because the code generated by dfx relies on process.env being set
global: "window",
...canisterDefinitions,
"process.env.NODE_ENV": JSON.stringify(
isDevelopment ? "development" : "production"
),
},
plugins: [
EnvironmentPlugin("all", {prefix: "CANISTER_"}),
EnvironmentPlugin("all", {prefix: "DFX_"}),
EnvironmentPlugin({BACKEND_CANISTER_ID: ""}),
],
});