UNPKG

@aziontech/opennextjs-azion

Version:
53 lines (52 loc) 2.13 kB
/** * This code was originally copied and modified from the @opennextjs/cloudflare repository. * Significant changes have been made to adapt it for use with Azion. */ /** * Inline `loadManifest` as it relies on `readFileSync` that is not supported by workerd. */ import { readFile } from "node:fs/promises"; import { join, posix, relative, sep } from "node:path"; import { getPackagePath } from "@opennextjs/aws/build/helper.js"; import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; import { glob } from "glob"; import { normalizePath } from "../../utils/normalize-path.js"; export function inlineLoadManifest(updater, buildOpts) { return updater.updateContent("inline-load-manifest", [ { field: { filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/load-manifest(\.external)?\.js$`, { escape: false, }), contentFilter: /function loadManifest\(/, callback: async ({ contents }) => patchCode(contents, await getRule(buildOpts)), }, }, ]); } async function getRule(buildOpts) { const { outputDir } = buildOpts; const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts)); const dotNextDir = join(baseDir, ".next"); const manifests = await glob(join(dotNextDir, "**/*-manifest.json"), { windowsPathsNoEscape: true }); const returnManifests = (await Promise.all(manifests.map(async (manifest) => ` if ($PATH.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) { return ${await readFile(manifest, "utf-8")}; } `))).join("\n"); return { rule: { pattern: ` function loadManifest($PATH, $$$ARGS) { $$$_ }`, }, fix: ` function loadManifest($PATH, $$$ARGS) { $PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)}); ${returnManifests} throw new Error(\`Unexpected loadManifest(\${$PATH}) call!\`); }`, }; }