-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (52 loc) · 1.59 KB
/
Copy pathserver.js
File metadata and controls
57 lines (52 loc) · 1.59 KB
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
/* eslint-disable import/no-extraneous-dependencies,no-console */
require('dotenv').config();
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const express = require('express');
const compression = require('compression');
const historyApiFallback = require('connect-history-api-fallback');
const jsonServer = require('./json-server');
const webpackConfig = require('./webpack.config');
const port = process.env.PORT || 8000;
const server = express();
if (process.env.NODE_ENV !== 'production') {
const compiler = webpack(webpackConfig);
const webpackOptions = {
stats: {
assets: true,
chunks: false,
modules: false,
colors: true,
performance: true,
timings: true,
version: true,
warnings: true,
},
watchOptions: {
aggregateTimeout: 300,
poll: true,
ignored: /node_modules/,
},
publicPath: webpackConfig.output.publicPath,
};
compiler.apply(new webpack.ProgressPlugin());
server.use(historyApiFallback());
server.use(webpackDevMiddleware(compiler, webpackOptions));
server.use(webpackHotMiddleware(compiler));
server.listen(port, 'localhost', () => {
console.log(`Server listening on port ${port}`);
});
jsonServer.initialize(server);
} else {
webpack(webpackConfig, (err) => {
if (err) {
console.log(err);
return;
}
server.use(compression());
server.use(historyApiFallback());
server.use(express.static(webpackConfig.output.path));
server.listen(port);
});
}