Created
February 11, 2022 16:11
-
-
Save robinelvin/e47d79d4fbf1aaf67bd19bba4260d455 to your computer and use it in GitHub Desktop.
Express app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @typescript-eslint/no-var-requires */ | |
import express, { Express } from 'express'; | |
import { IUserAwareRequest } from './middleware/AuthMiddleware'; | |
import { requestHandler } from './requesthandler'; | |
import 'core-js/stable'; | |
import 'regenerator-runtime/runtime'; | |
import { Configuration } from 'webpack'; | |
import webpack from 'webpack'; | |
import webpackDevMiddleware from 'webpack-dev-middleware'; | |
import webpackHotMiddleware from 'webpack-hot-middleware'; | |
const path = require('path'); | |
import Config from 'getconfig'; | |
const ONE_SECOND_MS = 1000; | |
const TEN = 10; | |
const TEN_SECONDS_MS = TEN * ONE_SECOND_MS; | |
let assets: Set<string>; | |
export const getStatsFromCompilation = () => | |
new Promise<Set<string>>((resolve) => { | |
resolve(assets); | |
}); | |
export const buildApp = (app: Express) => { | |
const appEnv = app.get('env'); | |
const runserver = process.argv.some((arg) => arg === '--env.runserver'); | |
const env = { | |
development: appEnv === 'development', | |
production: appEnv === 'production', | |
test: appEnv === 'test', | |
runserver, | |
}; | |
console.log(`Express running in ${appEnv} mode`); | |
// Set up development-only things | |
if (env.development) { | |
const webpackConfig: ( | |
e: typeof env | |
) => Configuration = require('../../ui.webpack.config'); | |
const configuration: Configuration = webpackConfig(env); | |
const compiler = webpack(configuration); | |
// Populate the stats for getting the bundle name | |
compiler.hooks.afterDone.tap('Done', (stats) => { | |
// console.log(stats.compilation.emittedAssets); | |
console.log( | |
'Compilation done', | |
stats.compilation.emittedAssets.size | |
); | |
if (!assets) { | |
assets = stats.compilation.emittedAssets; | |
} | |
}); | |
// webpack hmr | |
app.use( | |
webpackDevMiddleware(compiler, { | |
serverSideRender: true, | |
publicPath: configuration.output.publicPath, | |
// writeToDisk: false, | |
headers: { | |
'Set-Cookie': `config=${JSON.stringify(Config.client)};`, | |
}, | |
stats: configuration.stats, | |
}) | |
); | |
app.use( | |
webpackHotMiddleware(compiler, { | |
// tslint:disable-next-line:no-console | |
log: console.log, | |
heartbeat: TEN_SECONDS_MS, | |
publicPath: configuration.output.publicPath, | |
path: '/hmr/ui/', | |
}) | |
); | |
console.log( | |
`Serving static files from ${path.resolve('dist', 'static')}` | |
); | |
app.use('/static', express.static(path.resolve('dist', 'static'))); | |
} | |
app.get('*', (req: IUserAwareRequest, res, next) => | |
requestHandler(req, res, next, env.production === true) | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment