微小悟公众号管理系统

dev-server.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 chalk = require('chalk')
  7. var opn = require('opn')
  8. var path = require('path')
  9. var express = require('express')
  10. var webpack = require('webpack')
  11. var utils = require('./utils')
  12. var proxyMiddleware = require('http-proxy-middleware')
  13. var webpackConfig = process.env.NODE_ENV === 'testing'
  14. ? require('./webpack.prod.conf')
  15. : require('./webpack.dev.conf')
  16. // default port where dev server listens for incoming traffic
  17. var port = process.env.PORT || config.dev.port
  18. // automatically open browser, if not set will be false
  19. var autoOpenBrowser = !!config.dev.autoOpenBrowser
  20. // Define HTTP proxies to your custom API backend
  21. // https://github.com/chimurai/http-proxy-middleware
  22. var proxyTable = config.dev.proxyTable
  23. var app = express()
  24. var compiler = webpack(webpackConfig)
  25. var devMiddleware = require('webpack-dev-middleware')(compiler, {
  26. publicPath: webpackConfig.output.publicPath,
  27. quiet: true
  28. })
  29. var hotMiddleware = require('webpack-hot-middleware')(compiler, {
  30. log: () => {}
  31. })
  32. // force page reload when html-webpack-plugin template changes
  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. // proxy api requests
  40. Object.keys(proxyTable).forEach(function (context) {
  41. var options = proxyTable[context]
  42. if (typeof options === 'string') {
  43. options = { target: options, changeOrigin: true, logLevel: 'debug'}
  44. }
  45. app.use(proxyMiddleware(options.filter || context, options))
  46. })
  47. // handle fallback for HTML5 history API
  48. app.use(require('connect-history-api-fallback')())
  49. // serve webpack bundle output
  50. app.use(devMiddleware)
  51. // enable hot-reload and state-preserving
  52. // compilation error display
  53. app.use(hotMiddleware)
  54. // serve pure static assets
  55. var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
  56. app.use(staticPath, express.static('./static'))
  57. var uri = 'http://localhost:' + port
  58. var _resolve
  59. var readyPromise = new Promise(resolve => {
  60. _resolve = resolve
  61. })
  62. utils.copyright()
  63. utils.clog('blue',`
  64. 开发服务器 启动中...
  65. 访问地址:${uri}
  66. `)
  67. devMiddleware.waitUntilValid(() => {
  68. // when env is testing, don't need open it
  69. if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
  70. opn(uri)
  71. }
  72. _resolve()
  73. })
  74. var server = app.listen(port)
  75. module.exports = {
  76. ready: readyPromise,
  77. close: () => {
  78. server.close()
  79. }
  80. }