Skip to content

Commit 331bed8

Browse files
authored
Support .proxyrc.ts (#9654)
1 parent ae90f08 commit 331bed8

File tree

7 files changed

+70
-26
lines changed

7 files changed

+70
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const {createProxyMiddleware} = require('http-proxy-middleware');
2+
3+
module.exports = function(app: any) {
4+
app.use(createProxyMiddleware('/api', {
5+
target: 'http://localhost:9753/',
6+
pathRewrite: {
7+
'^/api': ''
8+
}
9+
}));
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function () {
2+
return 'Hello, Parcel.js!';
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+

packages/core/integration-tests/test/proxy.js

+26
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,30 @@ describe('proxy', function () {
196196
data = await get('/api/get', port);
197197
assert.equal(data, 'Request URL: /get');
198198
});
199+
200+
it('should handle proxy table written in .proxyrc.ts', async function () {
201+
let dir = path.join(__dirname, 'integration/proxyrc-ts');
202+
inputFS.chdir(dir);
203+
204+
let port = await getPort();
205+
let b = bundler(path.join(dir, 'index.js'), {
206+
config,
207+
serveOptions: {
208+
https: false,
209+
port: port,
210+
host: 'localhost',
211+
},
212+
});
213+
214+
subscription = await b.watch();
215+
await getNextBuild(b);
216+
217+
server = apiServer();
218+
219+
let data = await get('/index.js', port);
220+
assert.notEqual(data, 'Request URL: /index.js');
221+
222+
data = await get('/api/get', port);
223+
assert.equal(data, 'Request URL: /get');
224+
});
199225
});

packages/core/package-manager/src/NodePackageManager.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ export class NodePackageManager implements PackageManager {
215215
if (!filePath.includes(NODE_MODULES)) {
216216
let extname = path.extname(filePath);
217217
if (
218-
(extname === '.ts' || extname === '.tsx') &&
218+
(extname === '.ts' ||
219+
extname === '.tsx' ||
220+
extname === '.mts' ||
221+
extname === '.cts') &&
219222
// $FlowFixMe
220223
!Module._extensions[extname]
221224
) {

packages/dev/repl/.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"rules": {
1212
"react/jsx-no-bind": "off",
13-
"no-console": "off"
13+
"no-console": "off",
14+
"no-use-before-define": "off"
1415
},
1516
"globals": {
1617
"globalThis": "readonly"

packages/reporters/dev-server/src/Server.js

+21-24
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ export default class Server {
419419
this.options.inputFS,
420420
fileInRoot,
421421
[
422+
'.proxyrc.cts',
423+
'.proxyrc.mts',
424+
'.proxyrc.ts',
422425
'.proxyrc.cjs',
423426
'.proxyrc.mjs',
424427
'.proxyrc.js',
@@ -434,13 +437,24 @@ export default class Server {
434437

435438
const filename = path.basename(configFilePath);
436439

437-
if (
438-
filename === '.proxyrc.js' ||
439-
filename === '.proxyrc.cjs' ||
440-
filename === '.proxyrc.mjs'
441-
) {
442-
// $FlowFixMe
443-
// let cfg = (await import(configFilePath)).default;
440+
if (filename === '.proxyrc' || filename === '.proxyrc.json') {
441+
let conf = await readConfig(this.options.inputFS, configFilePath);
442+
if (!conf) {
443+
return this;
444+
}
445+
let cfg = conf.config;
446+
if (typeof cfg !== 'object') {
447+
this.options.logger.warn({
448+
message:
449+
"Proxy table in '.proxyrc' should be of object type. Skipping...",
450+
});
451+
return this;
452+
}
453+
for (const [context, options] of Object.entries(cfg)) {
454+
// each key is interpreted as context, and value as middleware options
455+
app.use(createProxyMiddleware(context, options));
456+
}
457+
} else {
444458
let cfg = await this.options.packageManager.require(
445459
configFilePath,
446460
fileInRoot,
@@ -459,23 +473,6 @@ export default class Server {
459473
return this;
460474
}
461475
cfg(app);
462-
} else if (filename === '.proxyrc' || filename === '.proxyrc.json') {
463-
let conf = await readConfig(this.options.inputFS, configFilePath);
464-
if (!conf) {
465-
return this;
466-
}
467-
let cfg = conf.config;
468-
if (typeof cfg !== 'object') {
469-
this.options.logger.warn({
470-
message:
471-
"Proxy table in '.proxyrc' should be of object type. Skipping...",
472-
});
473-
return this;
474-
}
475-
for (const [context, options] of Object.entries(cfg)) {
476-
// each key is interpreted as context, and value as middleware options
477-
app.use(createProxyMiddleware(context, options));
478-
}
479476
}
480477

481478
return this;

0 commit comments

Comments
 (0)