dev-server.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. require('./check-versions')()
  2. var config = require('../config')
  3. if (!process.env.NODE_ENV) {
  4. process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
  5. }
  6. var opn = require('opn')
  7. var path = require('path')
  8. var express = require('express')
  9. var webpack = require('webpack')
  10. var proxyMiddleware = require('http-proxy-middleware')
  11. var webpackConfig = require('./webpack.dev.conf')
  12. // default port where dev server listens for incoming traffic
  13. var port = process.env.PORT || config.dev.port
  14. // automatically open browser, if not set will be false
  15. var autoOpenBrowser = !!config.dev.autoOpenBrowser
  16. // Define HTTP proxies to your custom API backend
  17. // https://github.com/chimurai/http-proxy-middleware
  18. var proxyTable = config.dev.proxyTable
  19. var app = express()
  20. var compiler = webpack(webpackConfig)
  21. var devMiddleware = require('webpack-dev-middleware')(compiler, {
  22. publicPath: webpackConfig.output.publicPath,
  23. quiet: true
  24. })
  25. var hotMiddleware = require('webpack-hot-middleware')(compiler, {
  26. log: false,
  27. heartbeat: 2000
  28. })
  29. // force page reload when html-webpack-plugin template changes
  30. compiler.plugin('compilation', function (compilation) {
  31. compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
  32. hotMiddleware.publish({ action: 'reload' })
  33. cb()
  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(options.filter || context, options))
  43. })
  44. // handle fallback for HTML5 history API
  45. app.use(require('connect-history-api-fallback')())
  46. // serve webpack bundle output
  47. app.use(devMiddleware)
  48. // enable hot-reload and state-preserving
  49. // compilation error display
  50. app.use(hotMiddleware)
  51. // serve pure static assets
  52. var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
  53. app.use(staticPath, express.static('./static'))
  54. var uri = 'http://localhost:' + port
  55. var _resolve
  56. var readyPromise = new Promise(resolve => {
  57. _resolve = resolve
  58. })
  59. console.log('> Starting dev server...')
  60. devMiddleware.waitUntilValid(() => {
  61. console.log('> Listening at ' + uri + '\n')
  62. // when env is testing, don't need open it
  63. if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
  64. opn(uri)
  65. }
  66. _resolve()
  67. })
  68. var server = app.listen(port)
  69. module.exports = {
  70. ready: readyPromise,
  71. close: () => {
  72. server.close()
  73. }
  74. }