dev-server.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. var utils = require('./utils')
  15. // default port where dev server listens for incoming traffic
  16. var port = process.env.PORT || config.dev.port
  17. // automatically open browser, if not set will be false
  18. var autoOpenBrowser = !!config.dev.autoOpenBrowser
  19. // Define HTTP proxies to your custom API backend
  20. // https://github.com/chimurai/http-proxy-middleware
  21. var proxyTable = config.dev.proxyTable
  22. var app = express()
  23. var compiler = webpack(webpackConfig)
  24. if (process.env.PLATFORM === 'swan') {
  25. utils.writeFrameworkinfo()
  26. }
  27. // var devMiddleware = require('webpack-dev-middleware')(compiler, {
  28. // publicPath: webpackConfig.output.publicPath,
  29. // quiet: true
  30. // })
  31. // var hotMiddleware = require('webpack-hot-middleware')(compiler, {
  32. // log: false,
  33. // heartbeat: 2000
  34. // })
  35. // force page reload when html-webpack-plugin template changes
  36. // compiler.plugin('compilation', function (compilation) {
  37. // compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
  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(options.filter || 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. // var uri = 'http://localhost:' + port
  61. var _resolve
  62. var readyPromise = new Promise(resolve => {
  63. _resolve = resolve
  64. })
  65. // console.log('> Starting dev server...')
  66. // devMiddleware.waitUntilValid(() => {
  67. // console.log('> Listening at ' + uri + '\n')
  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. module.exports = new Promise((resolve, reject) => {
  75. portfinder.basePort = port
  76. portfinder.getPortPromise()
  77. .then(newPort => {
  78. if (port !== newPort) {
  79. console.log(`${port}端口被占用,开启新端口${newPort}`)
  80. }
  81. var server = app.listen(newPort, 'localhost')
  82. // for 小程序的文件保存机制
  83. require('webpack-dev-middleware-hard-disk')(compiler, {
  84. publicPath: webpackConfig.output.publicPath,
  85. quiet: true
  86. })
  87. resolve({
  88. ready: readyPromise,
  89. close: () => {
  90. server.close()
  91. }
  92. })
  93. }).catch(error => {
  94. console.log('没有找到空闲端口,请打开任务管理器杀死进程端口再试', error)
  95. })
  96. })