-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev-server.js
80 lines (66 loc) · 1.71 KB
/
dev-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
import path from 'path';
import pm2 from 'pm2';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import configCreator from './webpack.config.creator';
const server = true;
const prod = false;
const serverConfig = configCreator(server, prod);
const clientConfig = configCreator(!server, prod);
const serverHotConfig = {
...serverConfig,
entry: [
'webpack/hot/poll?1000',
...serverConfig.entry
],
plugins: [
...serverConfig.plugins,
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.NamedModulesPlugin()
]
};
const clientHotConfig = {
...clientConfig,
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:8080/',
'webpack/hot/only-dev-server',
...clientConfig.entry
],
plugins: [
...clientConfig.plugins,
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.NamedModulesPlugin()
]
};
const serverCompiler = webpack({ ...serverHotConfig });
const clientCompiler = webpack({ ...clientHotConfig });
const clientDevServer = new WebpackDevServer(clientCompiler, {
hot: true,
inline: true,
proxy: {
'/': 'http://localhost:3000',
},
publicPath: clientConfig.output.publicPath
});
clientDevServer.listen(8080);
serverCompiler.run((err, stats) => {
if (err) {
console.error(err);
return;
}
console.log('Server compiled');
pm2.connect(err => {
if (err) {
console.error('Error:', err);
process.exit(2);
}
pm2.start(path.resolve('pm2.json'), err => {
pm2.disconnect();
if (err) throw err
});
});
serverCompiler.watch({hot: true, inline: true}, (err, stats) => undefined);
});