Skip to content

Commit 4ee146f

Browse files
Fix npm start for react examples
1 parent 4a2873a commit 4ee146f

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

examples/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"scripts": {
3131
"verify-installation": "node scripts/verify-install.js && node scripts/copy-nutrient-files.js",
3232
"prestart": "npm run verify-installation",
33-
"start": "react-scripts start",
33+
"start": "node scripts/start.js",
3434
"prebuild": "npm run verify-installation",
3535
"build": "react-scripts build",
3636
"test": "react-scripts test",

examples/react/scripts/start.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env node
2+
3+
function getWebpackDevServerMajorVersion() {
4+
try {
5+
const pkgJson = require("webpack-dev-server/package.json");
6+
if (!pkgJson || typeof pkgJson.version !== "string") {
7+
return null;
8+
}
9+
const major = parseInt(pkgJson.version.split(".")[0], 10);
10+
return Number.isNaN(major) ? null : major;
11+
} catch (error) {
12+
return null;
13+
}
14+
}
15+
16+
function patchWebpackDevServerConfig() {
17+
const major = getWebpackDevServerMajorVersion();
18+
if (major === null || major < 5) {
19+
return;
20+
}
21+
22+
const configPath = require.resolve(
23+
"react-scripts/config/webpackDevServer.config"
24+
);
25+
const originalFactory = require(configPath);
26+
27+
const patchedFactory = (proxy, allowedHost) => {
28+
const config = originalFactory(proxy, allowedHost);
29+
30+
if (Object.prototype.hasOwnProperty.call(config, "https")) {
31+
const httpsOption = config.https;
32+
if (httpsOption) {
33+
if (typeof httpsOption === "object") {
34+
config.server = { type: "https", options: httpsOption };
35+
} else {
36+
config.server = "https";
37+
}
38+
}
39+
delete config.https;
40+
}
41+
42+
const onBefore = config.onBeforeSetupMiddleware;
43+
const onAfter = config.onAfterSetupMiddleware;
44+
const existingSetup =
45+
typeof config.setupMiddlewares === "function"
46+
? config.setupMiddlewares
47+
: null;
48+
49+
if (typeof onBefore !== "function" && typeof onAfter !== "function") {
50+
return config;
51+
}
52+
53+
delete config.onBeforeSetupMiddleware;
54+
delete config.onAfterSetupMiddleware;
55+
56+
config.setupMiddlewares = (middlewares, devServer) => {
57+
let resolvedMiddlewares = middlewares;
58+
59+
if (typeof existingSetup === "function") {
60+
const result = existingSetup(middlewares, devServer);
61+
if (Array.isArray(result)) {
62+
resolvedMiddlewares = result;
63+
}
64+
}
65+
66+
if (!devServer) {
67+
return resolvedMiddlewares;
68+
}
69+
70+
if (typeof onBefore === "function") {
71+
onBefore(devServer);
72+
}
73+
74+
if (typeof onAfter === "function") {
75+
onAfter(devServer);
76+
}
77+
78+
return resolvedMiddlewares;
79+
};
80+
81+
return config;
82+
};
83+
84+
require.cache[configPath].exports = patchedFactory;
85+
}
86+
87+
patchWebpackDevServerConfig();
88+
89+
require("react-scripts/scripts/start");

0 commit comments

Comments
 (0)