dev-server.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict'
  2. require('./check-versions')()
  3. const config = require('../config')
  4. if (!process.env.NODE_ENV) {
  5. process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
  6. }
  7. const opn = require('opn')
  8. const path = require('path')
  9. const express = require('express')
  10. const webpack = require('webpack')
  11. const proxyMiddleware = require('http-proxy-middleware')
  12. const webpackConfig = process.env.NODE_ENV === 'testing'
  13. ? require('./webpack.prod.conf')
  14. : require('./webpack.dev.conf')
  15. // default port where dev server listens for incoming traffic
  16. const port = process.env.PORT || config.dev.port
  17. // automatically open browser, if not set will be false
  18. const autoOpenBrowser = !!config.dev.autoOpenBrowser
  19. // Define HTTP proxies to your custom API backend
  20. // https://github.com/chimurai/http-proxy-middleware
  21. const proxyTable = config.dev.proxyTable
  22. const app = express()
  23. const compiler = webpack(webpackConfig)
  24. const devMiddleware = require('webpack-dev-middleware')(compiler, {
  25. publicPath: webpackConfig.output.publicPath,
  26. quiet: true
  27. })
  28. const hotMiddleware = require('webpack-hot-middleware')(compiler, {
  29. log: false
  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. // enable hot-reload and state-preserving
  39. // compilation error display
  40. app.use(hotMiddleware)
  41. // proxy api requests
  42. Object.keys(proxyTable).forEach(function (context) {
  43. let options = proxyTable[context]
  44. if (typeof options === 'string') {
  45. options = { target: options }
  46. }
  47. app.use(proxyMiddleware(options.filter || context, options))
  48. })
  49. // handle fallback for HTML5 history API
  50. app.use(require('connect-history-api-fallback')())
  51. // serve webpack bundle output
  52. app.use(devMiddleware)
  53. // serve pure static assets
  54. const staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
  55. app.use(staticPath, express.static('./static'))
  56. const uri = 'http://localhost:' + port
  57. let _resolve
  58. const 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. const server = app.listen(port)
  71. module.exports = {
  72. ready: readyPromise,
  73. close: () => {
  74. server.close()
  75. }
  76. }