-
Notifications
You must be signed in to change notification settings - Fork 7
/
vite.config.ts
78 lines (72 loc) · 2.04 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
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { imagetools } from 'vite-imagetools';
import { spawnSync } from 'child_process';
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)) {
// Create empty canisters
spawnSync('dfx', ['canister', 'create', '--all'], { cwd: __dirname });
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({
babel: {
plugins: ['babel-plugin-twin', 'babel-plugin-macros'],
ignore: ['\x00commonjsHelpers.js'], // Fix build error (ben-rogerson/babel-plugin-twin#9)
},
}),
imagetools(),
],
define: {
'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]: any) => [
`process.env.CANISTER_ID_${name.toUpperCase()}`,
JSON.stringify(ids[network] || ids[localNetwork]),
]),
),
},
optimizeDeps: {
esbuildOptions: {
define: {
// Patch CBOR library used in agent-js
global: 'globalThis',
},
},
},
server: {
// Local IC replica proxy
proxy: {
'/api': {
target: 'http://127.0.0.1:4943',
},
},
},
test: {
environment: 'jsdom',
setupFiles: './src/setupTests.ts',
coverage: {
reporter: ['text', 'json', 'html'],
},
},
});