dev-server.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. require('./check-versions')()
  2. var config = require('../config')
  3. Object.entries(config.dev.env).forEach(item => {
  4. if (!process.env[item[0]]) {
  5. process.env[item[0]] = JSON.parse(item[1])
  6. }
  7. })
  8. var opn = require('opn')
  9. var path = require('path')
  10. var express = require('express')
  11. var webpack = require('webpack')
  12. var proxyMiddleware = require('http-proxy-middleware')
  13. var webpackConfig = require('./webpack.dev.conf')
  14. // default port where dev server listens for incoming traffic
  15. var port = process.env.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. })
  30. // force page reload when html-webpack-plugin template changes
  31. compiler.plugin('compilation', function(compilation) {
  32. compilation.plugin('html-webpack-plugin-after-emit', function(data, cb) {
  33. hotMiddleware.publish({ action: 'reload' })
  34. cb()
  35. })
  36. })
  37. // proxy api requests
  38. Object.keys(proxyTable).forEach(function(context) {
  39. var options = proxyTable[context]
  40. if (typeof options === 'string') {
  41. options = { target: options }
  42. }
  43. app.use(proxyMiddleware(options.filter || context, options))
  44. })
  45. // handle fallback for HTML5 history API
  46. app.use(require('connect-history-api-fallback')())
  47. // serve webpack bundle output
  48. app.use(devMiddleware)
  49. // enable hot-reload and state-preserving
  50. // compilation error display
  51. app.use(hotMiddleware)
  52. // serve pure static assets
  53. var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
  54. app.use(staticPath, express.static('./static'))
  55. var uri = `http://${process.env.HOST}:${port}`
  56. var _resolve
  57. var readyPromise = new Promise(resolve => {
  58. _resolve = resolve
  59. })
  60. console.log('> Starting dev server...')
  61. devMiddleware.waitUntilValid(() => {
  62. console.log(`> Listening at ${uri}\n`)
  63. // when env is testing, don't need open it
  64. if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
  65. opn(uri)
  66. }
  67. _resolve()
  68. })
  69. var server = app.listen(port)
  70. module.exports = {
  71. ready: readyPromise,
  72. close: () => {
  73. server.close()
  74. }
  75. }