Skip to content

Commit

Permalink
Support .proxyrc.cjs (#8833)
Browse files Browse the repository at this point in the history
  • Loading branch information
ittaibaratz authored Feb 16, 2023
1 parent adb01fe commit 9d1933b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {createProxyMiddleware} = require('http-proxy-middleware');

module.exports = function(app) {
app.use(createProxyMiddleware('/api', {
target: 'http://localhost:9753/',
pathRewrite: {
'^/api': ''
}
}));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return 'Hello, Parcel.js!';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


26 changes: 26 additions & 0 deletions packages/core/integration-tests/test/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,30 @@ describe('proxy', function () {
data = await get('/api/get', port);
assert.equal(data, 'Request URL: /get');
});

it('should handle proxy table written in .proxyrc.cjs', async function () {
let dir = path.join(__dirname, 'integration/proxyrc-cjs');
inputFS.chdir(dir);

let port = await getPort();
let b = bundler(path.join(dir, 'index.js'), {
config,
serveOptions: {
https: false,
port: port,
host: 'localhost',
},
});

subscription = await b.watch();
await getNextBuild(b);

server = apiServer();

let data = await get('/index.js', port);
assert.notEqual(data, 'Request URL: /index.js');

data = await get('/api/get', port);
assert.equal(data, 'Request URL: /get');
});
});
7 changes: 3 additions & 4 deletions packages/reporters/dev-server/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export default class Server {
const pkg = await loadConfig(
this.options.inputFS,
fileInRoot,
['.proxyrc.js', '.proxyrc', '.proxyrc.json'],
['.proxyrc.cjs', '.proxyrc.js', '.proxyrc', '.proxyrc.json'],
this.options.projectRoot,
);

Expand All @@ -397,11 +397,10 @@ export default class Server {
const cfg = pkg.config;
const filename = path.basename(pkg.files[0].filePath);

if (filename === '.proxyrc.js') {
if (filename === '.proxyrc.js' || filename === '.proxyrc.cjs') {
if (typeof cfg !== 'function') {
this.options.logger.warn({
message:
"Proxy configuration file '.proxyrc.js' should export a function. Skipping...",
message: `Proxy configuration file '${filename}' should export a function. Skipping...`,
});
return this;
}
Expand Down

0 comments on commit 9d1933b

Please sign in to comment.