From 227e011d6c0b34a80a90e7607da41023e2a0eb70 Mon Sep 17 00:00:00 2001 From: sua yoo Date: Wed, 8 Jan 2025 10:49:40 -0800 Subject: [PATCH] fix server config --- frontend/scripts/serve.js | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/frontend/scripts/serve.js b/frontend/scripts/serve.js index efc7126b1e..d2cfa331fe 100644 --- a/frontend/scripts/serve.js +++ b/frontend/scripts/serve.js @@ -1,3 +1,4 @@ +// @ts-check // Serve app locally without building with webpack, e.g. for e2e const fs = require("fs"); const path = require("path"); @@ -15,23 +16,36 @@ require("dotenv").config({ path: dotEnvPath, }); -const [devConfig] = require("../webpack.dev.js"); +const devConfigs = require("../webpack.dev.js"); +const [devConfig] = devConfigs; const app = express(); -const { devServer } = devConfig; +/** @type {import('webpack').Configuration['devServer']} */ +const devServer = devConfig.devServer; -devServer.setupMiddlewares([], { app }); +if (!devServer) { + throw new Error("Dev server not defined in `webpack.dev.js`"); +} + +if (devServer.setupMiddlewares) { + // @ts-ignore Express app is compatible with `Server` + devServer.setupMiddlewares([], { app }); +} + +if (Array.isArray(devServer.proxy)) { + devServer.proxy.forEach((proxy) => { + app.use( + // @ts-ignore + proxy.context, + createProxyMiddleware({ + ...proxy, + followRedirects: true, + }), + ); + }); +} -Object.keys(devServer.proxy).forEach((path) => { - app.use( - path, - createProxyMiddleware({ - ...devServer.proxy[path], - followRedirects: true, - }), - ); -}); app.use("/", express.static(distPath)); app.get("/*", (req, res) => { res.sendFile(path.join(distPath, "index.html"));