dev-server.js 2.5 KB

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