-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
168 lines (153 loc) · 4.98 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
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
import react from '@vitejs/plugin-react';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import fs from 'node:fs';
import path from 'path';
import { PluginOption, UserConfig, defineConfig, loadEnv } from 'vite';
/* See https://github.com/mswjs/msw/discussions/712 */
function excludeMSWPlugin(): PluginOption {
return {
name: 'exclude-msw',
apply: 'build',
renderStart(outputOptions) {
const outDir = outputOptions.dir;
const msWorker = path.resolve(outDir || '', 'mockServiceWorker.js');
fs.rm(msWorker, () => console.log(`Deleted ${msWorker}`));
},
};
}
/* See https://stackoverflow.com/questions/69626090/how-to-watch-public-directory-in-vite-project-for-hot-reload allows
hot reloading when json files are modified in the public folder*/
function jsonHMR(): PluginOption {
return {
name: 'json-hmr',
enforce: 'post',
handleHotUpdate({ file, server }) {
if (file.endsWith('.json')) {
console.log('reloading json file...');
server.ws.send({
type: 'full-reload',
path: '*',
});
}
},
};
}
// Obtain default coverage config from vitest when not building for production
// (to avoid importing vitest during build as its a dev dependency)
let vitestDefaultExclude: string[] = [];
let vitestCoverageConfigDefaultsExclude: string[] = [];
if (process.env.NODE_ENV !== 'production') {
await import('vitest/config').then((vitestConfig) => {
vitestDefaultExclude = vitestConfig.defaultExclude;
vitestCoverageConfigDefaultsExclude =
vitestConfig.coverageConfigDefaults.exclude;
});
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
// Whether to output build files in a way SciGateway can load (the default for production unless e2e testing)
const buildLibrary =
env.NODE_ENV === 'production' && env.VITE_APP_BUILD_STANDALONE !== 'true';
// Whether to exclude MSW from the build
const excludeMSW = env.VITE_APP_INCLUDE_MSW !== 'true';
const plugins: PluginOption[] = [react()];
// Allow hot reloading of json files in public folder when in development
if (env.NODE_ENV === 'development') plugins.push(jsonHMR());
const config: UserConfig = {
plugins: plugins,
server: {
port: 3000,
},
preview: {
port: 5001,
},
define: {
// See https://vitejs.dev/guide/build.html#library-mode
// we need to replace here as the build in library mode won't
'process.env.NODE_ENV': JSON.stringify(env.NODE_ENV),
},
};
const rollupExternals: string[] = [];
// Exclude msw if necessary
if (excludeMSW) {
plugins.push(excludeMSWPlugin());
rollupExternals.push('msw');
}
if (buildLibrary) {
// Config for deployment in SciGateway
config.build = {
lib: {
// We use `umd` here as `es` causes some import statements to leak into the main.js, breaking the build
// removing this entirely uses a default of both, which for build results in `umd` taking precedence but when
// using --watch, `es` appears to replace it intermittently. Hopefully this can be fixed in the future and we
// can use `es` instead.
formats: ['umd'],
entry: 'src/main.tsx',
name: 'operationsgateway',
},
rollupOptions: {
external: ['react', 'react-dom'].concat(rollupExternals),
input: 'src/main.tsx',
output: {
entryFileNames: '[name].js',
chunkFileNames: '[name].chunk.js',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
preserveEntrySignatures: 'strict',
},
};
} else {
// Config for stand alone deployment e.g. for cypress
config.build = {
rollupOptions: {
input: ['src/main.tsx', './index.html'],
// Don't make react/react-dom external as not a library here, so have to bundle
external: rollupExternals,
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
preserveEntrySignatures: 'strict',
},
};
}
// Use browserslist config
config.build.target = browserslistToEsbuild();
return {
...config,
test: {
globals: true,
environment: 'jsdom',
globalSetup: './globalSetup.js',
setupFiles: ['src/setupTests.ts'],
exclude: [...vitestDefaultExclude, 'e2e/**'],
coverage: {
reporter: [
// Default
'text',
'html',
'clover',
'json',
// Extra for VSCode extension
['lcov', { outputFile: 'lcov.info', silent: true }],
],
exclude: [
...vitestCoverageConfigDefaultsExclude,
'public/*',
'server/*',
'playwright.config.ts',
// Leave handlers to show up unused code
'src/mocks/browser.ts',
'src/mocks/server.ts',
'src/vite-env.d.ts',
],
},
},
};
});