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

View File

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

View File

@ -10,7 +10,7 @@ const defaultHttpCachePath = "./.elm-pages/http-cache";
/** /**
* @param {string} mode * @param {string} mode
* @param {{url: string;headers: {[x: string]: string;};method: string;body: Body;}} rawRequest * @param {{url: string;headers: {[x: string]: string;};method: string;body: Body;}} rawRequest
* @param {string} portsFile * @param {Record<string, unknown>} portsFile
* @param {boolean} hasFsAccess * @param {boolean} hasFsAccess
* @returns {Promise<Response>} * @returns {Promise<Response>}
*/ */
@ -27,16 +27,12 @@ export function lookupOrPerform(
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const request = toRequest(rawRequest); const request = toRequest(rawRequest);
let portBackendTask = {}; let portBackendTask = portsFile;
let portBackendTaskImportError = null; let portBackendTaskImportError = null;
try { try {
if (portsFile === undefined) { if (portsFile === undefined) {
throw "missing"; 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) { } catch (e) {
portBackendTaskImportError = e; portBackendTaskImportError = e;
} }