From 9d1933b3d89a3f57f3ac1c70f4fb755738d78604 Mon Sep 17 00:00:00 2001 From: Ittai Baratz Date: Thu, 16 Feb 2023 09:14:16 -0800 Subject: [PATCH] Support .proxyrc.cjs (#8833) --- .../test/integration/proxyrc-cjs/.proxyrc.cjs | 10 +++++++ .../test/integration/proxyrc-cjs/index.js | 3 +++ .../test/integration/proxyrc-cjs/yarn.lock | 4 +++ packages/core/integration-tests/test/proxy.js | 26 +++++++++++++++++++ packages/reporters/dev-server/src/Server.js | 7 +++-- 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 packages/core/integration-tests/test/integration/proxyrc-cjs/.proxyrc.cjs create mode 100644 packages/core/integration-tests/test/integration/proxyrc-cjs/index.js create mode 100644 packages/core/integration-tests/test/integration/proxyrc-cjs/yarn.lock diff --git a/packages/core/integration-tests/test/integration/proxyrc-cjs/.proxyrc.cjs b/packages/core/integration-tests/test/integration/proxyrc-cjs/.proxyrc.cjs new file mode 100644 index 00000000000..18a72c3f458 --- /dev/null +++ b/packages/core/integration-tests/test/integration/proxyrc-cjs/.proxyrc.cjs @@ -0,0 +1,10 @@ +const {createProxyMiddleware} = require('http-proxy-middleware'); + +module.exports = function(app) { + app.use(createProxyMiddleware('/api', { + target: 'http://localhost:9753/', + pathRewrite: { + '^/api': '' + } + })); +}; diff --git a/packages/core/integration-tests/test/integration/proxyrc-cjs/index.js b/packages/core/integration-tests/test/integration/proxyrc-cjs/index.js new file mode 100644 index 00000000000..313f4281067 --- /dev/null +++ b/packages/core/integration-tests/test/integration/proxyrc-cjs/index.js @@ -0,0 +1,3 @@ +module.exports = function () { + return 'Hello, Parcel.js!'; +}; diff --git a/packages/core/integration-tests/test/integration/proxyrc-cjs/yarn.lock b/packages/core/integration-tests/test/integration/proxyrc-cjs/yarn.lock new file mode 100644 index 00000000000..fb57ccd13af --- /dev/null +++ b/packages/core/integration-tests/test/integration/proxyrc-cjs/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + diff --git a/packages/core/integration-tests/test/proxy.js b/packages/core/integration-tests/test/proxy.js index 34e53dd7fa7..aac2fce656c 100644 --- a/packages/core/integration-tests/test/proxy.js +++ b/packages/core/integration-tests/test/proxy.js @@ -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'); + }); }); diff --git a/packages/reporters/dev-server/src/Server.js b/packages/reporters/dev-server/src/Server.js index f974f4f4af7..af997f12944 100644 --- a/packages/reporters/dev-server/src/Server.js +++ b/packages/reporters/dev-server/src/Server.js @@ -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, ); @@ -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; }