From 0de70df472725c6fcfafea4b2df852c9c5c872a2 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 20 Nov 2023 13:46:51 +0100 Subject: [PATCH] Move dev server logic out of _startInternal Break out the logic for setting up the webpack dev server into its own method to reduce the complexity of _startInternal. --- packages/controller/src/Controller.ts | 60 ++++++++++++++------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 1bff3415..29249626 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -165,34 +165,7 @@ export default class Controller { // Start webpack development server if enabled if (args.dev || args.devPlugin) { - logger.warn("Webpack development mode enabled"); - /* eslint-disable @typescript-eslint/no-var-requires, node/no-unpublished-require */ - const webpack = require("webpack"); - const webpackDevMiddleware = require("webpack-dev-middleware"); - const webpackConfigs = []; - - if (args.dev) { - // eslint-disable-next-line node/no-missing-require - webpackConfigs.push(require("../../../webpack.config")({})); // Path outside of build - } - if (args.devPlugin) { - let devPlugins = new Map(); - for (let name of args.devPlugin) { - let info = this.pluginInfos.find(i => i.name === name); - if (!info) { - throw new lib.StartupError(`No plugin named ${name}`); - } - let config = require(path.posix.join(info.requirePath, "webpack.config"))({}); - devPlugins.set(name, webpackConfigs.length); - webpackConfigs.push(config); - } - this.app.locals.devPlugins = devPlugins; - } - /* eslint-enable @typescript-eslint/no-var-requires, node/no-unpublished-require */ - - const compiler = webpack(webpackConfigs); - this.devMiddleware = webpackDevMiddleware(compiler, { serverSideRender: true }); - this.app.use(this.devMiddleware); + this._startDevServer(args); } let databaseDirectory = this.config.get("controller.database_directory"); @@ -283,6 +256,37 @@ export default class Controller { this._state = "running"; } + async _startDevServer(args: ControllerArgs) { + logger.warn("Webpack development mode enabled"); + /* eslint-disable @typescript-eslint/no-var-requires, node/no-unpublished-require */ + const webpack = require("webpack"); + const webpackDevMiddleware = require("webpack-dev-middleware"); + const webpackConfigs = []; + + if (args.dev) { + // eslint-disable-next-line node/no-missing-require + webpackConfigs.push(require("../../../webpack.config")({})); // Path outside of build + } + if (args.devPlugin) { + let devPlugins = new Map(); + for (let name of args.devPlugin) { + let info = this.pluginInfos.find(i => i.name === name); + if (!info) { + throw new lib.StartupError(`No plugin named ${name}`); + } + let config = require(path.posix.join(info.requirePath, "webpack.config"))({}); + devPlugins.set(name, webpackConfigs.length); + webpackConfigs.push(config); + } + this.app.locals.devPlugins = devPlugins; + } + /* eslint-enable @typescript-eslint/no-var-requires, node/no-unpublished-require */ + + const compiler = webpack(webpackConfigs); + this.devMiddleware = webpackDevMiddleware(compiler, { serverSideRender: true }); + this.app.use(this.devMiddleware); + } + /** * Stops the controller. *