dev-server.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. require('./check-versions')()
  2. process.env.PLATFORM = process.argv[process.argv.length - 1] || 'wx'
  3. var config = require('../config')
  4. if (!process.env.NODE_ENV) {
  5. process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
  6. }
  7. // var opn = require('opn')
  8. var path = require('path')
  9. var express = require('express')
  10. var webpack = require('webpack')
  11. var proxyMiddleware = require('http-proxy-middleware')
  12. var portfinder = require('portfinder')
  13. var webpackConfig = 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: false,
  29. // heartbeat: 2000
  30. // })
  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. // hotMiddleware.publish({ action: 'reload' })
  35. // cb()
  36. // })
  37. // })
  38. // proxy api requests
  39. Object.keys(proxyTable).forEach(function (context) {
  40. var options = proxyTable[context]
  41. if (typeof options === 'string') {
  42. options = { target: options }
  43. }
  44. app.use(proxyMiddleware(options.filter || context, options))
  45. })
  46. // handle fallback for HTML5 history API
  47. app.use(require('connect-history-api-fallback')())
  48. // serve webpack bundle output
  49. // app.use(devMiddleware)
  50. // enable hot-reload and state-preserving
  51. // compilation error display
  52. // app.use(hotMiddleware)
  53. // serve pure static assets
  54. var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
  55. app.use(staticPath, express.static('./static'))
  56. // var uri = 'http://localhost:' + port
  57. var _resolve
  58. var readyPromise = new Promise(resolve => {
  59. _resolve = resolve
  60. })
  61. // console.log('> Starting dev server...')
  62. // devMiddleware.waitUntilValid(() => {
  63. // console.log('> Listening at ' + uri + '\n')
  64. // // when env is testing, don't need open it
  65. // if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
  66. // opn(uri)
  67. // }
  68. // _resolve()
  69. // })
  70. module.exports = new Promise((resolve, reject) => {
  71. portfinder.basePort = port
  72. portfinder.getPortPromise()
  73. .then(newPort => {
  74. if (port !== newPort) {
  75. console.log(`${port}端口被占用,开启新端口${newPort}`)
  76. }
  77. var server = app.listen(newPort, 'localhost')
  78. // for 小程序的文件保存机制
  79. require('webpack-dev-middleware-hard-disk')(compiler, {
  80. publicPath: webpackConfig.output.publicPath,
  81. quiet: true
  82. })
  83. resolve({
  84. ready: readyPromise,
  85. close: () => {
  86. server.close()
  87. }
  88. })
  89. }).catch(error => {
  90. console.log('没有找到空闲端口,请打开任务管理器杀死进程端口再试', error)
  91. })
  92. })