-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstart.js
123 lines (97 loc) · 3.55 KB
/
start.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
const webpack = require('webpack');
const nodemon = require('nodemon');
const rimraf = require('rimraf');
const webpackConfig = require('./config/webpack.config.js')(process.env.NODE_ENV || 'development');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const express = require('express');
const paths = require('./config/paths');
const { logMessage, compilerPromise } = require('./utils');
const app = express();
const WEBPACK_PORT =
process.env.WEBPACK_PORT ||
(!isNaN(Number(process.env.PORT)) ? Number(process.env.PORT) + 1 : 8501);
const start = async () => {
rimraf.sync(paths.clientBuild);
rimraf.sync(paths.serverBuild);
const [clientConfig, serverConfig] = webpackConfig;
clientConfig.entry.bundle = [
`webpack-hot-middleware/client?path=http://localhost:${WEBPACK_PORT}/__webpack_hmr`,
...clientConfig.entry.bundle,
];
clientConfig.output.hotUpdateMainFilename = 'updates/[hash].hot-update.json';
clientConfig.output.hotUpdateChunkFilename = 'updates/[id].[hash].hot-update.js';
const publicPath = clientConfig.output.publicPath;
clientConfig.output.publicPath = [`http://localhost:${WEBPACK_PORT}`, publicPath]
.join('/')
.replace(/([^:+])\/+/g, '$1/');
serverConfig.output.publicPath = [`http://localhost:${WEBPACK_PORT}`, publicPath]
.join('/')
.replace(/([^:+])\/+/g, '$1/');
const multiCompiler = webpack([clientConfig, serverConfig]);
const clientCompiler = multiCompiler.compilers[0];
const serverCompiler = multiCompiler.compilers[1];
const clientPromise = compilerPromise(clientCompiler);
const serverPromise = compilerPromise(serverCompiler);
const watchOptions = {
// poll: true,
ignored: /node_modules/,
stats: clientConfig.stats,
};
app.use((req, res, next) => {
res.header(
'Access-Control-Allow-Origin',
'http://localhost:3000'
);
return next();
});
app.use(
webpackDevMiddleware(clientCompiler, {
publicPath: clientConfig.output.publicPath,
stats: clientConfig.stats,
watchOptions,
})
);
app.use(webpackHotMiddleware(clientCompiler));
app.use('/static', express.static(paths.clientBuild));
app.listen(WEBPACK_PORT);
serverCompiler.watch(watchOptions, (error, stats) => {
if (!error && !stats.hasErrors()) {
console.log(stats.toString(serverConfig.stats));
return;
}
if (error) {
logMessage(error, 'error');
}
if (stats.hasErrors()) {
const info = stats.toJson();
const errors = info.errors[0].split('\n');
logMessage(errors[0], 'error');
logMessage(errors[1], 'error');
logMessage(errors[2], 'error');
}
});
// wait until client and server is compiled
try {
await serverPromise;
await clientPromise;
} catch (error) {
logMessage(error, 'error');
}
const script = nodemon({
script: `${paths.serverBuild}/server.js`,
ignore: ['src', 'scripts', 'config', './*.*', 'build/client'],
});
script.on('restart', () => {
logMessage('Server side app has been restarted.', 'warning');
});
script.on('quit', () => {
console.log('Process ended');
process.exit();
});
script.on('error', () => {
logMessage('An error occured. Exiting', 'error');
process.exit(1);
});
};
start();