-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
304 lines (293 loc) · 9.53 KB
/
webpack.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const smp = new SpeedMeasurePlugin();
const basename = process.env.BASENAME || '/';
const pathPrefix = basename.endsWith('/') ? basename.slice(0, basename.length - 1) : basename;
const app = process.env.APP || 'dev';
const configFileName = (app === 'dev') ? 'default' : app;
// eslint-disable-next-line import/no-dynamic-require
const configFile = require(`./data/config/${configFileName}.json`);
const { DAPTrackingURL } = configFile;
const scriptSrcURLs = [];
const connectSrcURLs = [];
if (DAPTrackingURL) {
scriptSrcURLs.push(DAPTrackingURL);
connectSrcURLs.push(DAPTrackingURL);
}
if (process.env.DATA_UPLOAD_BUCKET) {
connectSrcURLs.push(`https://${process.env.DATA_UPLOAD_BUCKET}.s3.amazonaws.com`);
}
// add any extra URLs that should be whitelisted
if (configFile.connectSrcCSPWhitelist && configFile.connectSrcCSPWhitelist.length > 0) {
connectSrcURLs.push(...configFile.connectSrcCSPWhitelist);
}
if (configFile.featureFlags && configFile.featureFlags.discoveryUseAggMDS) {
connectSrcURLs.push('https://dataguids.org');
}
if (configFile.featureFlags && configFile.featureFlags.studyRegistration) {
connectSrcURLs.push('https://clinicaltrials.gov');
}
if (process.env.DATADOG_APPLICATION_ID && process.env.DATADOG_CLIENT_TOKEN) {
connectSrcURLs.push('https://*.logs.datadoghq.com');
}
if (process.env.MAPBOX_API_TOKEN) {
connectSrcURLs.push('https://*.tiles.mapbox.com');
connectSrcURLs.push('https://api.mapbox.com');
connectSrcURLs.push('https://events.mapbox.com');
}
const iFrameApplicationURLs = [];
if (configFile && configFile.analysisTools) {
configFile.analysisTools.forEach((e) => {
if (e.applicationUrl) {
iFrameApplicationURLs.push(e.applicationUrl);
}
});
}
const plugins = [
new webpack.EnvironmentPlugin(['NODE_ENV']),
new webpack.EnvironmentPlugin({ MOCK_STORE: null }),
new webpack.EnvironmentPlugin(['APP']),
new webpack.EnvironmentPlugin({ BASENAME: '/' }),
new webpack.EnvironmentPlugin(['LOGOUT_INACTIVE_USERS']),
new webpack.EnvironmentPlugin(['WORKSPACE_TIMEOUT_IN_MINUTES']),
new webpack.EnvironmentPlugin(['REACT_APP_PROJECT_ID']),
new webpack.EnvironmentPlugin(['REACT_APP_DISABLE_SOCKET']),
new webpack.EnvironmentPlugin(['TIER_ACCESS_LEVEL']),
new webpack.EnvironmentPlugin(['TIER_ACCESS_LIMIT']),
new webpack.EnvironmentPlugin(['FENCE_URL']),
new webpack.EnvironmentPlugin(['INDEXD_URL']),
new webpack.EnvironmentPlugin(['USE_INDEXD_AUTHZ']),
new webpack.EnvironmentPlugin(['WORKSPACE_URL']),
new webpack.EnvironmentPlugin(['WTS_URL']),
new webpack.EnvironmentPlugin(['MANIFEST_SERVICE_URL']),
new webpack.EnvironmentPlugin(['MAPBOX_API_TOKEN']),
new webpack.EnvironmentPlugin(['DATADOG_APPLICATION_ID']),
new webpack.EnvironmentPlugin(['DATADOG_CLIENT_TOKEN']),
new webpack.EnvironmentPlugin(['DATA_UPLOAD_BUCKET']),
new webpack.DefinePlugin({ // <-- key to reducing React's size
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'dev'),
LOGOUT_INACTIVE_USERS: !(process.env.LOGOUT_INACTIVE_USERS === 'false'),
WORKSPACE_TIMEOUT_IN_MINUTES: process.env.WORKSPACE_TIMEOUT_IN_MINUTES || 480,
REACT_APP_PROJECT_ID: JSON.stringify(process.env.REACT_APP_PROJECT_ID || 'search'),
REACT_APP_DISABLE_SOCKET: JSON.stringify(process.env.REACT_APP_DISABLE_SOCKET || 'true'),
},
}),
new HtmlWebpackPlugin({
title: configFile.components.appName || 'Generic Data Commons',
metaDescription: configFile.components.metaDescription || '',
basename: pathPrefix,
template: 'src/index.ejs',
connect_src: (function () {
const rv = {};
if (typeof process.env.FENCE_URL !== 'undefined') {
rv[(new URL(process.env.FENCE_URL)).origin] = true;
}
if (typeof process.env.INDEXD_URL !== 'undefined') {
rv[(new URL(process.env.INDEXD_URL)).origin] = true;
}
if (typeof process.env.WORKSPACE_URL !== 'undefined') {
rv[(new URL(process.env.WORKSPACE_URL)).origin] = true;
}
if (typeof process.env.WTS_URL !== 'undefined') {
rv[(new URL(process.env.WTS_URL)).origin] = true;
}
if (typeof process.env.MANIFEST_SERVICE_URL !== 'undefined') {
rv[(new URL(process.env.MANIFEST_SERVICE_URL)).origin] = true;
}
if (iFrameApplicationURLs.length > 0) {
iFrameApplicationURLs.forEach((url) => {
rv[(new URL(url)).origin] = true;
});
}
if (connectSrcURLs.length > 0) {
connectSrcURLs.forEach((url) => {
rv[(new URL(url)).origin] = true;
});
}
return Object.keys(rv).join(' ');
}()),
dap_url: DAPTrackingURL,
script_src: (function () {
const rv = {};
if (scriptSrcURLs.length > 0) {
scriptSrcURLs.forEach((url) => {
rv[(new URL(url)).origin] = true;
});
}
return Object.keys(rv).join(' ');
}()),
hash: true,
chunks: ['vendors~bundle', 'bundle'],
}),
/*
Can do this kind of thing to deploy multi-page apps in the future ...
new HtmlWebpackPlugin({
title: "Gen3 Workspaces",
filename: "workspaces.html",
template: 'src/index.ejs',
hash: true,
chunks: ['vendors~bundle~workspaceBundle', 'workspaceBundle']
}),
*/
new webpack.optimize.AggressiveMergingPlugin(), // Merge chunks
];
const allowedHosts = process.env.PORTAL_HOSTNAME ? [process.env.PORTAL_HOSTNAME] : 'auto';
let optimization = {};
let devtool = false;
if (process.env.NODE_ENV !== 'dev' && process.env.NODE_ENV !== 'auto') {
// optimization for production mode
optimization = {
splitChunks: {
chunks: 'all',
},
};
} else {
// add sourcemap tools for development mode
devtool = 'eval-source-map';
}
const entry = {
bundle: './src/index.jsx',
workspaceBundle: './src/workspaceIndex.jsx',
covid19Bundle: './src/covid19Index.jsx',
nctBundle: './src/nctIndex.jsx',
ecosystemBundle: './src/ecosystemIndex.jsx',
};
// if GEN3_BUNDLE is set with a value
if (process.env.GEN3_BUNDLE) {
switch (process.env.GEN3_BUNDLE) {
case 'workspace':
// Just build the workspace bundle as bundle.js
entry.bundle = entry.workspaceBundle;
delete entry.workspaceBundle;
delete entry.covid19Bundle;
delete entry.nctBundle;
delete entry.ecosystemBundle;
break;
case 'covid19':
entry.bundle = entry.covid19Bundle;
delete entry.workspaceBundle;
delete entry.covid19Bundle;
delete entry.nctBundle;
delete entry.ecosystemBundle;
break;
case 'nct':
entry.bundle = entry.nctBundle;
delete entry.workspaceBundle;
delete entry.covid19Bundle;
delete entry.nctBundle;
delete entry.ecosystemBundle;
break;
case 'heal':
case 'ecosystem':
entry.bundle = entry.ecosystemBundle;
delete entry.workspaceBundle;
delete entry.covid19Bundle;
delete entry.nctBundle;
delete entry.ecosystemBundle;
break;
default:
// by default we build for commons bundle
delete entry.workspaceBundle;
delete entry.covid19Bundle;
delete entry.nctBundle;
delete entry.ecosystemBundle;
break;
}
}
// else - if GEN3_BUNDLE is NOT set then build all entry points
// note that runWebpack ensures GEN3_BUNDLE is set,
// and the Dockerfile leaves it unset ...
module.exports = smp.wrap({
entry,
target: 'web',
externals: [{
xmlhttprequest: '{XMLHttpRequest:XMLHttpRequest}',
}],
mode: process.env.NODE_ENV !== 'dev' && process.env.NODE_ENV !== 'auto' ? 'production' : 'development',
output: {
path: __dirname + "/build",
filename: '[name].js',
publicPath: basename,
},
optimization,
devtool,
devServer: {
historyApiFallback: {
index: 'dev.html',
},
compress: true,
hot: true,
port: 9443,
server: 'https',
host: 'localhost',
allowedHosts,
client: {
overlay: {
warnings: false,
errors: true,
},
},
},
module: {
rules: [{
// test: /\.tsx?$/,
// exclude: /node_modules\/(?!(graphiql|graphql-language-service-parser)\/).*/,
// use: {
// loader: 'ts-loader',
// },
// },
// {
test: /\.jsx?$|\.tsx?$/,
exclude: /node_modules\/(?!(graphiql|graphql-language-service-parser)\/).*/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/react'],
plugins: ['@babel/plugin-proposal-class-properties'],
},
},
},
{
test: /\.less$/,
loaders: [
'style-loader',
'css-loader',
'less-loader',
],
},
{
test: /\.css$/,
loader: 'style-loader!css-loader',
},
{
test: /\.svg$/,
// loaders: ['babel-loader', 'react-svg-loader'], // to address the `css-what` vulnerability issue, after updating to webpack 5 and latest `react-svg-loader` we can switch back to this
loader: 'svg-react-loader',
},
{
test: /\.(png|jpg|gif|woff|ttf|eot)$/,
loaders: 'url-loader',
query: {
limit: 8192,
},
},
{
test: /\.flow$/,
loader: 'ignore-loader',
},
],
},
resolve: {
alias: {
graphql: path.resolve('./node_modules/graphql'),
react: path.resolve('./node_modules/react'), // Same issue.
graphiql: path.resolve('./node_modules/graphiql'),
'graphql-language-service-parser': path.resolve('./node_modules/graphql-language-service-parser'),
},
extensions: ['.mjs', '.js', '.jsx', '.ts', '.tsx', '.json'],
},
plugins,
});