-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
120 lines (114 loc) · 3.26 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
import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import type { PluginOption } from 'vite'
import react from '@vitejs/plugin-react'
import { visualizer } from 'rollup-plugin-visualizer'
import { parse } from 'node-html-parser'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const prodPlugins = [react(), setNonceAttToScripts()]
const devPlugins = [react(), visualizer(), configurePreviewServer()]
const testPlugins = [react()]
const plugins =
mode === 'development'
? devPlugins
: mode === 'production'
? prodPlugins
: mode === 'test'
? testPlugins
: undefined
return {
base: '/ui',
plugins,
resolve: {
alias: {
'@/': `${path.resolve(__dirname, 'src')}/`,
},
},
build: {
target: 'es2017',
minify: mode !== 'development',
sourcemap: mode === 'development',
commonjsOptions: {
/** 'auto' does not work very well for mui's icons-material package */
defaultIsModuleExports(id) {
if (/@mui\/icons-material/.test(id)) return false
return 'auto'
},
},
chunkSizeWarningLimit: 1800,
},
envPrefix: 'REACT_APP_',
server: {
port: 3000,
},
test: {
globals: true,
environment: 'jsdom',
setupFiles: './setupTests.ts',
coverage: {
reporter: ['text', 'lcov'],
exclude: [
'**/node_modules/**',
'**/__tests__/**',
'**/__test__/**',
'**/__mocks__/**',
'**/api/agreement/**',
'**/api/attribute/**',
'**/api/auth/**',
'**/api/client/**',
'**/api/eservice/**',
'**/api/party/**',
'**/api/purpose/**',
'**/api/voucher/**',
'**/api/one-trust-notices/**',
'**/api/maintenance/**',
],
},
},
}
})
/**
* Adds nonce attribute with placeholder to all script tags in index.html
*/
function setNonceAttToScripts(): PluginOption {
return {
name: 'html-transform',
transformIndexHtml: {
enforce: 'post',
transform(html) {
const dom = parse(html)
dom.querySelectorAll('script').forEach((script) => {
script.setAttribute('nonce', '**CSP_NONCE**')
})
return dom.toString()
},
},
}
}
function configurePreviewServer(): PluginOption {
const testNonce = 'OXtve5rl0YunhKAkT+Qlww=='
const env = Object.assign(process.env, loadEnv('development', process.cwd(), ''))
return {
name: 'configure-preview-server',
configurePreviewServer(server) {
server.middlewares.use((req, res, next) => {
res.setHeader(
'Content-Security-Policy',
`default-src 'self'; object-src 'none'; connect-src 'self' ${env.REACT_APP_API_HOST}; script-src 'nonce-${testNonce}'; style-src 'self' 'unsafe-inline'; worker-src 'none'; font-src 'self'; img-src 'self' data:; base-uri 'self'`
)
next()
})
},
transformIndexHtml: {
enforce: 'post',
transform(html) {
const dom = parse(html)
dom.querySelectorAll('script').forEach((script) => {
script.setAttribute('nonce', testNonce)
})
return dom.toString().replace('**CSP_NONCE**', testNonce)
},
},
}
}