Try importing custom-backend-task definitions directly to avoid bundling issues.

This commit is contained in:
Dillon Kearns 2023-01-27 10:18:19 -08:00
parent 5f8ce1b2eb
commit 19004f9180
3 changed files with 8 additions and 15 deletions

View File

@ -1,4 +1,5 @@
import fs from "fs";
import * as fs from "fs";
import * as path from "path";
export default async function run({
renderFunctionFilePath,
@ -19,11 +20,6 @@ export default async function run({
renderFunctionFilePath,
"./functions/server-render/elm-pages-cli.cjs"
);
fs.copyFileSync(portsFilePath, "./functions/render/custom-backend-task.mjs");
fs.copyFileSync(
portsFilePath,
"./functions/server-render/custom-backend-task.mjs"
);
fs.writeFileSync(
"./functions/render/index.mjs",
@ -108,6 +104,7 @@ import * as busboy from "busboy";
import { fileURLToPath } from "url";
import * as renderer from "../../../../generator/src/render.js";
import * as preRenderHtml from "../../../../generator/src/pre-render-html.js";
import * as customBackendTask from "${path.resolve(portsFilePath)}";
const htmlTemplate = ${JSON.stringify(htmlTemplate)};
${
@ -128,7 +125,6 @@ export const handler = render;`
async function render(event, context) {
const requestTime = new Date();
console.log(JSON.stringify(event));
const compiledPortsFile = "./functions/server-render/custom-backend-task.mjs";
try {
const basePath = "/";
@ -136,7 +132,7 @@ async function render(event, context) {
const addWatcher = () => {};
const renderResult = await renderer.render(
compiledPortsFile,
customBackendTask,
basePath,
(await import("./elm-pages-cli.cjs")).default,
mode,

View File

@ -4,12 +4,13 @@ import * as fs from "./dir-helpers.js";
import { readFileSync, writeFileSync } from "fs";
import { stat } from "fs/promises";
import { parentPort, threadId, workerData } from "worker_threads";
import * as url from "url";
async function run({ mode, pathname, serverRequest, portsFilePath }) {
console.time(`${threadId} ${pathname}`);
try {
const renderResult = await renderer.render(
portsFilePath,
await import(url.pathToFileURL(path.resolve(portsFilePath)).href),
workerData.basePath,
await requireElm(mode),
mode,

View File

@ -10,7 +10,7 @@ const defaultHttpCachePath = "./.elm-pages/http-cache";
/**
* @param {string} mode
* @param {{url: string;headers: {[x: string]: string;};method: string;body: Body;}} rawRequest
* @param {string} portsFile
* @param {Record<string, unknown>} portsFile
* @param {boolean} hasFsAccess
* @returns {Promise<Response>}
*/
@ -27,16 +27,12 @@ export function lookupOrPerform(
return new Promise(async (resolve, reject) => {
const request = toRequest(rawRequest);
let portBackendTask = {};
let portBackendTask = portsFile;
let portBackendTaskImportError = null;
try {
if (portsFile === undefined) {
throw "missing";
}
const portBackendTaskPath = path.resolve(portsFile);
// On Windows, we need cannot use paths directly and instead must use a file:// URL.
// portBackendTask = await require(url.pathToFileURL(portBackendTaskPath).href);
portBackendTask = await import(portBackendTaskPath);
} catch (e) {
portBackendTaskImportError = e;
}