-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.dev.ts
201 lines (177 loc) · 4.91 KB
/
webpack.dev.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
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
199
200
201
import axios from "axios";
import dotenv from "dotenv";
import https from "https";
import path from "path";
import createStyledComponentsTransformer from "typescript-plugin-styled-components";
import type { WebpackConfiguration } from "webpack-dev-server";
import { appData } from "./apps";
interface Credentials {
username: string;
password: string;
}
interface Session {
accessToken: string;
refreshToken: string;
expiration: string;
userId: string;
}
dotenv.config();
if (!process.env.USERNAME || !process.env.PASSWORD) {
throw new Error("Username and password must be provided");
}
let session: Session | undefined;
const credentials: Credentials = {
username: process.env.USERNAME,
password: process.env.PASSWORD
};
const apiProxyClient = axios.create({
baseURL: process.env.API_BASE_URL,
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
headers: {
"Digma-Access-Token": `Token ${process.env.API_TOKEN}`
}
});
const login = async ({ username, password }: Credentials) => {
const response = await apiProxyClient.post<{
accessToken: string;
refreshToken: string;
expiration: string;
userId: string;
}>("/authentication/login", {
username,
password
});
return response.data;
};
const refreshToken = async (session: Session) => {
const response = await apiProxyClient.post<{
accessToken: string;
refreshToken: string;
expiration: string;
userId: string;
}>("/authentication/refresh-token", {
accessToken: session.accessToken,
refreshToken: session.refreshToken
});
return response.data;
};
const getSession = async (
credentials: Credentials,
session: Session | undefined
) => {
if (!session) {
return await login(credentials);
} else {
const expiration = new Date(session.expiration).valueOf();
if (expiration < Date.now()) {
return await refreshToken(session);
}
}
return session;
};
const styledComponentsTransformer = createStyledComponentsTransformer();
const webApps = Object.entries(appData)
.filter(([, entry]) => entry.platforms.includes("Web"))
.map(([name]) => name);
const config: WebpackConfiguration = {
extends: path.resolve(__dirname, "./webpack.common.ts"),
mode: "development",
devtool: "eval-source-map",
devServer: {
historyApiFallback: {
rewrites: [
// Bypass HMR-related requests
{
from: /\.hot-update\.json$/,
to: ({ request }) => request.url
},
...webApps.map((app) => ({
from: new RegExp(`/${app}/`),
to: `/${app}/index.html`
}))
]
},
port: 3000,
proxy: [
{
context: ["/api"],
target: process.env.API_BASE_URL,
pathRewrite: { "^/api": "" },
secure: false,
changeOrigin: true
},
...(process.env.UI_BASE_URL && process.env.JAEGER_API_PATH
? [
{
context: [process.env.JAEGER_API_PATH],
target: `${process.env.UI_BASE_URL}${process.env.JAEGER_API_PATH}`,
pathRewrite: { "^/api": "" },
secure: false,
changeOrigin: true
}
]
: []),
{
context: ["/auth"],
target: process.env.AUTH_API_BASE_URL,
secure: false,
changeOrigin: true
}
],
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error("webpack-dev-server is not defined");
}
webApps.forEach((app) =>
devServer.app?.get(`/${app}/env.js`, (req, res) => {
const envVariables = {
// Put app environment variables here
};
let envFileContent = "";
Object.entries(envVariables).forEach(([name, value]) => {
envFileContent += `window.${name} = ${JSON.stringify(value)};\n`;
});
res.setHeader("Content-Type", "application/javascript");
res.end(envFileContent);
})
);
devServer.app?.use("/api", (req, res, next) => {
getSession(credentials, session)
.then((session) => {
req.headers.authorization = `Bearer ${session?.accessToken}`;
req.headers[
"Digma-Access-Token"
] = `Token ${process.env.API_TOKEN}`;
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error("Failed to enrich request with tokens", error);
})
.finally(() => {
next();
});
});
return middlewares;
},
static: {
directory: path.resolve(__dirname, "./dist")
}
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
configFile: path.resolve(__dirname, "./tsconfig.dev.json"),
getCustomTransformers: () => ({
before: [styledComponentsTransformer]
})
}
}
]
}
};
export default config;