-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathwebpack.dev.js
More file actions
175 lines (167 loc) · 5.54 KB
/
webpack.dev.js
File metadata and controls
175 lines (167 loc) · 5.54 KB
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
const { execSync } = require('child_process');
const path = require('path');
const { merge } = require('webpack-merge');
const { setupWebpackDotenvFilesForEnv, setupDotenvFilesForEnv } = require('./dotenv');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const smp = new SpeedMeasurePlugin({ disable: !process.env.MEASURE });
const env = process.env.DEV_ENV ?? 'development';
const isTilt = env === 'tilt';
setupDotenvFilesForEnv({ env });
const webpackCommon = require('./webpack.common.js');
const RELATIVE_DIRNAME = process.env._RELATIVE_DIRNAME;
const IS_PROJECT_ROOT_DIR = process.env._IS_PROJECT_ROOT_DIR;
const SRC_DIR = process.env._SRC_DIR;
const COMMON_DIR = process.env._COMMON_DIR;
const PUBLIC_PATH = process.env._PUBLIC_PATH;
const DIST_DIR = process.env._DIST_DIR;
const HOST = process.env._HOST;
const PORT = process.env._PORT;
const PROXY_PROTOCOL = process.env._PROXY_PROTOCOL;
const PROXY_HOST = process.env._PROXY_HOST;
const PROXY_PORT = process.env._PROXY_PORT;
const DEPLOYMENT_MODE = process.env._DEPLOYMENT_MODE;
const AUTH_METHOD = process.env._AUTH_METHOD;
const BASE_PATH = DEPLOYMENT_MODE === 'kubeflow' ? '/workspaces' : PUBLIC_PATH;
// Get the kubeconfig token at startup as a fallback for standalone dev mode.
const getKubeconfigToken = () => {
try {
const token = execSync(
"kubectl config view --raw --minify --flatten -o jsonpath='{.users[].user.token}'",
)
.toString()
.trim();
const username = execSync("kubectl auth whoami -o jsonpath='{.status.userInfo.username}'")
.toString()
.trim();
// eslint-disable-next-line no-console
console.info('Logged in as user:', username);
return token;
} catch (error) {
// eslint-disable-next-line no-console
console.error('Failed to get Kubernetes token:', error.message);
return '';
}
};
const fallbackToken = AUTH_METHOD === 'user_token' ? getKubeconfigToken() : '';
// Build static proxy headers for auth methods that don't need dynamic forwarding.
const getProxyHeaders = () => {
if (AUTH_METHOD === 'internal') {
return {
'kubeflow-userid': 'user@example.com',
};
}
// For user_token, auth headers are set dynamically in onProxyReq below.
return {};
};
// When using user_token auth, dynamically forward the authorization header from the
// incoming request if present (e.g. from a host backend proxy with dev impersonation).
// Fall back to the kubeconfig token captured at startup for standalone dev mode.
const onProxyReq = (proxyReq, req) => {
if (AUTH_METHOD !== 'user_token') {
return;
}
const incomingAuth = req.headers.authorization;
if (incomingAuth) {
proxyReq.setHeader('Authorization', incomingAuth);
const token = incomingAuth.replace(/^Bearer\s+/i, '');
proxyReq.setHeader('x-forwarded-access-token', token);
} else if (fallbackToken) {
proxyReq.setHeader('Authorization', `Bearer ${fallbackToken}`);
proxyReq.setHeader('x-forwarded-access-token', fallbackToken);
}
};
module.exports = smp.wrap(
merge(
{
plugins: [
...setupWebpackDotenvFilesForEnv({
directory: RELATIVE_DIRNAME,
env,
isRoot: IS_PROJECT_ROOT_DIR,
}),
],
},
webpackCommon('development'),
{
mode: 'development',
devtool: 'eval-source-map',
optimization: {
runtimeChunk: 'single',
removeEmptyChunks: true,
},
devServer: {
host: HOST,
port: PORT,
compress: true,
historyApiFallback: {
index: `${BASE_PATH}/index.html`.replace('//', '/'),
},
// In Tilt mode, disable HMR and live reload since the Istio gateway
// doesn't route WebSocket traffic. Webpack still watches files and
// rebuilds on changes; just refresh the browser manually.
hot: !isTilt,
liveReload: !isTilt,
open: [BASE_PATH],
proxy: [
{
context: ['/api', '/workspaces/api'],
target: {
host: PROXY_HOST,
protocol: PROXY_PROTOCOL,
port: PROXY_PORT,
},
changeOrigin: true,
headers: getProxyHeaders(),
onProxyReq,
},
],
devMiddleware: {
stats: 'errors-only',
publicPath: BASE_PATH,
},
client: isTilt
? false
: {
overlay: false,
},
static: {
directory: DIST_DIR,
publicPath: BASE_PATH,
},
onListening: (devServer) => {
if (devServer) {
// eslint-disable-next-line no-console
console.log(
`\x1b[32m✓ Dashboard available at: \x1b[4mhttp://localhost:${
devServer.server.address().port
}\x1b[0m`,
);
}
},
},
module: {
rules: [
{
test: /\.css$/,
include: [
SRC_DIR,
COMMON_DIR,
path.resolve(RELATIVE_DIRNAME, 'node_modules/@patternfly'),
path.resolve(
RELATIVE_DIRNAME,
'node_modules/mod-arch-shared/node_modules/@patternfly',
),
],
use: ['style-loader', 'css-loader'],
},
],
},
plugins: [
new ForkTsCheckerWebpackPlugin(),
new ReactRefreshWebpackPlugin({ overlay: false }),
],
},
),
);