-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
130 lines (126 loc) · 4.72 KB
/
vite.config.js
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
import react from '@vitejs/plugin-react';
import fs from 'fs';
import path from 'path';
import { loadEnv } from 'vite';
import svgr from 'vite-plugin-svgr';
import { defineConfig } from 'vitest/config';
import { createMockResponder, staticJsonResponse } from "./_mocks/createMockResponder.js";
import { featureTogglesFactory } from "./_mocks/featureToggles.js";
const createProxy = (target, pathRewrite) => ({
target,
changeOrigin: !!target,
secure: false,
ws: false,
rewrite: p =>
pathRewrite ? p.replace(new RegExp(Object.keys(pathRewrite)[0]), pathRewrite[Object.keys(pathRewrite)[0]]) : p,
configure: proxy => {
proxy.on('proxyRes', (proxyRes, req, res) => {
if (proxyRes.statusCode === 401) {
// eslint-disable-next-line no-param-reassign
proxyRes.headers.location = `/k9/sak/resource/login?original=${req.originalUrl}`;
}
// Viss respons frå proxied server inneheld location header med server adresse, fjern server addressa slik at redirect
// går til dev server istadenfor proxied server. Dette for å unngå CORS feil når request går direkte til proxied server.
if (proxyRes.headers.location?.startsWith(target)) {
// eslint-disable-next-line no-param-reassign
proxyRes.headers.location = proxyRes.headers.location.replace(target, "")
}
});
},
});
function excludeMsw() {
return {
name: "exclude-msw",
resolveId(source) {
return source === "virtual-module" ? source : null;
},
renderStart(outputOptions, _inputOptions) {
const outDir = outputOptions.dir;
if (!outDir.includes('storybook')) {
const msWorker = path.resolve(outDir, "mockServiceWorker.js");
fs.rm(msWorker, () => console.log(`Deleted ${msWorker}`));
}
},
};
}
export default ({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, `${process.cwd()}/envDir`) };
return defineConfig({
server: {
port: 9000,
proxy: {
'/k9/formidling/dokumentdata': createProxy(process.env.APP_URL_K9FORMIDLING_DD || 'http://localhost:8294'),
'/k9/formidling': createProxy(process.env.APP_URL_K9FORMIDLING || 'http://localhost:8290'),
'/k9/sak': {
target: process.env.APP_URL_SAK || 'http://localhost:8080',
changeOrigin: !!process.env.APP_URL_SAK,
ws: false,
secure: false,
configure: proxy => {
proxy.on('proxyRes', (proxyRes, req, res) => {
if (proxyRes.headers.location && proxyRes.headers.location.startsWith(process.env.APP_URL_SAK)) {
// eslint-disable-next-line no-param-reassign, prefer-destructuring
proxyRes.headers.location = proxyRes.headers.location.split(process.env.APP_URL_SAK)[1];
}
if (proxyRes.statusCode === 401) {
// eslint-disable-next-line no-param-reassign
proxyRes.headers.location = '/k9/sak/resource/login';
}
});
},
},
'/k9/oppdrag': createProxy(process.env.APP_URL_K9OPPDRAG || 'http://localhost:8070'),
'/k9/klage': createProxy(process.env.APP_URL_KLAGE || 'http://localhost:8701'),
'/k9/tilbake': createProxy(process.env.APP_URL_K9TILBAKE || 'http://localhost:8030'),
'k9/endringslogg': createProxy(
process.env.ENDRINGSLOGG_URL || 'https://familie-endringslogg.intern.dev.nav.no',
{
'^/k9/endringslogg': '',
},
),
'/k9/feature-toggle/toggles.json': createMockResponder('http://localhost:8080', staticJsonResponse(featureTogglesFactory())),
},
},
base: '/k9/web',
publicDir: './public',
plugins: [
react({
include: [/\.jsx$/, /\.tsx?$/],
}),
svgr(),
excludeMsw()
],
build: {
// Relative to the root
outDir: './dist/k9/web',
sourcemap: true,
rollupOptions: {
external: [
"mockServiceWorker.js"
],
},
},
test: {
deps: {
inline: ['@navikt/k9-sak-typescript-client'], // Without this, tests using k9-sak-typescript-client through backend project failed.
interopDefault: true
},
environment: 'jsdom',
css: {
modules: {
classNameStrategy: 'non-scoped',
},
},
globals: true,
setupFiles: ['./vitest-setup.ts', './packages/utils-test/src/setup-test-env-hooks.ts'],
watch: false,
testTimeout: 15000,
onConsoleLog(log) {
// if (log.includes('Warning: ReactDOM.render is no longer supported in React 18.')) return false
return !log.includes(
'Download the React DevTools for a better development experience: https://reactjs.org/link/react-devtools',
);
},
}
});
};