DEV: Fix alternative output directories in ember-cli proxy (#25291)

Specifying alternatives to `dist/` is useful when running multiple servers locally (e.g. for performance testing)
This commit is contained in:
David Taylor 2024-01-29 14:09:41 +00:00 committed by GitHub
parent 63f6bd5efe
commit ed2802eeb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 6 deletions

View File

@ -10,8 +10,11 @@ const { env } = require("node:process");
const { glob } = require("glob"); const { glob } = require("glob");
const { HTMLRewriter } = require("html-rewriter-wasm"); const { HTMLRewriter } = require("html-rewriter-wasm");
async function listDistAssets() { async function listDistAssets(outputPath) {
const files = await glob("**/*.js", { nodir: true, cwd: "dist/assets" }); const files = await glob("**/*.js", {
nodir: true,
cwd: `${outputPath}/assets`,
});
return new Set(files); return new Set(files);
} }
@ -89,7 +92,7 @@ function updateScriptReferences({
}); });
} }
async function handleRequest(proxy, baseURL, req, res) { async function handleRequest(proxy, baseURL, req, res, outputPath) {
// x-forwarded-host is used in e.g. GitHub CodeSpaces // x-forwarded-host is used in e.g. GitHub CodeSpaces
let originalHost = req.headers["x-forwarded-host"] || req.headers.host; let originalHost = req.headers["x-forwarded-host"] || req.headers.host;
@ -178,8 +181,8 @@ async function handleRequest(proxy, baseURL, req, res) {
if (isHTML) { if (isHTML) {
const [responseText, chunkInfoText, distAssets] = await Promise.all([ const [responseText, chunkInfoText, distAssets] = await Promise.all([
response.text(), response.text(),
fsPromises.readFile("dist/assets.json", "utf-8"), fsPromises.readFile(`${outputPath}/assets.json`, "utf-8"),
listDistAssets(), listDistAssets(outputPath),
]); ]);
const chunkInfos = JSON.parse(chunkInfoText); const chunkInfos = JSON.parse(chunkInfoText);
@ -233,6 +236,7 @@ module.exports = {
serverMiddleware(config) { serverMiddleware(config) {
const app = config.app; const app = config.app;
let { proxy, rootURL, baseURL } = config.options; let { proxy, rootURL, baseURL } = config.options;
const outputPath = config.options.path ?? config.options.outputPath;
if (!proxy) { if (!proxy) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
@ -268,7 +272,7 @@ to serve API requests. For example:
app.use(pathRestrictedRawMiddleware, async (req, res, next) => { app.use(pathRestrictedRawMiddleware, async (req, res, next) => {
try { try {
if (this.shouldHandleRequest(req, baseURL)) { if (this.shouldHandleRequest(req, baseURL)) {
await handleRequest(proxy, baseURL, req, res); await handleRequest(proxy, baseURL, req, res, outputPath);
} else { } else {
// Fixes issues when using e.g. "localhost" instead of loopback IP address // Fixes issues when using e.g. "localhost" instead of loopback IP address
req.headers.host = "127.0.0.1"; req.headers.host = "127.0.0.1";