-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdev-server.js
More file actions
62 lines (56 loc) · 2.02 KB
/
dev-server.js
File metadata and controls
62 lines (56 loc) · 2.02 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
58
59
60
61
62
import path from 'path';
import express from 'express';
import webpack from 'webpack';
import config from './webpack.config.dev';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
// Get Host Config Data
const host = require("./host.config");
const app = express();
const compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, {
//**** Start the server from specific path of project ****
contentBase: config.output.path, // "./dist" - see webpack.config.js the output path in dev case
// Hot Module Reload
hot: true,
historyApiFallback: true,
//The target file using in
publicPath: config.output.publicPath,
// display no info to console (only warnings and errors)
noInfo: false,
// display nothing to the console
quiet: true,
// Make the colors for the terminal logs
stats: {
colors: true
}
}));
app.use(webpackHotMiddleware(compiler));
// If we meet the readFileSystem error, that because of html webpack plugin
// failed to make index.html file, it cannot run the parse since some webpack compiling error
// by your code in src, so check some require function in your src code to see whether
// webpack is failed to require any file path
app.use(function (req, res, next) {
let filename = path.join(compiler.outputPath,'index.html');
compiler.outputFileSystem.readFile(filename, 'utf-8', function(err, result){
if (err) {
return next(err);
}
res.set('content-type','text/html');
res.send(result);
res.end();
});
});
app.listen(host.hostPort, host.hostIP, function(err) {
if (err) {
console.log(err);
return;
}
console.log("----------------------------------------------------------");
console.log();
console.log('\x1b[32m',"Server Started at PublicPath: " + config.output.publicPath);
console.log('\x1b[37m','');
console.log("----------------------------------------------------------");
console.log("Please, use the PublicPath above to work with browser for stability!");
console.log();
});