-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
144 lines (132 loc) · 3.73 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
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
import inject from "@rollup/plugin-inject";
import { sveltekit } from "@sveltejs/kit/vite";
import { readFileSync } from "fs";
import { dirname, join, resolve } from "path";
import { fileURLToPath } from "url";
import type { UserConfig } from "vite";
import { defineConfig, loadEnv } from "vite";
const file = fileURLToPath(new URL("package.json", import.meta.url));
const json = readFileSync(file, "utf8");
const { version } = JSON.parse(json);
// npm run dev = local
// npm run build = local
// dfx deploy = local
// dfx deploy --network ic = ic
const network = process.env.DFX_NETWORK ?? "local";
const readCanisterIds = ({
prefix,
}: {
prefix?: string;
}): Record<string, string> => {
const canisterIdsJsonFile =
network === "ic"
? join(process.cwd(), "canister_ids.json")
: join(process.cwd(), ".dfx", "local", "canister_ids.json");
try {
type Details = {
ic?: string;
local?: string;
};
const config: Record<string, Details> = JSON.parse(
readFileSync(canisterIdsJsonFile, "utf-8"),
);
return Object.entries(config).reduce((acc, current: [string, Details]) => {
const [canisterName, canisterDetails] = current;
return {
...acc,
[`${prefix ?? ""}${canisterName.toUpperCase()}_CANISTER_ID`]:
canisterDetails[network as keyof Details],
};
}, {});
} catch (e) {
throw Error(`Could not get canister ID from ${canisterIdsJsonFile}: ${e}`);
}
};
const config: UserConfig = {
plugins: [sveltekit()],
resolve: {
alias: {
$declarations: resolve("./src/declarations"),
},
},
build: {
target: "es2022",
rollupOptions: {
output: {
manualChunks: (id) => {
const folder = dirname(id);
if (
["@sveltejs", "svelte", "layercake", "@skeletonlabs"].find((lib) =>
folder.includes(lib),
) === undefined &&
folder.includes("node_modules")
) {
return "vendor";
}
return "index";
},
},
// Polyfill Buffer for production build
plugins: [
inject({
modules: { Buffer: ["buffer", "Buffer"] },
}),
],
},
},
// proxy /api to port 8000 during development
server: {
proxy: {
"/api": "http://localhost:8000",
},
watch: {
ignored: ["**/.dfx/**", "**/.github/**"],
},
},
// Node polyfill agent-js. Thanks solution shared by chovyfu on the Discord channel.
// https://stackoverflow.com/questions/71744659/how-do-i-deploy-a-sveltekit-app-to-a-dfinity-container
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: "globalThis",
},
// Enable esbuild polyfill plugins
plugins: [
NodeModulesPolyfillPlugin(),
{
name: "fix-node-globals-polyfill",
setup(build) {
build.onResolve(
{ filter: /_virtual-process-polyfill_\.js/ },
({ path }) => ({ path }),
);
},
},
],
},
},
worker: {
format: "es",
},
};
export default defineConfig(({ mode }: UserConfig): UserConfig => {
// Expand environment - .env files - with canister IDs
process.env = {
...process.env,
...loadEnv(mode ?? "development", process.cwd()),
...readCanisterIds({ prefix: "VITE_" }),
};
return {
...config,
// Backwards compatibility for auto generated types of dfx that are meant for webpack and process.env
define: {
"process.env": {
...readCanisterIds({}),
DFX_NETWORK: network,
},
VITE_APP_VERSION: JSON.stringify(version),
},
};
});