1 | 'use strict';
|
2 |
|
3 | const express = require('express');
|
4 | const webpack = require('webpack');
|
5 | const makeWebpackConfig = require('./make-webpack-config');
|
6 | const pugStatic = require('pug-static');
|
7 |
|
8 | module.exports = function createServer(config, env) {
|
9 | const webpackConfig = makeWebpackConfig(config, env);
|
10 | const compiler = webpack(webpackConfig);
|
11 | const app = express();
|
12 |
|
13 | if (env === 'development') {
|
14 |
|
15 | app.use(require('webpack-dev-middleware')(compiler, {
|
16 | noInfo: true,
|
17 | stats: webpackConfig.stats || {},
|
18 | }));
|
19 |
|
20 | app.use(require('webpack-hot-middleware')(compiler));
|
21 | }
|
22 |
|
23 |
|
24 | if (config.assetsDir) {
|
25 | app.use(express.static(config.assetsDir));
|
26 | }
|
27 |
|
28 |
|
29 | if (config.viewsDir) {
|
30 | app.use(pugStatic(config.viewsDir));
|
31 | }
|
32 |
|
33 |
|
34 | if (config.configureServer) {
|
35 | config.configureServer(app, env);
|
36 | }
|
37 |
|
38 | return {app, compiler};
|
39 | };
|