|
| 1 | +'use strict' |
| 2 | +require('./check-versions')() |
| 3 | + |
| 4 | +const config = require('../config') |
| 5 | +if (!process.env.NODE_ENV) { |
| 6 | + process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV) |
| 7 | +} |
| 8 | + |
| 9 | +const opn = require('opn') |
| 10 | +const path = require('path') |
| 11 | +const express = require('express') |
| 12 | +const webpack = require('webpack') |
| 13 | +const proxyMiddleware = require('http-proxy-middleware') |
| 14 | +const webpackConfig = require('./webpack.dev.conf') |
| 15 | + |
| 16 | +// default port where dev server listens for incoming traffic |
| 17 | +const port = process.env.PORT || config.dev.port |
| 18 | +// automatically open browser, if not set will be false |
| 19 | +const autoOpenBrowser = !!config.dev.autoOpenBrowser |
| 20 | +// Define HTTP proxies to your custom API backend |
| 21 | +// https://github.com/chimurai/http-proxy-middleware |
| 22 | +const proxyTable = config.dev.proxyTable |
| 23 | + |
| 24 | +const app = express() |
| 25 | +const compiler = webpack(webpackConfig) |
| 26 | + |
| 27 | +const devMiddleware = require('webpack-dev-middleware')(compiler, { |
| 28 | + publicPath: webpackConfig.output.publicPath, |
| 29 | + quiet: true |
| 30 | +}) |
| 31 | + |
| 32 | +const hotMiddleware = require('webpack-hot-middleware')(compiler, { |
| 33 | + log: false, |
| 34 | + heartbeat: 2000 |
| 35 | +}) |
| 36 | +// force page reload when html-webpack-plugin template changes |
| 37 | +// currently disabled until this is resolved: |
| 38 | +// https://github.com/jantimon/html-webpack-plugin/issues/680 |
| 39 | +// compiler.plugin('compilation', function (compilation) { |
| 40 | +// compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) { |
| 41 | +// hotMiddleware.publish({ action: 'reload' }) |
| 42 | +// cb() |
| 43 | +// }) |
| 44 | +// }) |
| 45 | + |
| 46 | +// enable hot-reload and state-preserving |
| 47 | +// compilation error display |
| 48 | +app.use(hotMiddleware) |
| 49 | + |
| 50 | +// proxy api requests |
| 51 | +Object.keys(proxyTable).forEach(function (context) { |
| 52 | + let options = proxyTable[context] |
| 53 | + if (typeof options === 'string') { |
| 54 | + options = { target: options } |
| 55 | + } |
| 56 | + app.use(proxyMiddleware(options.filter || context, options)) |
| 57 | +}) |
| 58 | + |
| 59 | +// handle fallback for HTML5 history API |
| 60 | +app.use(require('connect-history-api-fallback')()) |
| 61 | + |
| 62 | +// serve webpack bundle output |
| 63 | +app.use(devMiddleware) |
| 64 | + |
| 65 | +// serve pure static assets |
| 66 | +const staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory) |
| 67 | +app.use(staticPath, express.static('./static')) |
| 68 | + |
| 69 | +const uri = 'http://localhost:' + port |
| 70 | + |
| 71 | +var _resolve |
| 72 | +var _reject |
| 73 | +var readyPromise = new Promise((resolve, reject) => { |
| 74 | + _resolve = resolve |
| 75 | + _reject = reject |
| 76 | +}) |
| 77 | + |
| 78 | +var server |
| 79 | +var portfinder = require('portfinder') |
| 80 | +portfinder.basePort = port |
| 81 | + |
| 82 | +console.log('> Starting dev server...') |
| 83 | +devMiddleware.waitUntilValid(() => { |
| 84 | + portfinder.getPort((err, port) => { |
| 85 | + if (err) { |
| 86 | + _reject(err) |
| 87 | + } |
| 88 | + process.env.PORT = port |
| 89 | + var uri = 'http://localhost:' + port |
| 90 | + console.log('> Listening at ' + uri + '\n') |
| 91 | + // when env is testing, don't need open it |
| 92 | + if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') { |
| 93 | + opn(uri) |
| 94 | + } |
| 95 | + server = app.listen(port) |
| 96 | + _resolve() |
| 97 | + }) |
| 98 | +}) |
| 99 | + |
| 100 | +module.exports = { |
| 101 | + ready: readyPromise, |
| 102 | + close: () => { |
| 103 | + server.close() |
| 104 | + } |
| 105 | +} |
0 commit comments