-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathung.vite.config.js
112 lines (108 loc) · 3.83 KB
/
ung.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
import react from '@vitejs/plugin-react';
import fs from 'fs/promises';
import path from 'path';
import { loadEnv } from 'vite';
import { createHtmlPlugin } from "vite-plugin-html";
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 = `/ung/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).then(() => console.log(`Deleted ${msWorker}`));
}
},
};
}
export default ({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, `${process.cwd()}/envDir/ung`) };
return defineConfig({
server: {
port: 9005,
proxy: {
'/ung/sak': {
target: process.env.APP_URL_UNG_SAK || 'http://localhost:8901',
changeOrigin: !!process.env.APP_URL_UNG_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_UNG_SAK)) {
// eslint-disable-next-line no-param-reassign, prefer-destructuring
proxyRes.headers.location = proxyRes.headers.location.split(process.env.APP_URL_UNG_SAK)[1];
}
if (proxyRes.statusCode === 401) {
// eslint-disable-next-line no-param-reassign
proxyRes.headers.location = '/ung/sak/resource/login';
}
});
},
},
'/ung/feature-toggle/toggles.json': createMockResponder('http://localhost:8901', staticJsonResponse(featureTogglesFactory())),
},
},
base: '/ung/web',
publicDir: './public',
plugins: [
createHtmlPlugin({
template: 'ung.html'
}),
react({
include: [/\.jsx$/, /\.tsx?$/],
}),
svgr(),
excludeMsw(),
{
// Endre namn på bygd entrypoint html frå ung.html til index.html
name: "rename-html-entry",
closeBundle: async () => {
const buildDir = path.join(__dirname, "dist/ung/web")
const oldPath = path.join(buildDir, "ung.html")
const newPath = path.join(buildDir, "index.html")
await fs.rename(oldPath, newPath)
}
}
],
build: {
// Relative to the root
outDir: './dist/ung/web',
sourcemap: true,
rollupOptions: {
input: './ung.html',
external: [
"mockServiceWorker.js"
],
},
},
});
};