-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathvite.config.mts
198 lines (187 loc) · 5.15 KB
/
vite.config.mts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// @ts-strict-ignore
import * as path from 'path';
import inject from '@rollup/plugin-inject';
import basicSsl from '@vitejs/plugin-basic-ssl';
import react from '@vitejs/plugin-react-swc';
import { visualizer } from 'rollup-plugin-visualizer';
/// <reference types="vitest" />
import { defineConfig, loadEnv, Plugin } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';
import viteTsconfigPaths from 'vite-tsconfig-paths';
const addWatchers = (): Plugin => ({
name: 'add-watchers',
configureServer(server) {
server.watcher
.add([
path.resolve('../loot-core/lib-dist/electron/*.js'),
path.resolve('../loot-core/lib-dist/browser/*.js'),
])
.on('all', function () {
for (const wsc of server.ws.clients) {
wsc.send(JSON.stringify({ type: 'static-changed' }));
}
});
},
});
// Inject build shims using the inject plugin
const injectShims = (): Plugin[] => {
const buildShims = path.resolve('./src/build-shims.js');
const commonInject = {
exclude: ['src/setupTests.js'],
global: [buildShims, 'global'],
};
return [
{
name: 'inject-build-process',
config: () => ({
// rename process.env in build mode so it doesn't get set to an empty object up by the vite:define plugin
// this isn't needed in serve mode, because vite:define doesn't empty it in serve mode. And defines also happen last anyways in serve mode.
define: {
'process.env': `_process.env`,
},
}),
apply: 'build',
},
{
...inject({
...commonInject,
process: [buildShims, 'process'],
}),
enforce: 'post',
apply: 'serve',
},
{
...inject({
...commonInject,
_process: [buildShims, 'process'],
}),
enforce: 'post',
apply: 'build',
},
];
};
// https://vitejs.dev/config/
export default defineConfig(async ({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const devHeaders = {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
};
// Forward Netlify env variables
if (process.env.REVIEW_ID) {
process.env.REACT_APP_REVIEW_ID = process.env.REVIEW_ID;
}
let resolveExtensions = [
'.web.js',
'.web.jsx',
'.web.ts',
'.web.tsx',
'.mjs',
'.js',
'.mts',
'.ts',
'.jsx',
'.tsx',
'.json',
];
if (env.IS_GENERIC_BROWSER) {
resolveExtensions = [
'.browser.js',
'.browser.jsx',
'.browser.ts',
'.browser.tsx',
...resolveExtensions,
];
}
const browserOpen = env.BROWSER_OPEN ? `//${env.BROWSER_OPEN}` : true;
return {
base: '/',
envPrefix: 'REACT_APP_',
build: {
terserOptions: {
compress: false,
mangle: false,
},
target: 'es2022',
sourcemap: true,
outDir: mode === 'desktop' ? 'build-electron' : 'build',
assetsDir: 'static',
manifest: true,
assetsInlineLimit: 0,
chunkSizeWarningLimit: 1500,
rollupOptions: {
output: {
assetFileNames: assetInfo => {
const info = assetInfo.name.split('.');
let extType = info[info.length - 1];
if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
extType = 'img';
} else if (/woff|woff2/.test(extType)) {
extType = 'media';
}
return `static/${extType}/[name].[hash][extname]`;
},
chunkFileNames: 'static/js/[name].[hash].chunk.js',
entryFileNames: 'static/js/[name].[hash].js',
},
},
},
server: {
host: true,
headers: mode === 'development' ? devHeaders : undefined,
port: +env.PORT || 5173,
open: env.BROWSER
? ['chrome', 'firefox', 'edge', 'browser', 'browserPrivate'].includes(
env.BROWSER,
)
: browserOpen,
watch: {
disableGlobbing: false,
},
},
resolve: {
extensions: resolveExtensions,
},
plugins: [
// electron (desktop) builds do not support PWA
mode === 'desktop'
? undefined
: VitePWA({
registerType: 'prompt',
workbox: {
globPatterns: [
'**/*.{js,css,html,txt,wasm,sql,sqlite,ico,png,woff2,webmanifest}',
],
ignoreURLParametersMatching: [/^v$/],
navigateFallback: '/index.html',
navigateFallbackDenylist: [
/^\/account\/.*$/,
/^\/admin\/.*$/,
/^\/secret\/.*$/,
/^\/openid\/.*$/,
],
},
}),
injectShims(),
addWatchers(),
react({
plugins: [
[
'@swc/plugin-react-remove-properties',
{ properties: ['^data-debug'] },
],
],
devTarget: 'es2022',
}),
viteTsconfigPaths({ root: '../..' }),
visualizer({ template: 'raw-data' }),
!!env.HTTPS && basicSsl(),
],
test: {
include: ['src/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
environment: 'jsdom',
globals: true,
setupFiles: './src/setupTests.js',
},
};
});