dev-server.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 portfinder = require('portfinder')
  12. var webpackConfig = require('./webpack.dev.conf')
  13. // default port where dev server listens for incoming traffic
  14. var port = process.env.PORT || config.dev.port
  15. // automatically open browser, if not set will be false
  16. var autoOpenBrowser = !!config.dev.autoOpenBrowser
  17. // Define HTTP proxies to your custom API backend
  18. // https://github.com/chimurai/http-proxy-middleware
  19. var proxyTable = config.dev.proxyTable
  20. var app = express()
  21. var compiler = webpack(webpackConfig)
  22. // var devMiddleware = require('webpack-dev-middleware')(compiler, {
  23. // publicPath: webpackConfig.output.publicPath,
  24. // quiet: true
  25. // })
  26. // var hotMiddleware = require('webpack-hot-middleware')(compiler, {
  27. // log: false,
  28. // heartbeat: 2000
  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. module.exports = new Promise((resolve, reject) => {
  70. portfinder.basePort = port
  71. portfinder.getPortPromise()
  72. .then(newPort => {
  73. if (port !== newPort) {
  74. console.log(`${port}端口被占用,开启新端口${newPort}`)
  75. }
  76. var server = app.listen(newPort, 'localhost')
  77. // for 小程序的文件保存机制
  78. require('webpack-dev-middleware-hard-disk')(compiler, {
  79. publicPath: webpackConfig.output.publicPath,
  80. quiet: true
  81. })
  82. resolve({
  83. ready: readyPromise,
  84. close: () => {
  85. server.close()
  86. }
  87. })
  88. }).catch(error => {
  89. console.log('没有找到空闲端口,请打开任务管理器杀死进程端口再试', error)
  90. })
  91. })