/
startDevServer.js
53 lines (45 loc) · 1.45 KB
/
startDevServer.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
"use strict";
const path = require('path');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const consoleColors = require('colors');
const hotTransformers = require('./hotTransformers');
function startDevServer (options) {
options || (options = {});
establishPort.call(this, options);
const devServer = generateDevServer.call(this, options);
devServer.listen(this.port, 'localhost', consoleMessages.bind(this));
}
function establishPort(options) {
if (!options.immutableConfig) {
this.port = this.port || 4000;
}
}
function generateDevServer (options) {
const config = generateConfig.call(this, options);
const serverConfig = generateServerConfig.call(this);
const devServer = new WebpackDevServer(webpack(config), serverConfig);
return devServer;
}
function generateConfig (options) {
const config = this.generateConfig();
if (!options.immutableConfig && this.hot !== false) {
for (var transformerName in hotTransformers) {
hotTransformers[transformerName].call(this, config);
}
}
return config;
}
function generateServerConfig() {
return {
publicPath: this.output.publicPath,
hot: this.hot || true,
noInfo: true,
historyApiFallback: true
};
}
function consoleMessages(error) {
if (error) { return console.log(consoleColors.red(error)); }
return console.log(consoleColors.green(`Running at http://localhost:${this.port}`));
}
module.exports = startDevServer;