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