Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as patches from "./patches/index.js";
import { ContentUpdater } from "./patches/plugins/content-updater.js";
import { inlineEvalManifest } from "./patches/plugins/eval-manifest.js";
import { patchFetchCacheSetMissingWaitUntil } from "./patches/plugins/fetch-cache-wait-until.js";
import { inlineFindDir } from "./patches/plugins/find-dir.js";
import { patchLoadInstrumentation } from "./patches/plugins/load-instrumentation.js";
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
import { fixRequire } from "./patches/plugins/require.js";
Expand Down Expand Up @@ -91,6 +92,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
patchLoadInstrumentation(updater),
patchFetchCacheSetMissingWaitUntil(updater),
inlineEvalManifest(updater, buildOpts),
inlineFindDir(updater, buildOpts),
// Apply updater updaters, must be the last plugin
updater.plugin,
],
Expand Down Expand Up @@ -195,7 +197,6 @@ export async function updateWorkerBundledCode(
["require", patches.patchRequire],
["`buildId` function", (code) => patches.patchBuildId(code, buildOpts)],
["`loadManifest` function", (code) => patches.patchLoadManifest(code, buildOpts)],
["`findDir` function", (code) => patches.patchFindDir(code, buildOpts)],
["cacheHandler", (code) => patches.patchCache(code, buildOpts)],
[
"'require(this.middlewareManifestPath)'",
Expand Down
50 changes: 50 additions & 0 deletions packages/cloudflare/src/cli/build/patches/plugins/find-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Inline `evalManifest` as it relies on `readFileSync` and `runInNewContext`
* that are not supported by workerd.
*/

import { existsSync } from "node:fs";
import { join } from "node:path";

import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";

import { patchCode } from "../ast/util.js";
import type { ContentUpdater } from "./content-updater.js";

export function inlineFindDir(updater: ContentUpdater, buildOpts: BuildOptions) {
return updater.updateContent(
"inline-find-dir",
{
filter: getCrossPlatformPathRegex(String.raw`/next/dist/lib/find-pages-dir\.js$`, { escape: false }),
contentFilter: /function findDir\(/,
},
async ({ contents }) => patchCode(contents, await getRule(buildOpts))
);
}

async function getRule(buildOpts: BuildOptions) {
const { outputDir } = buildOpts;

const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server");

const appExists = existsSync(join(baseDir, "app"));
const pagesExists = existsSync(join(baseDir, "pages"));

return `
rule:
pattern: function findDir($DIR, $NAME) { $$$_ }
fix: |-
function findDir($DIR, $NAME) {
if ($DIR.endsWith(".next/server")) {
if ($NAME === "app") {
return ${appExists};
}
if ($NAME === "pages") {
return ${pagesExists};
}
}
throw new Error(\`Unexpected findDir(\${$DIR}, \${$NAME}) call!\`);
}
`;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./inline-middleware-manifest-require.js";
export * from "./patch-exception-bubbling.js";
export * from "./patch-find-dir.js";
export * from "./patch-read-file.js";

This file was deleted.