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 | if (typeof config.assetsDir === 'string') {
|
26 | app.use(express.static(config.assetsDir));
|
27 | }
|
28 | if (Array.isArray(config.assetsDir)) {
|
29 | config.assetsDir.forEach(function (path) {
|
30 | app.use(express.static(path));
|
31 | });
|
32 | }
|
33 | }
|
34 |
|
35 |
|
36 | if (config.viewsDir) {
|
37 | app.use(pugStatic(config.viewsDir));
|
38 | }
|
39 |
|
40 |
|
41 | if (config.configureServer) {
|
42 | config.configureServer(app, env);
|
43 | }
|
44 |
|
45 | return {app, compiler};
|
46 | };
|