forked from dotkom/onlineweb4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.server.js
73 lines (64 loc) · 2.31 KB
/
webpack.server.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
// Not relevant for nodejs
/* eslint no-console: 0 */
// Breaks for some reason
/* eslint comma-dangle: ["error", {"functions":
"arrays": "always-multiline",
"objects": "always-multiline",
"imports": "always-multiline",
"exports": "always-multiline",
"functions": "ignore",
}] */
const config = require('./webpack.config.js');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const HTTPS = process.env.WEBPACK_DEV_HTTPS || false;
const IP = process.env.WEBPACK_DEV_IP || '0.0.0.0';
const PUBLIC_IP = process.env.WEBPACK_DEV_PUBLIC_IP || IP;
const PORT = process.env.WEBPACK_DEV_PORT || 3000;
const PUBLIC_PORT = process.env.WEBPACK_DEV_PUBLIC_PORT || PORT;
const HOST = `${PUBLIC_IP}:${PORT}`;
const PROTOCOL = HTTPS ? 'https' : 'http';
// Add hot reloading to all entries
// https://webpack.js.org/concepts/hot-module-replacement/
Object.keys(config.entry).forEach((entry) => {
if ({}.hasOwnProperty.call(config.entry, entry)) {
config.entry[entry].unshift(
`webpack-dev-server/client?http://${HOST}`,
'webpack/hot/dev-server'
);
}
});
// Remove [hash] since webpack-dev-server stores all generated copies in memory based on filename
config.output.filename = '[name].js';
// Entries will be served from a seperate http server instead of from filesystem
config.output.publicPath = process.env.WEBPACK_DEV_GITPOD === 'true' ? `${PROTOCOL}://${PUBLIC_IP}/static/` :
`${PROTOCOL}://${PUBLIC_IP}:${PUBLIC_PORT}/static/`;
console.log(config.output.publicPath)
// Don't reload if there is an error
config.plugins.unshift(new webpack.NoEmitOnErrorsPlugin());
// HMR
config.plugins.unshift(new webpack.HotModuleReplacementPlugin());
const compiler = webpack(config);
new WebpackDevServer(compiler, {
publicPath: config.output.publicPath,
// Enables Hot Module Reloading (only CSS and React atm)
hot: true,
// Adds some code that refreshes the page if the code changes
inline: true,
// TODO: Not sure if this is actually needed
historyApiFallback: true,
disableHostCheck: true,
// Allow CORS
headers: { 'Access-Control-Allow-Origin': '*' },
stats: {
// Hide some 'useless' info
chunks: false,
// Enables color output
colors: true,
},
}).listen(PORT, IP, (err) => {
if (err) {
console.error(err);
}
console.log(`Listening at ${HOST}`);
});