dev-server.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. require('./check-versions')()
  2. var config = require('../config')
  3. if (!process.env.NODE_ENV) process.env.NODE_ENV = config.dev.env
  4. var path = require('path')
  5. var express = require('express')
  6. var webpack = require('webpack')
  7. var opn = require('opn')
  8. var proxyMiddleware = require('http-proxy-middleware')
  9. var webpackConfig = process.env.NODE_ENV === 'testing'
  10. ? require('./webpack.prod.conf')
  11. : require('./webpack.dev.conf')
  12. var updateEmoji = require('./update-emoji').updateEmoji
  13. updateEmoji()
  14. // default port where dev server listens for incoming traffic
  15. var port = process.env.PORT || config.dev.port
  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. writeToDisk: true,
  24. stats: {
  25. colors: true,
  26. chunks: false
  27. }
  28. })
  29. var hotMiddleware = require('webpack-hot-middleware')(compiler)
  30. // FIXME: The statement below gives error about hooks being required in webpack 5.
  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. // // FIXME: This supposed to reload whole page when index.html is changed,
  35. // // however now it reloads entire page on every breath, i suppose the order
  36. // // of plugins changed or something. It's a minor thing and douesn't hurt
  37. // // disabling it, constant reloads hurt much more
  38. // // hotMiddleware.publish({ action: 'reload' })
  39. // // cb()
  40. // })
  41. // })
  42. // proxy api requests
  43. Object.keys(proxyTable).forEach(function (context) {
  44. var options = proxyTable[context]
  45. if (typeof options === 'string') {
  46. options = { target: options }
  47. }
  48. app.use(proxyMiddleware.createProxyMiddleware(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. // enable hot-reload and state-preserving
  55. // compilation error display
  56. app.use(hotMiddleware)
  57. // serve pure static assets
  58. var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
  59. app.use(staticPath, express.static('./static'))
  60. module.exports = app.listen(port, function (err) {
  61. if (err) {
  62. console.log(err)
  63. return
  64. }
  65. var uri = 'http://localhost:' + port
  66. console.log('Listening at ' + uri + '\n')
  67. // opn(uri)
  68. })