您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

92 行
2.5 KiB

  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 = process.env.NODE_ENV === 'testing'
  12. ? require('./webpack.prod.conf')
  13. : require('./webpack.dev.conf')
  14. // default port where dev server listens for incoming traffic
  15. var port = process.env.PORT || config.dev.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://localhost:' + 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. }