forked from sadernalwis/Ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
50 lines (47 loc) · 2 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
import path from "path";
import { defineConfig } from "vite";
import EnvironmentPlugin from "vite-plugin-environment";
import dotenv from "dotenv";
dotenv.config();
const isDevelopment = process.env.NODE_ENV !== "production";
const network = process.env.DFX_NETWORK || (process.env.NODE_ENV === "production" ? "ic" : "local");
function initCanisterEnv() {
let localCanisters, prodCanisters;
try { localCanisters = require( path.resolve(".dfx", "local", "canister_ids.json") ); }
catch (error) { console.log( "No local canister_ids.json found. Continuing production", path.resolve("../.dfx", "local", "canister_ids.json") ); }
try { prodCanisters = require(path.resolve("canister_ids.json")); }
catch (error) { console.log( "No production canister_ids.json found. Continuing with local" ); }
const canisterConfig = network === "local" ? localCanisters : prodCanisters;
return Object.entries(canisterConfig).reduce((prev, current) => {
const [canisterName, canisterDetails] = current;
prev[canisterName.toUpperCase() + "_CANISTER_ID"] =
canisterDetails[network];
return prev;
}, {});
}
const canisterEnvVariables = initCanisterEnv();
let environment = {is_development : isDevelopment, mode: process.env.NODE_ENV}
environment = Object.keys(process.env).reduce((accumulator, key) => {
const value = process.env[key];
if (key.includes("CANISTER") || key.includes("DFX")){ accumulator[key] = value; }
return accumulator; }, environment);
export default defineConfig({
root: path.resolve(__dirname, "src", "frontend"),
build: {
outDir: path.resolve(__dirname, "dist"),
emptyOutDir: true,
},
define: {
global: "window",
'process.env.NODE_ENV': JSON.stringify(canisterEnvVariables),
"process.env.__CANISTERS__": JSON.stringify(environment)
},
server: {
proxy: { "/api": { target: "http://localhost:4943", changeOrigin: true, }, },
},
plugins: [
EnvironmentPlugin("all", { prefix: "CANISTER_" }),
EnvironmentPlugin("all", { prefix: "DFX_" }),
EnvironmentPlugin({ BACKEND_CANISTER_ID: "" }),
],
});