forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
573 lines (509 loc) · 19.7 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable no-console, no-process-env */
const childProcess = require('child_process');
const http = require('http');
const https = require('https');
const path = require('path');
const url = require('url');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExternalTemplateRemotesPlugin = require('external-remotes-plugin');
const webpack = require('webpack');
const {ModuleFederationPlugin} = require('webpack').container;
const nodeExternals = require('webpack-node-externals');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const WebpackPwaManifest = require('webpack-pwa-manifest');
const LiveReloadPlugin = require('webpack-livereload-plugin');
// const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const packageJson = require('./package.json');
const NPM_TARGET = process.env.npm_lifecycle_event;
const targetIsRun = NPM_TARGET?.startsWith('run');
const targetIsTest = NPM_TARGET === 'test';
const targetIsStats = NPM_TARGET === 'stats';
const targetIsDevServer = NPM_TARGET?.startsWith('dev-server');
const targetIsEslint = NPM_TARGET === 'check' || NPM_TARGET === 'fix' || process.env.VSCODE_CWD;
const DEV = targetIsRun || targetIsStats || targetIsDevServer;
const boardsDevServerUrl = process.env.MM_BOARDS_DEV_SERVER_URL ?? 'http://localhost:9006';
const STANDARD_EXCLUDE = [
path.join(__dirname, 'node_modules'),
];
let publicPath = '/static/';
// Allow overriding the publicPath in dev from the exported SiteURL.
if (DEV) {
const siteURL = process.env.MM_SERVICESETTINGS_SITEURL || '';
if (siteURL) {
publicPath = path.join(new url.URL(siteURL).pathname, 'static') + '/';
}
}
// Track the build time so that we can bust any caches that may have incorrectly cached remote_entry.js from before we
// started setting Cache-Control: no-cache for that file on the server. This can be removed in 2024 after those cached
// entries are guaranteed to have expired.
const buildTimestamp = Date.now();
var config = {
entry: ['./root.tsx', 'root.html'],
output: {
publicPath,
filename: '[name].[contenthash].js',
chunkFilename: '[name].[contenthash].js',
clean: true,
},
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: STANDARD_EXCLUDE,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
// Babel configuration is in .babelrc because jest requires it to be there.
},
},
},
{
type: 'javascript/auto',
test: /\.json$/,
include: [
path.resolve(__dirname, 'i18n'),
],
exclude: [/en\.json$/],
use: [
{
loader: 'file-loader?name=i18n/[name].[contenthash].[ext]',
},
],
},
{
test: /\.(css|scss)$/,
use: [
DEV ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: ['sass'],
},
},
},
],
},
{
test: /\.(png|eot|tiff|svg|woff2|woff|ttf|gif|mp3|jpg)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'files/[contenthash].[ext]',
},
},
{
loader: 'image-webpack-loader',
options: {},
},
],
},
{
test: /\.apng$/,
use: [
{
loader: 'file-loader',
options: {
name: 'files/[contenthash].[ext]',
},
},
],
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
sources: false,
},
},
],
},
],
},
resolve: {
modules: [
'node_modules',
path.resolve(__dirname),
],
alias: {
'mattermost-redux/test': 'packages/mattermost-redux/test',
'mattermost-redux': 'packages/mattermost-redux/src',
reselect: 'packages/reselect/src',
},
extensions: ['.ts', '.tsx', '.js', '.jsx'],
fallback: {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
buffer: require.resolve('buffer/'),
},
},
performance: {
hints: 'warning',
},
target: 'web',
plugins: [
new webpack.ProvidePlugin({
process: 'process/browser',
}),
new webpack.DefinePlugin({
COMMIT_HASH: JSON.stringify(childProcess.execSync('git rev-parse HEAD || echo dev').toString()),
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[name].[contenthash].css',
}),
new HtmlWebpackPlugin({
filename: 'root.html',
inject: 'head',
template: 'root.html',
meta: {
csp: {
'http-equiv': 'Content-Security-Policy',
content: generateCSP(),
},
},
}),
new CopyWebpackPlugin({
patterns: [
{from: 'images/emoji', to: 'emoji'},
{from: 'images/img_trans.gif', to: 'images'},
{from: 'images/logo-email.png', to: 'images'},
{from: 'images/circles.png', to: 'images'},
{from: 'images/favicon', to: 'images/favicon'},
{from: 'images/appIcons.png', to: 'images'},
{from: 'images/warning.png', to: 'images'},
{from: 'images/logo-email.png', to: 'images'},
{from: 'images/browser-icons', to: 'images/browser-icons'},
{from: 'images/cloud', to: 'images'},
{from: 'images/welcome_illustration_new.png', to: 'images'},
{from: 'images/logo_email_blue.png', to: 'images'},
{from: 'images/logo_email_dark.png', to: 'images'},
{from: 'images/logo_email_gray.png', to: 'images'},
{from: 'images/forgot_password_illustration.png', to: 'images'},
{from: 'images/invite_illustration.png', to: 'images'},
{from: 'images/channel_icon.png', to: 'images'},
{from: 'images/add_payment_method.png', to: 'images'},
{from: 'images/add_subscription.png', to: 'images'},
{from: 'images/c_avatar.png', to: 'images'},
{from: 'images/c_download.png', to: 'images'},
{from: 'images/c_socket.png', to: 'images'},
{from: 'images/admin-onboarding-background.jpg', to: 'images'},
{from: 'images/payment-method-illustration.png', to: 'images'},
{from: 'images/cloud-laptop.png', to: 'images'},
{from: 'images/cloud-laptop-error.png', to: 'images'},
{from: 'images/cloud-laptop-warning.png', to: 'images'},
{from: 'images/cloud-upgrade-person-hand-to-face.png', to: 'images'},
],
}),
// Generate manifest.json, honouring any configured publicPath. This also handles injecting
// <link rel="apple-touch-icon" ... /> and <meta name="apple-*" ... /> tags into root.html.
new WebpackPwaManifest({
name: 'Mattermost',
short_name: 'Mattermost',
start_url: '..',
description: 'Mattermost is an open source, self-hosted Slack-alternative',
background_color: '#ffffff',
inject: true,
ios: true,
fingerprints: false,
orientation: 'any',
filename: 'manifest.json',
icons: [{
src: path.resolve('images/favicon/android-chrome-192x192.png'),
type: 'image/png',
sizes: '192x192',
}, {
src: path.resolve('images/favicon/apple-touch-icon-120x120.png'),
type: 'image/png',
sizes: '120x120',
ios: true,
}, {
src: path.resolve('images/favicon/apple-touch-icon-144x144.png'),
type: 'image/png',
sizes: '144x144',
ios: true,
}, {
src: path.resolve('images/favicon/apple-touch-icon-152x152.png'),
type: 'image/png',
sizes: '152x152',
ios: true,
}, {
src: path.resolve('images/favicon/apple-touch-icon-57x57.png'),
type: 'image/png',
sizes: '57x57',
ios: true,
}, {
src: path.resolve('images/favicon/apple-touch-icon-60x60.png'),
type: 'image/png',
sizes: '60x60',
ios: true,
}, {
src: path.resolve('images/favicon/apple-touch-icon-72x72.png'),
type: 'image/png',
sizes: '72x72',
ios: true,
}, {
src: path.resolve('images/favicon/apple-touch-icon-76x76.png'),
type: 'image/png',
sizes: '76x76',
ios: true,
}, {
src: path.resolve('images/favicon/favicon-16x16.png'),
type: 'image/png',
sizes: '16x16',
}, {
src: path.resolve('images/favicon/favicon-32x32.png'),
type: 'image/png',
sizes: '32x32',
}, {
src: path.resolve('images/favicon/favicon-96x96.png'),
type: 'image/png',
sizes: '96x96',
}],
}),
// Disabling this plugin until we come up with better bundle analysis ci
// new BundleAnalyzerPlugin({
// analyzerMode: 'disabled',
// generateStatsFile: true,
// statsFilename: 'bundlestats.json',
// }),
],
};
function generateCSP() {
let csp = 'script-src \'self\' cdn.rudderlabs.com/ js.stripe.com/v3';
if (DEV) {
// react-hot-loader and development source maps require eval
csp += ' \'unsafe-eval\'';
csp += ' ' + boardsDevServerUrl;
}
return csp;
}
async function initializeModuleFederation() {
function makeSharedModules(packageNames, singleton) {
const sharedObject = {};
for (const packageName of packageNames) {
const version = packageJson.dependencies[packageName];
sharedObject[packageName] = {
requiredVersion: singleton ? version : undefined,
singleton,
strictVersion: singleton,
version,
};
}
return sharedObject;
}
function isWebpackDevServerAvailable(baseUrl) {
return new Promise((resolve) => {
if (!DEV) {
resolve(false);
return;
}
const requestModule = baseUrl.startsWith('https:') ? https : http;
const req = requestModule.request(`${baseUrl}/remote_entry.js`, (response) => {
return resolve(response.statusCode === 200);
});
req.setTimeout(100, () => {
// If this times out, we've connected to the dev server even if it's not ready yet
resolve(true);
});
req.on('error', () => {
resolve(false);
});
req.end();
});
}
async function getRemoteContainers() {
const products = [
{name: 'boards', baseUrl: boardsDevServerUrl},
];
const remotes = {};
if (process.env.MM_DONT_INCLUDE_PRODUCTS) {
console.warn('Skipping initialization of products');
} else if (DEV) {
// For development, identify which product dev servers are available
// Wait for 5 seconds for product dev servers to start up if they were started at the same time as this one
await new Promise((resolve) => setTimeout(resolve, 5000));
const productsFound = await Promise.all(products.map((product) => isWebpackDevServerAvailable(product.baseUrl)));
for (let i = 0; i < products.length; i++) {
const product = products[i];
const found = productsFound[i];
if (found) {
console.log(`Product ${product.name} found, adding as remote module`);
remotes[product.name] = `${product.name}@${product.baseUrl}/remote_entry.js`;
} else {
console.log(`Product ${product.name} not found`);
}
}
} else {
// For production, hardcode the URLs of product containers to be based on the web app URL
for (const product of products) {
remotes[product.name] = `${product.name}@[window.basename]/static/products/${product.name}/remote_entry.js?bt=${buildTimestamp}`;
}
}
const aliases = {};
for (const product of products) {
if (remotes[product.name]) {
continue;
}
// Add false aliases to prevent Webpack from trying to resolve the missing modules
aliases[product.name] = false;
aliases[`${product.name}/manifest`] = false;
}
return {remotes, aliases};
}
const {remotes, aliases} = await getRemoteContainers();
const moduleFederationPluginOptions = {
name: 'mattermost_webapp',
remotes,
shared: [
// Shared modules will be made available to other containers (ie products and plugins using module federation).
// To allow for better sharing, containers shouldn't require exact versions of packages like the web app does.
// Other containers will use these shared modules if their required versions match. If they don't match, the
// version packaged with the container will be used.
'@mattermost/client',
'@mattermost/types',
'prop-types',
makeSharedModules([
'luxon',
], false),
// Other containers will be forced to use the exact versions of shared modules that the web app provides.
makeSharedModules([
'history',
'react',
'react-beautiful-dnd',
'react-bootstrap',
'react-dom',
'react-intl',
'react-redux',
'react-router-dom',
], true),
],
};
// Desktop specific code for remote module loading
moduleFederationPluginOptions.exposes = {
'./app': 'components/app.jsx',
'./store': 'stores/redux_store.jsx',
'./styles': './sass/styles.scss',
'./registry': 'module_registry',
};
moduleFederationPluginOptions.filename = `remote_entry.js?bt=${buildTimestamp}`;
config.plugins.push(new ModuleFederationPlugin(moduleFederationPluginOptions));
// Add this plugin to perform the substitution of window.basename when loading remote containers
config.plugins.push(new ExternalTemplateRemotesPlugin());
config.resolve.alias = {
...config.resolve.alias,
...aliases,
};
config.plugins.push(new webpack.DefinePlugin({
REMOTE_CONTAINERS: JSON.stringify(remotes),
}));
}
if (DEV) {
// Development mode configuration
config.mode = 'development';
config.devtool = 'eval-cheap-module-source-map';
} else {
// Production mode configuration
config.mode = 'production';
config.devtool = 'source-map';
}
const env = {
STRIPE_PUBLIC_KEY: JSON.stringify(process.env.STRIPE_PUBLIC_KEY || ''),
};
if (DEV) {
env.PUBLIC_PATH = JSON.stringify(publicPath);
env.RUDDER_KEY = JSON.stringify(process.env.RUDDER_KEY || '');
env.RUDDER_DATAPLANE_URL = JSON.stringify(process.env.RUDDER_DATAPLANE_URL || '');
if (process.env.MM_LIVE_RELOAD) {
config.plugins.push(new LiveReloadPlugin());
}
} else {
env.NODE_ENV = JSON.stringify('production');
env.RUDDER_KEY = JSON.stringify(process.env.RUDDER_KEY || '');
env.RUDDER_DATAPLANE_URL = JSON.stringify(process.env.RUDDER_DATAPLANE_URL || '');
}
config.plugins.push(new webpack.DefinePlugin({
'process.env': env,
}));
// Test mode configuration
if (targetIsTest) {
config.entry = ['./root.tsx'];
config.target = 'node';
config.externals = [nodeExternals()];
}
if (targetIsDevServer) {
const proxyToServer = {
logLevel: 'silent',
target: process.env.MM_SERVICESETTINGS_SITEURL ?? 'http://localhost:8065',
xfwd: true,
};
config = {
...config,
devtool: 'eval-cheap-module-source-map',
devServer: {
liveReload: true,
proxy: {
// Forward these requests to the server
'/api': {
...proxyToServer,
ws: true,
},
'/plugins': proxyToServer,
'/static/plugins': proxyToServer,
},
port: 9005,
devMiddleware: {
writeToDisk: false,
},
historyApiFallback: {
index: '/static/root.html',
},
},
performance: false,
optimization: {
...config.optimization,
splitChunks: false,
},
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
'react-dom': '@hot-loader/react-dom',
},
},
};
}
// Export PRODUCTION_PERF_DEBUG=1 when running webpack to enable support for the react profiler
// even while generating production code. (Performance testing development code is typically
// not helpful.)
// See https://reactjs.org/blog/2018/09/10/introducing-the-react-profiler.html and
// https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977
if (process.env.PRODUCTION_PERF_DEBUG) {
console.log('Enabling production performance debug settings'); //eslint-disable-line no-console
config.resolve.alias['react-dom'] = 'react-dom/profiling';
config.resolve.alias['schedule/tracing'] = 'schedule/tracing-profiling';
config.optimization = {
// Skip minification to make the profiled data more useful.
minimize: false,
};
}
if (targetIsEslint) {
// ESLint can't handle setting an async config, so just skip the async part
module.exports = config;
} else {
module.exports = async () => {
// Do this asynchronously so we can determine whether which remote modules are available
await initializeModuleFederation();
return config;
};
}