dev-server.js 2.8 KB

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