UNPKG

1.31 kBJavaScriptView Raw
1'use strict';
2
3const express = require('express');
4const webpack = require('webpack');
5const makeWebpackConfig = require('./make-webpack-config');
6const pugStatic = require('pug-static');
7
8module.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 // register webpack middlewares
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 // configure static assets
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 // configure static template files, use template engine pug
36 if (config.viewsDir) {
37 app.use(pugStatic(config.viewsDir));
38 }
39
40 // user defined customizations
41 if (config.configureServer) {
42 config.configureServer(app, env);
43 }
44
45 return {app, compiler};
46};