From 60876fa920045fce48a2b33f436b7a1bb47974f2 Mon Sep 17 00:00:00 2001 From: Vladimir Tikhonov Date: Thu, 18 Sep 2025 18:24:11 +0200 Subject: [PATCH] Fix `npm start` for react examples --- examples/react/package.json | 2 +- examples/react/scripts/start.js | 89 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 examples/react/scripts/start.js diff --git a/examples/react/package.json b/examples/react/package.json index e131f4b..e91c908 100755 --- a/examples/react/package.json +++ b/examples/react/package.json @@ -30,7 +30,7 @@ "scripts": { "verify-installation": "node scripts/verify-install.js && node scripts/copy-nutrient-files.js", "prestart": "npm run verify-installation", - "start": "react-scripts start", + "start": "node scripts/start.js", "prebuild": "npm run verify-installation", "build": "react-scripts build", "test": "react-scripts test", diff --git a/examples/react/scripts/start.js b/examples/react/scripts/start.js new file mode 100644 index 0000000..8286cce --- /dev/null +++ b/examples/react/scripts/start.js @@ -0,0 +1,89 @@ +#!/usr/bin/env node + +function getWebpackDevServerMajorVersion() { + try { + const pkgJson = require("webpack-dev-server/package.json"); + if (!pkgJson || typeof pkgJson.version !== "string") { + return null; + } + const major = Number.parseInt(pkgJson.version.split(".")[0], 10); + return Number.isNaN(major) ? null : major; + } catch (error) { + return null; + } +} + +function patchWebpackDevServerConfig() { + const major = getWebpackDevServerMajorVersion(); + if (major === null || major < 5) { + return; + } + + const configPath = require.resolve( + "react-scripts/config/webpackDevServer.config", + ); + const originalFactory = require(configPath); + + const patchedFactory = (proxy, allowedHost) => { + const originalConfig = originalFactory(proxy, allowedHost); + + const { + https: httpsOption, + onBeforeSetupMiddleware: onBefore, + onAfterSetupMiddleware: onAfter, + setupMiddlewares: originalSetup, + ...restConfig + } = originalConfig; + + const config = { ...restConfig }; + const existingSetup = + typeof originalSetup === "function" ? originalSetup : null; + + if ( + Object.prototype.hasOwnProperty.call(originalConfig, "https") && + httpsOption + ) { + config.server = + typeof httpsOption === "object" + ? { type: "https", options: httpsOption } + : "https"; + } + + if (typeof onBefore === "function" || typeof onAfter === "function") { + config.setupMiddlewares = (middlewares, devServer) => { + let resolvedMiddlewares = middlewares; + + if (existingSetup) { + const result = existingSetup(middlewares, devServer); + if (Array.isArray(result)) { + resolvedMiddlewares = result; + } + } + + if (!devServer) { + return resolvedMiddlewares; + } + + if (typeof onBefore === "function") { + onBefore(devServer); + } + + if (typeof onAfter === "function") { + onAfter(devServer); + } + + return resolvedMiddlewares; + }; + } else if (existingSetup) { + config.setupMiddlewares = existingSetup; + } + + return config; + }; + + require.cache[configPath].exports = patchedFactory; +} + +patchWebpackDevServerConfig(); + +require("react-scripts/scripts/start");