2017-12-24 16:26:17 +00:00
|
|
|
import * as express from 'express'
|
|
|
|
import * as webpack from 'webpack'
|
|
|
|
import * as devMiddleware from 'webpack-dev-middleware'
|
|
|
|
import * as hotMiddleware from 'webpack-hot-middleware'
|
|
|
|
|
|
|
|
import { forceUnwrap as u } from '../app/src/lib/fatal-error'
|
|
|
|
|
|
|
|
import configs = require('../app/webpack.development')
|
|
|
|
|
|
|
|
import { run } from './run'
|
|
|
|
|
2018-05-14 04:31:32 +00:00
|
|
|
function getPortOrDefault() {
|
|
|
|
const port = process.env.PORT
|
|
|
|
if (port != null) {
|
|
|
|
const result = parseInt(port)
|
|
|
|
if (isNaN(result)) {
|
|
|
|
throw new Error(`Unable to parse '${port}' into valid number`)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return 3000
|
|
|
|
}
|
|
|
|
|
2017-12-24 16:26:17 +00:00
|
|
|
function startApp() {
|
|
|
|
const runningApp = run({ stdio: 'inherit' })
|
2018-07-04 18:29:29 +00:00
|
|
|
if (runningApp == null) {
|
2017-12-24 16:26:17 +00:00
|
|
|
console.error(
|
|
|
|
"Couldn't launch the app. You probably need to build it first. Run `yarn build:dev`."
|
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
runningApp.on('close', () => {
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
startApp()
|
|
|
|
} else {
|
|
|
|
const rendererConfig = configs[1]
|
|
|
|
|
|
|
|
const server = express()
|
|
|
|
const compiler = webpack(rendererConfig)
|
2018-05-14 04:31:32 +00:00
|
|
|
const port = getPortOrDefault()
|
2017-12-24 16:26:17 +00:00
|
|
|
|
|
|
|
const message = 'Could not find public path from configuration'
|
|
|
|
server.use(
|
|
|
|
devMiddleware(compiler, {
|
2018-01-01 16:58:16 +00:00
|
|
|
publicPath: u(
|
|
|
|
message,
|
|
|
|
u(message, u(message, rendererConfig).output).publicPath
|
|
|
|
),
|
2018-05-23 05:42:47 +00:00
|
|
|
logLevel: 'error',
|
2017-12-24 16:26:17 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
server.use(hotMiddleware(compiler))
|
|
|
|
|
|
|
|
server.listen(port, 'localhost', (err: Error | null) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Server running at http://localhost:${port}`)
|
|
|
|
startApp()
|
|
|
|
})
|
|
|
|
}
|