-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
122 lines (114 loc) · 2.65 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
import inject from '@rollup/plugin-inject';
import { sveltekit } from '@sveltejs/kit/vite';
import { basename, dirname, resolve } from 'node:path';
import { defineConfig, loadEnv, type UserConfig } from 'vite';
import { defineViteReplacements, readCanisterIds } from './vite.utils';
// npm run dev = local
// npm run build = local
// dfx deploy = local
// dfx deploy --network ic = ic
// dfx deploy --network beta = beta
// dfx deploy --network staging = staging
const network = process.env.DFX_NETWORK ?? 'local';
const config: UserConfig = {
plugins: [sveltekit()],
resolve: {
alias: {
$declarations: resolve('./src/declarations')
}
},
css: {
preprocessorOptions: {
scss: {
api: 'modern-compiler'
}
}
},
build: {
target: 'es2020',
rollupOptions: {
output: {
manualChunks: (id) => {
const folder = dirname(id);
const lazy = ['@dfinity/nns', '@dfinity/nns-proto', 'html5-qrcode', 'qr-creator'];
if (
['@sveltejs', 'svelte', '@dfinity/gix-components', ...lazy].find((lib) =>
folder.includes(lib)
) === undefined &&
folder.includes('node_modules')
) {
return 'vendor';
}
if (
lazy.find((lib) => folder.includes(lib)) !== undefined &&
folder.includes('node_modules')
) {
return 'lazy';
}
return 'index';
}
},
// Polyfill Buffer for production build
plugins: [
inject({
modules: { Buffer: ['buffer', 'Buffer'] }
})
],
external: (id) => {
// A list of file to exclude because we parse those manually with custom scripts.
const filename = basename(id);
return ['+oisy.page.css'].includes(filename);
}
}
},
// proxy /api to port 4943 during development
server: {
proxy: {
'/api': 'http://localhost:4943'
}
},
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis'
},
plugins: [
{
name: 'fix-node-globals-polyfill',
setup(build) {
build.onResolve({ filter: /_virtual-process-polyfill_\.js/ }, ({ path }) => ({ path }));
}
}
]
}
},
worker: {
format: 'es'
}
};
export default defineConfig((): UserConfig => {
// Expand environment - .env files - with canister IDs
process.env = {
...process.env,
...loadEnv(
network === 'ic'
? 'production'
: ['beta', 'staging'].includes(network)
? network
: '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
},
...defineViteReplacements()
}
};
});