|
| 1 | +var path = require('path') |
| 2 | +var express = require('express') |
| 3 | +var webpack = require('webpack') |
| 4 | +var config = require('../config') |
| 5 | +var proxyMiddleware = require('http-proxy-middleware') |
| 6 | +var webpackConfig = process.env.NODE_ENV === 'testing' |
| 7 | + ? require('./webpack.prod.conf') |
| 8 | + : require('./webpack.dev.conf') |
| 9 | + |
| 10 | +// default port where dev server listens for incoming traffic |
| 11 | +var port = process.env.PORT || config.dev.port |
| 12 | +// Define HTTP proxies to your custom API backend |
| 13 | +// https://github.com/chimurai/http-proxy-middleware |
| 14 | +var proxyTable = config.dev.proxyTable |
| 15 | + |
| 16 | +var app = express() |
| 17 | +var compiler = webpack(webpackConfig) |
| 18 | + |
| 19 | +var devMiddleware = require('webpack-dev-middleware')(compiler, { |
| 20 | + publicPath: webpackConfig.output.publicPath, |
| 21 | + stats: { |
| 22 | + colors: true, |
| 23 | + chunks: false |
| 24 | + } |
| 25 | +}) |
| 26 | + |
| 27 | +var hotMiddleware = require('webpack-hot-middleware')(compiler) |
| 28 | +// force page reload when html-webpack-plugin template changes |
| 29 | +compiler.plugin('compilation', function (compilation) { |
| 30 | + compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) { |
| 31 | + hotMiddleware.publish({ action: 'reload' }) |
| 32 | + cb() |
| 33 | + }) |
| 34 | +}) |
| 35 | + |
| 36 | +// proxy api requests |
| 37 | +Object.keys(proxyTable).forEach(function (context) { |
| 38 | + var options = proxyTable[context] |
| 39 | + if (typeof options === 'string') { |
| 40 | + options = { target: options } |
| 41 | + } |
| 42 | + app.use(proxyMiddleware(context, options)) |
| 43 | +}) |
| 44 | + |
| 45 | +// handle fallback for HTML5 history API |
| 46 | +app.use(require('connect-history-api-fallback')()) |
| 47 | + |
| 48 | +// serve webpack bundle output |
| 49 | +app.use(devMiddleware) |
| 50 | + |
| 51 | +// enable hot-reload and state-preserving |
| 52 | +// compilation error display |
| 53 | +app.use(hotMiddleware) |
| 54 | + |
| 55 | +// serve pure static assets |
| 56 | +var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory) |
| 57 | +app.use(staticPath, express.static('./static')) |
| 58 | + |
| 59 | +module.exports = app.listen(port, function (err) { |
| 60 | + if (err) { |
| 61 | + console.log(err) |
| 62 | + return |
| 63 | + } |
| 64 | + console.log('Listening at http://localhost:' + port + '\n') |
| 65 | +}) |
0 commit comments