thelounge/src/plugins/dev-server.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-05-05 00:41:57 +02:00
import webpackDevMiddleware from "webpack-dev-middleware";
2022-05-05 06:10:12 +02:00
import webpackHotMiddleware from "webpack-hot-middleware";
import express from "express";
import log from "../log";
2022-05-22 02:27:51 +02:00
import webpack from "webpack";
import config from "../../webpack.config";
2022-05-05 06:10:12 +02:00
export default (app: express.Application) => {
log.debug("Starting server in development mode");
2022-05-22 02:27:51 +02:00
const webpackConfig = config(undefined, {mode: "production"});
if (
!webpackConfig ||
!webpackConfig.plugins?.length ||
!webpackConfig.entry ||
!webpackConfig.entry["js/bundle.js"]
) {
throw new Error("No valid production webpack config found");
}
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
webpackConfig.entry["js/bundle.js"].push(
"webpack-hot-middleware/client?path=storage/__webpack_hmr"
);
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
const compiler = webpack(webpackConfig);
app.use(
2022-05-05 00:41:57 +02:00
webpackDevMiddleware(compiler, {
index: "/",
publicPath: webpackConfig.output?.publicPath,
})
).use(
2022-05-05 06:10:12 +02:00
// TODO: Fix compiler type
webpackHotMiddleware(compiler as any, {
path: "/storage/__webpack_hmr",
})
);
};