const express = require('express')
const webpack = require('webpack')
const getPort = require('./utils/port')
const log = require('../utils/log')
const getHostIp = require('../utils/ip')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const app = express()
export default function server({
initPort = 9000,
devConfig
}) {
const compiler = webpack(devConfig)
app.use(webpackDevMiddleware(compiler, {
publicPath: devConfig.output.publicPath || './dist'
}))
app.use(webpackHotMiddleware(compiler));
getPort(initPort).then(port => {
app.listen(port, function (err) {
if (err) log.fail('dev server start failed', err)
log.success(`
App running at:
===========================
- Local: http://localhost:${port}
- Host: http://${getHostIp()}:${port}
===========================`)
})
})
}