-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
91 lines (82 loc) · 2.2 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
import inject from '@rollup/plugin-inject';
import { sveltekit } from '@sveltejs/kit/vite';
import { readFileSync } from 'fs';
import { join } from 'path';
import type { UserConfig } from 'vite';
import { defineConfig, loadEnv } from 'vite';
// npm run dev = local
// npm run build = local
// dfx deploy = local
// dfx deploy --network ic = ic
const network = process.env.DFX_NETWORK ?? 'local';
const host = network === 'local' ? 'http://localhost:8080' : 'https://ic0.app';
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()],
build: {
target: 'esnext',
rollupOptions: {
// Polyfill Buffer for production build
plugins: [
inject({
modules: { Buffer: ['buffer', 'Buffer'] }
})
]
}
},
server: {
proxy: {
'/api': 'http://localhost:8080'
}
},
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: 'globalThis'
}
}
}
};
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_' }),
VITE_DFX_NETWORK: network,
VITE_HOST: host
};
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
}
}
};
});