|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import { join, posix, sep } from "node:path"; |
| 3 | + |
| 4 | +import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js"; |
| 5 | +import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; |
| 6 | +import type { Plugin } from "esbuild"; |
| 7 | + |
| 8 | +import { normalizePath } from "../../utils/normalize-path.js"; |
| 9 | +import { patchCode, type RuleConfig } from "../ast/util.js"; |
| 10 | +import type { ContentUpdater } from "./content-updater.js"; |
| 11 | + |
| 12 | +async function getPagesManifests(serverDir: string): Promise<string[]> { |
| 13 | + try { |
| 14 | + return Object.values(JSON.parse(await readFile(join(serverDir, "pages-manifest.json"), "utf-8"))); |
| 15 | + } catch { |
| 16 | + // The file does not exist |
| 17 | + return []; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +async function getAppPathsManifests(serverDir: string): Promise<string[]> { |
| 22 | + try { |
| 23 | + return Object.values(JSON.parse(await readFile(join(serverDir, "app-paths-manifest.json"), "utf-8"))); |
| 24 | + } catch { |
| 25 | + // The file does not exist |
| 26 | + return []; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function getServerDir(buildOpts: BuildOptions) { |
| 31 | + return join(buildOpts.outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server"); |
| 32 | +} |
| 33 | + |
| 34 | +function getRequires(idVariable: string, files: string[], serverDir: string) { |
| 35 | + // Inline fs access and dynamic requires that are not supported by workerd. |
| 36 | + return files |
| 37 | + .map( |
| 38 | + (file) => ` |
| 39 | + if (${idVariable}.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)}).endsWith(${JSON.stringify(normalizePath(file))})) { |
| 40 | + return require(${JSON.stringify(join(serverDir, file))}); |
| 41 | + }` |
| 42 | + ) |
| 43 | + .join("\n"); |
| 44 | +} |
| 45 | + |
| 46 | +export function inlineDynamicRequires(updater: ContentUpdater, buildOpts: BuildOptions): Plugin { |
| 47 | + updater.updateContent( |
| 48 | + "inline-node-module-loader", |
| 49 | + { |
| 50 | + filter: getCrossPlatformPathRegex( |
| 51 | + String.raw`/next/dist/server/lib/module-loader/node-module-loader\.js$`, |
| 52 | + { escape: false } |
| 53 | + ), |
| 54 | + contentFilter: /class NodeModuleLoader {/, |
| 55 | + }, |
| 56 | + async ({ contents }) => patchCode(contents, await getNodeModuleLoaderRule(buildOpts)) |
| 57 | + ); |
| 58 | + updater.updateContent( |
| 59 | + "inline-require-page", |
| 60 | + { |
| 61 | + filter: getCrossPlatformPathRegex(String.raw`/next/dist/server/require\.js$`, { escape: false }), |
| 62 | + contentFilter: /function requirePage\(/, |
| 63 | + }, |
| 64 | + async ({ contents }) => patchCode(contents, await getRequirePageRule(buildOpts)) |
| 65 | + ); |
| 66 | + return { name: "inline-dynamic-requires", setup() {} }; |
| 67 | +} |
| 68 | + |
| 69 | +async function getNodeModuleLoaderRule(buildOpts: BuildOptions) { |
| 70 | + const serverDir = getServerDir(buildOpts); |
| 71 | + |
| 72 | + const manifests = await getPagesManifests(serverDir); |
| 73 | + |
| 74 | + const files = manifests.filter((file) => file.endsWith(".js")); |
| 75 | + |
| 76 | + return ` |
| 77 | +rule: |
| 78 | + kind: method_definition |
| 79 | + all: |
| 80 | + - has: |
| 81 | + field: name |
| 82 | + regex: ^load$ |
| 83 | + - has: |
| 84 | + field: parameters |
| 85 | + has: |
| 86 | + kind: required_parameter |
| 87 | + pattern: $ID |
| 88 | + inside: |
| 89 | + stopBy: |
| 90 | + kind: class_declaration |
| 91 | + kind: class_declaration |
| 92 | + has: |
| 93 | + field: name |
| 94 | + regex: ^NodeModuleLoader$ |
| 95 | +fix: | |
| 96 | + async load($ID) { |
| 97 | + ${getRequires("$ID", files, serverDir)} |
| 98 | + }`; |
| 99 | +} |
| 100 | + |
| 101 | +async function getRequirePageRule(buildOpts: BuildOptions) { |
| 102 | + const serverDir = getServerDir(buildOpts); |
| 103 | + |
| 104 | + const pagesManifests = await getPagesManifests(serverDir); |
| 105 | + const appPathsManifests = await getAppPathsManifests(serverDir); |
| 106 | + |
| 107 | + const manifests = pagesManifests.concat(appPathsManifests); |
| 108 | + |
| 109 | + const htmlFiles = manifests.filter((file) => file.endsWith(".html")); |
| 110 | + const jsFiles = manifests.filter((file) => file.endsWith(".js")); |
| 111 | + |
| 112 | + return { |
| 113 | + rule: { |
| 114 | + pattern: ` |
| 115 | +function requirePage($PAGE, $DIST_DIR, $IS_APP_PATH) { |
| 116 | + const $_ = getPagePath($$$ARGS); |
| 117 | + $$$_BODY |
| 118 | +}`, |
| 119 | + }, // Inline fs access and dynamic require that are not supported by workerd. |
| 120 | + fix: ` |
| 121 | +function requirePage($PAGE, $DIST_DIR, $IS_APP_PATH) { |
| 122 | + const pagePath = getPagePath($$$ARGS).replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)}); |
| 123 | +
|
| 124 | + // html |
| 125 | + ${( |
| 126 | + await Promise.all( |
| 127 | + htmlFiles.map( |
| 128 | + async (file) => `if (pagePath.endsWith(${JSON.stringify(normalizePath(file))})) { |
| 129 | + return ${JSON.stringify(await readFile(join(serverDir, file), "utf-8"))}; |
| 130 | + }` |
| 131 | + ) |
| 132 | + ) |
| 133 | + ).join("\n")} |
| 134 | + // js |
| 135 | + process.env.__NEXT_PRIVATE_RUNTIME_TYPE = $IS_APP_PATH ? 'app' : 'pages'; |
| 136 | + try { |
| 137 | + ${getRequires("pagePath", jsFiles, serverDir)} |
| 138 | + } finally { |
| 139 | + process.env.__NEXT_PRIVATE_RUNTIME_TYPE = ''; |
| 140 | + } |
| 141 | +}`, |
| 142 | + } satisfies RuleConfig; |
| 143 | +} |
0 commit comments