Nav apraksta

dev-server.js 2.4KB

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