Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
89 changes: 89 additions & 0 deletions examples/react/scripts/start.js
Original file line number Diff line number Diff line change
@@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vladimir-tikhonov-nutrient we use patch-package in the monorepo to solve similar issues; do you think we could use it here as well?

Loading