代码挖掘后,typescript create-react-app 似乎尚未包含自定义代理功能。我不得不更新两个文件:
https://github.com/samuelstevens9/create-react-app/blob/next/packages/react-scripts/config/paths.js
在每个 module.exports 中添加了proxySetup: resolveApp('src/setupProxy.js'),,最后一个(第三个)是proxySetup: resolveOwn('template/src/setupProxy.js'),
https://github.com/samuelstevens9/create-react-app/blob/next/packages/react-scripts/config/webpackDevServer.config.js
在第 15 行下方添加了const fs = require('fs'); const paths = require('./paths'); 并添加了
if (fs.existsSync(paths.proxySetup)) {
// This registers user provided middleware for proxy reasons
require(paths.proxySetup)(app);
}
在文件末尾的before(app) { ... } 函数内。
我正在创建一个对主存储库的拉取请求,但看起来 v3.1.0 文件与 next 分支上的最新文件不同。现在我使用我制作的补丁脚本,因为我们使用的是更新所有必要软件包的 lerna monorepo:
#!/bin/bash
CONFIG_PATHS_URL="https://raw.githubusercontent.com/samuelstevens9/create-react-app/next/packages/react-scripts/config/paths.js"
CONFIG_WEBPACKDEVSERVER_URL="https://raw.githubusercontent.com/samuelstevens9/create-react-app/next/packages/react-scripts/config/webpackDevServer.config.js"
SETUPPROXY_URL="https://gist.githubusercontent.com/samuelstevens9/5872e72ac915dfc1a8ae2fdcef323899/raw/7f2c76d42bc0915026379dfc7884cb1bd97f56bb/setupProxy.js"
for f in packages/*; do
if [ -d ${f} ]; then
echo $f
# Will not run if no directories are available
NODE_MODULES_CONFIG_DIR=$f/node_modules/react-scripts-ts/config
if [ -d "$NODE_MODULES_CONFIG_DIR" ]; then
# Control will enter here if $DIRECTORY exists.
echo $NODE_MODULES_CONFIG_DIR
curl -o $NODE_MODULES_CONFIG_DIR/paths.js $CONFIG_PATHS_URL
curl -o $NODE_MODULES_CONFIG_DIR/webpackDevServer.config.js $CONFIG_WEBPACKDEVSERVER_URL
curl -o $f/src/setupProxy.js $SETUPPROXY_URL
fi
fi
done
并更新每个包中的setupProxy.js 文件。希望这会有所帮助。