Skip to content

Commit da12e55

Browse files
committed
adds proxy script
1 parent 45cd275 commit da12e55

File tree

5 files changed

+10395
-43
lines changed

5 files changed

+10395
-43
lines changed

bin/react-scripts-ssr.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
11
#!/usr/bin/env node
2-
var spawn = require('cross-spawn');
2+
var spawn = require("cross-spawn");
33
const args = process.argv.slice(2);
44

5-
const scriptIndex = args.findIndex(
6-
x => x === 'build-server' || x === 'start'
7-
);
5+
const scriptIndex = args.findIndex(x => x === "build-server" || x === "start");
86
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
97
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
108

119
switch (script) {
12-
case 'build-server':
13-
case 'start': {
14-
const result = spawn.sync(
15-
'node',
16-
nodeArgs
17-
.concat(require.resolve('../scripts/' + script))
18-
.concat(args.slice(scriptIndex + 1)),
19-
{ stdio: 'inherit' }
10+
case "proxy":
11+
case "build-server":
12+
case "start": {
13+
const result = spawn.sync(
14+
"node",
15+
nodeArgs
16+
.concat(require.resolve("../scripts/" + script))
17+
.concat(args.slice(scriptIndex + 1)),
18+
{ stdio: "inherit" }
19+
);
20+
if (result.signal) {
21+
if (result.signal === "SIGKILL") {
22+
console.log(
23+
"The build failed because the process exited too early. " +
24+
"This probably means the system ran out of memory or someone called " +
25+
"`kill -9` on the process."
2026
);
21-
if (result.signal) {
22-
if (result.signal === 'SIGKILL') {
23-
console.log(
24-
'The build failed because the process exited too early. ' +
25-
'This probably means the system ran out of memory or someone called ' +
26-
'`kill -9` on the process.'
27-
);
28-
} else if (result.signal === 'SIGTERM') {
29-
console.log(
30-
'The build failed because the process exited too early. ' +
31-
'Someone might have called `kill` or `killall`, or the system could ' +
32-
'be shutting down.'
33-
);
34-
}
35-
process.exit(1);
36-
}
37-
process.exit(result.status);
38-
break;
39-
}
40-
default:
41-
console.log('Unknown script "' + script + '".');
42-
console.log('Perhaps you need to update react-scripts?');
27+
} else if (result.signal === "SIGTERM") {
4328
console.log(
44-
'See: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#updating-to-new-releases'
29+
"The build failed because the process exited too early. " +
30+
"Someone might have called `kill` or `killall`, or the system could " +
31+
"be shutting down."
4532
);
46-
break;
47-
}
33+
}
34+
process.exit(1);
35+
}
36+
process.exit(result.status);
37+
break;
38+
}
39+
default:
40+
console.log('Unknown script "' + script + '".');
41+
console.log("Perhaps you need to update react-scripts?");
42+
console.log(
43+
"See: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#updating-to-new-releases"
44+
);
45+
break;
46+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"babel-loader": "^8.0.4",
1414
"babel-plugin-named-asset-import": "^0.2.3",
15+
"commander": "^2.19.0",
1516
"cross-spawn": "^6.0.5",
1617
"express": "^4.16.4",
1718
"http-proxy-middleware": "^0.19.0",

scripts/proxy.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const httpProxy = require("http-proxy");
2+
const http = require("http");
3+
const fs = require("fs");
4+
const program = require("commander");
5+
const pathCustomConfig = "./.react-scripts-ssr.json";
6+
const openBrowser = require("react-dev-utils/openBrowser");
7+
8+
program
9+
.version("0.1.0")
10+
.option(
11+
"-wh, --web-host [webHost]",
12+
"Add web host where the target website runs. localhost by default"
13+
)
14+
.option(
15+
"-wp, --web-port [webPort]",
16+
"Add web host where the target website runs. 3000 by default"
17+
)
18+
.option(
19+
"-au, --api-url [apiUrl]",
20+
"Add API url where the target API runs. http://localhost:8080 by default"
21+
)
22+
.option(
23+
"-pp, --proxy-port [proxyPort]",
24+
"Add proxy port where the proxy will be listening to. 5050 by default"
25+
)
26+
.option(
27+
"-ph, --proxy-host [proxyHost]",
28+
"Add proxy host where the proxy will be listening to. localhost by default"
29+
)
30+
.parse(process.argv);
31+
32+
let customHttpProxyConfig = { proxy: {} };
33+
try {
34+
if (fs.existsSync(pathCustomConfig)) {
35+
customConfig = JSON.parse(fs.readFileSync(pathCustomConfig));
36+
if (customConfig.proxy) {
37+
customHttpProxyConfig = customConfig;
38+
}
39+
}
40+
} catch (error) {
41+
console.log(
42+
"react-scripts-ssr custom proxy config was not loaded. Are you sure .react-scripts-ssr.json is a JSON with a key called 'proxy'?"
43+
);
44+
}
45+
46+
const WEB_HOST =
47+
customHttpProxyConfig.proxy.webHost || program.webHost || "localhost";
48+
const WEB_PORT =
49+
customHttpProxyConfig.proxy.webPort || program.webPort || "3000";
50+
const WEB_URL = `http://${WEB_HOST}:${WEB_PORT}`;
51+
const API_URL =
52+
customHttpProxyConfig.proxy.apiUrl ||
53+
program.apiUrl ||
54+
"http://localhost:8080";
55+
const PROXY_PORT =
56+
customHttpProxyConfig.proxy.proxyPort || program.proxyPort || 5050;
57+
const PROXY_HOST =
58+
customHttpProxyConfig.proxy.proxyHost || program.proxyHost || "localhost";
59+
60+
const proxy = httpProxy.createProxyServer({
61+
ws: true,
62+
...customHttpProxyConfig.proxy
63+
});
64+
65+
console.log("customHttpProxyConfig", customHttpProxyConfig);
66+
console.log("customHttpProxyConfig", customHttpProxyConfig);
67+
var server = http.createServer((req, res) => {
68+
try {
69+
const host = req.headers.host.split(":")[0];
70+
if (host.startsWith("api") || req.url.startsWith("/api")) {
71+
proxy.web(req, res, {
72+
target: API_URL
73+
});
74+
} else {
75+
proxy.web(req, res, {
76+
target: WEB_URL,
77+
ws: true
78+
});
79+
}
80+
} catch (error) {
81+
console.log("there was an error in the proxy", error);
82+
}
83+
});
84+
85+
proxy.on("error", (err, req, res) => {});
86+
87+
server.on("upgrade", (req, socket, head) => {
88+
proxy.ws(req, socket, head);
89+
});
90+
91+
server.listen(PROXY_PORT, PROXY_HOST, function() {
92+
const proxyUrl = `http://${PROXY_HOST}:${PROXY_PORT}`;
93+
console.log(
94+
`Proxy listening on ${proxyUrl} to web ${WEB_URL} ${
95+
API_URL ? ` and API ${API_URL}` : ""
96+
}`
97+
);
98+
openBrowser(proxyUrl);
99+
});

scripts/start.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
2-
3-
// ln -s /Users/alexlobera/Projects/leanjscom/internal/react-design-system/react-scripts-ssr/bin/index.js /Users/alexlobera/Projects/leanjscom/internal/react-design-system/node_modules/.bin/react-scripts-ssr
2+
const program = require("commander");
43

54
// Do this as the first thing so that any code reading it knows the right env.
65
process.env.BABEL_ENV = "development";
@@ -22,7 +21,6 @@ const fs = require("fs");
2221
const chalk = require("chalk");
2322
const webpack = require("webpack");
2423
const WebpackDevServer = require("webpack-dev-server");
25-
const clearConsole = require("react-dev-utils/clearConsole");
2624
const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles");
2725
const {
2826
choosePort,
@@ -37,7 +35,6 @@ const config = require(`${reactScriptsPath}/config/webpack.config.dev`);
3735
const createDevServerConfig = require(`${reactScriptsPath}/config/webpackDevServer.config`);
3836

3937
const useYarn = fs.existsSync(paths.yarnLockFile);
40-
const isInteractive = process.stdout.isTTY;
4138

4239
// Warn and crash if required files are missing
4340
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
@@ -94,13 +91,26 @@ choosePort(HOST, DEFAULT_PORT)
9491

9592
process.env.REACT_APP_DEV_SERVER_PORT = port;
9693

97-
const configServer = require(`../config/webpack.config.server`);
98-
const compiler = webpack(configServer);
94+
program
95+
.version("0.1.0")
96+
.option("-enpx, --enable-proxy", "Enables proxy")
97+
.option("-dssr, --disable-ssr", "Disable SSR")
98+
.parse(process.argv);
99+
99100
const devUrls = prepareUrls(protocol, HOST, port);
100101
let isServerRunning;
101102

102103
openBrowser(devUrls.localUrlForBrowser);
103104

105+
if (program.enableProxy) {
106+
require("./proxy.js");
107+
}
108+
if (program.disableSsr) {
109+
return;
110+
}
111+
const configServer = require(`../config/webpack.config.server`);
112+
const compiler = webpack(configServer);
113+
104114
compiler.watch(
105115
{
106116
aggregateTimeout: 300

0 commit comments

Comments
 (0)