Skip to content

Commit

Permalink
feat: support CRA-style proxy config (#1674)
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Apr 15, 2022
1 parent 8d3bd42 commit 0346549
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-peas-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-cli': minor
---

Supports consuming "proxy" from package.json to proxy API requests in watch mode
38 changes: 38 additions & 0 deletions packages/cli/lib/lib/webpack/webpack-client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,43 @@ function isProd(env) {
return prodConfig;
}

function setupProxy(target) {
if (!target) {
return;
}

const errorTemplate = warn =>
`Invalid proxy configuration in package.json. ${warn} Skipping and using default.`;

if (typeof target !== 'string') {
warn(errorTemplate('If provided, "proxy" must be a string.'));
return;
} else if (!/https?:\/\//.test(target)) {
warn(
errorTemplate(
'If provided, "proxy" must start with "http://" or "https://".'
)
);
return;
}

return {
target,
logLevel: 'warn',
context: (pathname, req) => {
return (
req.method != 'GET' ||
(!(/^\/?assets/.test(pathname) || pathname.startsWith('/ws')) &&
req.headers.accept.indexOf('html') === -1)
);
},
secure: false,
changeOrigin: true,
ws: true,
xfwd: true,
};
}

/**
* @returns {import('webpack').Configuration}
*/
Expand Down Expand Up @@ -315,6 +352,7 @@ function isDev(env) {
logging: 'none',
overlay: false,
},
proxy: setupProxy(env.pkg.proxy),
},
};
}
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/tests/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ describe('preact build', () => {
// The tsconfig.json in the template covers the test directory,
// so TS will error out if it can't find even test-only module definitions
shell.cd(dir);
shell.exec('npm i @types/enzyme enzyme-adapter-preact-pure');
//shell.exec('npm i @types/enzyme@3.10.11 enzyme-adapter-preact-pure');
// Remove when https://github.com/preactjs/enzyme-adapter-preact-pure/issues/161 is resolved
shell.exec('rm tsconfig.json');

expect(() => build(dir)).not.toThrow();
});
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ exports.getServer = (dir, port = 3000) => {
maxAge: 0,
single: true,
};
return polka().use(sirv(dir, opts)).listen(port);
return polka()
.use(sirv(dir, opts))
.get('/proxied/request', (_req, res) => {
res.end('Hello World!');
})
.listen(port);
};
14 changes: 14 additions & 0 deletions packages/cli/tests/subjects/proxy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { h } from 'preact';
import { useEffect, useState } from 'preact/hooks';

export default () => {
const [val, setVal] = useState('');

useEffect(() => {
fetch('/proxied/request')
.then(res => res.text())
.then(data => setVal(data));
}, []);

return <h1>Data retrieved from proxied server: {val}</h1>;
};
5 changes: 5 additions & 0 deletions packages/cli/tests/subjects/proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"private": true,
"name": "preact-proxy",
"proxy": "http://localhost:8086"
}
19 changes: 19 additions & 0 deletions packages/cli/tests/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { resolve } = require('path');
const startChrome = require('./lib/chrome');
const { create, watch } = require('./lib/cli');
const { determinePort } = require('../lib/commands/watch');
const { subject } = require('./lib/output');
const { getServer } = require('./server');

const { loadPage, waitUntilExpression } = startChrome;
let chrome, server;
Expand Down Expand Up @@ -62,6 +64,23 @@ describe('preact', () => {

server.close();
});

it('should proxy requests when "proxy" exists in package.json', async () => {
const api = getServer('', 8086);
let app = await subject('proxy');

server = await watch(app, 8087);

let page = await loadPage(chrome, 'http://127.0.0.1:8087/');

await waitUntilExpression(
page,
`document.querySelector('h1').innerText === 'Data retrieved from proxied server: Hello World!'`
);

server.close();
api.server.close();
});
});

describe('should determine the correct port', () => {
Expand Down

0 comments on commit 0346549

Please sign in to comment.