-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
127 lines (116 loc) · 3.13 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
import { defineConfig, loadEnv } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import replace from "@rollup/plugin-replace";
import { createHtmlPlugin } from "vite-plugin-html";
import { readFileSync } from "fs";
import { VitePWA } from "vite-plugin-pwa";
import * as packageJson from "./package.json";
type ContractsAddresses = {
[key: string]: string;
};
type Networks = {
[key: string]: ContractsAddresses;
};
const nodeEnv = process.env.NODE_ENV?.toLowerCase() || "development";
const buildPwaOptions = (env) =>
({
mode: process.env.NODE_ENV ? "production" : "development",
base: "/",
manifest: {
short_name: env.HTML_TITLE,
name: env.HTML_TITLE,
description: "DAO mission control",
icons: [
{
src: `/images/${env.VITE_PROJECT_KEY}/logo-192.png`,
type: "image/png",
sizes: "192x192",
purpose: "any maskable",
},
{
src: `/images/${env.VITE_PROJECT_KEY}/logo-512.png`,
type: "image/png",
sizes: "512x512",
purpose: "any maskable",
},
],
start_url: "/",
background_color: "#000000",
display: "standalone",
scope: "/",
theme_color: "#000000",
shortcuts: [
{
name: "Your Tasks",
short_name: "Tasks",
description: "View all your tasks and do time tracking",
url: "/#/tasks",
icons: [
{ src: "/images/shortcut-time-tracking-192.png", sizes: "192x192" },
],
},
],
},
devOptions: {
enabled: !process.env.NODE_ENV,
type: "module",
navigateFallback: "index.html",
},
} as any);
function getAddresses(chainId: string, dirs: string[]) {
let networks = {} as Networks;
for (let dir of dirs) {
networks = {
...JSON.parse(readFileSync(dir, "utf8")),
};
}
if (!networks[chainId]) {
throw new Error(`Cannot find contracts for chainId ${chainId}`);
}
return networks[chainId];
}
// https://stackoverflow.com/a/35778030/597097
const gitRevision = require("child_process")
.execSync("git rev-parse HEAD")
.toString()
.trim();
// https://vitejs.dev/config/
export default async ({ mode }) => {
const env = loadEnv(mode, process.cwd(), [
"VITE_",
"HTML_",
"SMART_CONTRACT_",
]);
const smartContractPath = env?.SMART_CONTRACT_PATH;
if (!smartContractPath) {
console.error("Cannod find smart contract directory");
process.exit(1);
}
const chainId = env.VITE_ETHEREUM_CHAIN_ID;
const contractsAddresses = getAddresses(
chainId,
smartContractPath.split(";")
);
const pwaOptions = buildPwaOptions(env);
return defineConfig({
base: "./",
plugins: [
svelte(),
VitePWA(pwaOptions),
replace({
__VITE_CONTRACTS_ADDRESSES__: JSON.stringify(contractsAddresses),
__BUILD_DATE__: new Date().toISOString(),
__GIT_REVISION__: gitRevision,
__VERSION__: packageJson.version,
__ENV__: nodeEnv,
}),
createHtmlPlugin({
inject: {
data: {
...env,
},
},
}),
],
});
};