-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
47 lines (43 loc) · 1.38 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
import react from '@vitejs/plugin-react';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { defineConfig } from 'vite';
const localNetwork = 'local';
const network = process.env['DFX_NETWORK'] || localNetwork;
let canisterIdPath: string;
if (network === localNetwork) {
// Local replica canister IDs
canisterIdPath = join(__dirname, '.dfx/local/canister_ids.json');
} else {
// Custom canister IDs
canisterIdPath = join(__dirname, 'canister_ids.json');
}
if (!existsSync(canisterIdPath)) {
throw new Error(
'Unable to find canisters. Running `dfx deploy` should fix this problem.',
);
}
const canisterIds = JSON.parse(readFileSync(canisterIdPath, 'utf8'));
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
define: {
global: 'window',
'process.env.DFX_NETWORK': JSON.stringify(process.env.DFX_NETWORK),
// Expose canister IDs provided by `dfx deploy`
...Object.fromEntries(
Object.entries(canisterIds).map(([name, ids]) => [
`process.env.${name.toUpperCase()}_CANISTER_ID`,
JSON.stringify(ids[network] || ids[localNetwork]),
]),
),
},
server: {
// Local IC replica proxy
proxy: {
'/api': {
target: 'http://127.0.0.1:4943',
},
},
},
});