-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.server.js
97 lines (87 loc) · 2.34 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require('fs');
const proc = require('child_process');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const nodeExternals = require('webpack-node-externals');
const StartServerPlugin = require('start-server-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
const constants = require('./src/server/constants');
const __GIT_SHA__ = proc.execSync('git rev-parse --short HEAD').toString().trim();
function readFileSync(file) {
return fs.readFileSync(file).toString();
}
function isDevMode(env) {
return Boolean(env && env.dev);
}
function getDevTool(env) {
if (isDevMode(env)) {
return 'cheap-eval-source-map';
}
return 'source-map';
}
function getEntry(env) {
const entry = [];
if (isDevMode(env)) {
entry.push('webpack/hot/poll?1000');
}
entry.push('./src/server/index.js');
return entry;
}
function getPlugins(env) {
const globals = {
__CLIENT_CSS__: JSON.stringify('client.css'),
__CLIENT_JS__: JSON.stringify('client.js'),
__GIT_SHA__: JSON.stringify(__GIT_SHA__),
'process.env.NODE_ENV': JSON.stringify(
isDevMode(env) ? 'development' : 'production',
),
};
const plugins = [];
if (isDevMode(env)) {
plugins.push(
new StartServerPlugin('server.js'),
new webpack.HotModuleReplacementPlugin(),
new OpenBrowserPlugin({ url: `http://0.0.0.0:${constants.PORT}` }),
);
} else {
globals.__CLIENT_CSS__ = JSON.stringify(readFileSync(path.join('dist', 'client.css')));
globals.__CLIENT_JS__ = JSON.stringify(readFileSync(path.join('dist', 'client.js')));
}
plugins.push(
new webpack.BannerPlugin(`Version: ${__GIT_SHA__}`),
new webpack.DefinePlugin(globals),
);
return plugins;
}
module.exports = function config(env) {
return {
devtool: getDevTool(env),
entry: getEntry(env),
externals: [
nodeExternals({
whitelist: [
'webpack/hot/poll?1000',
],
}),
],
mode: isDevMode(env) ? 'development' : 'none',
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: [
/src\/client/,
/src\/server/,
],
},
],
},
output: {
filename: 'server.js',
},
plugins: getPlugins(env),
target: 'node',
watch: Boolean(isDevMode(env)),
};
};