Skip to content

Commit

Permalink
Replace advanced proxy with user provided proxy.js (facebook#3366)
Browse files Browse the repository at this point in the history
  • Loading branch information
iansu committed Jan 17, 2018
1 parent 11f1aa0 commit 4412385
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 50 deletions.
75 changes: 26 additions & 49 deletions packages/react-dev-utils/WebpackDevServerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,36 +263,47 @@ function onProxyError(proxy) {
};
}

function prepareProxy(proxy, appPublicFolder) {
// `proxy` lets you specify alternate servers for specific requests.
// It can either be a string or an object conforming to the Webpack dev server proxy configuration
// https://webpack.github.io/docs/webpack-dev-server.html
if (!proxy) {
return undefined;
}
if (typeof proxy !== 'object' && typeof proxy !== 'string') {
function prepareProxy(proxy, paths) {
// `proxy` lets you specify an alternate server for non-asset requests.
if (typeof proxy !== 'string' && proxy !== undefined) {
console.log(
chalk.red(
'When specified, "proxy" in package.json must be a string or an object.'
)
chalk.red('When specified, "proxy" in package.json must be a string.')
);
console.log(
chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".')
);
if (typeof proxy === 'object') {
console.log(
chalk.red('Proxy object is no longer supported. Use proxy.js instead.')
);
}
console.log(
chalk.red(
'Either remove "proxy" from package.json, or make it an object.'
)
chalk.red('Either remove "proxy" from package.json, or make it a string.')
);
process.exit(1);
}

// Otherwise, if proxy is specified, we will let it handle any request except for files in the public folder.
function mayProxy(pathname) {
const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1));
const maybePublicPath = path.resolve(paths.appPublic, pathname.slice(1));
return !fs.existsSync(maybePublicPath);
}

function proxyFileExists() {
return fs.existsSync(path.resolve(paths.appPath, 'proxy.js'));
}

if (proxyFileExists()) {
if (typeof proxy === 'string') {
console.log(
chalk.red('proxy.js found, ignoring "proxy" in package.json')
);
}

const getProxyConfig = require(path.resolve(paths.appPath, 'proxy.js'));
return getProxyConfig(paths, resolveLoopback, mayProxy, onProxyError);
}

// Support proxy as a string for those who are using the simple proxy option
if (typeof proxy === 'string') {
if (!/^http(s)?:\/\//.test(proxy)) {
Expand Down Expand Up @@ -348,40 +359,6 @@ function prepareProxy(proxy, appPublicFolder) {
},
];
}

// Otherwise, proxy is an object so create an array of proxies to pass to webpackDevServer
return Object.keys(proxy).map(function(context) {
if (!proxy[context].hasOwnProperty('target')) {
console.log(
chalk.red(
'When `proxy` in package.json is as an object, each `context` object must have a ' +
'`target` property specified as a url string'
)
);
process.exit(1);
}
let target;
if (process.platform === 'win32') {
target = resolveLoopback(proxy[context].target);
} else {
target = proxy[context].target;
}
return Object.assign({}, proxy[context], {
context: function(pathname) {
return mayProxy(pathname) && pathname.match(context);
},
onProxyReq: proxyReq => {
// Browers may send Origin headers even with same-origin
// requests. To prevent CORS issues, we have to change
// the Origin to match the target URL.
if (proxyReq.getHeader('origin')) {
proxyReq.setHeader('origin', target);
}
},
target,
onError: onProxyError(target),
});
});
}

function choosePort(host, defaultPort) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ checkBrowsers(paths.appPath)
const compiler = createCompiler(webpack, config, appName, urls, useYarn);
// Load proxy config
const proxySetting = require(paths.appPackageJson).proxy;
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
const proxyConfig = prepareProxy(proxySetting, paths);
// Serve webpack assets generated by the compiler over a web sever.
const serverConfig = createDevServerConfig(
proxyConfig,
Expand Down

0 comments on commit 4412385

Please sign in to comment.