-
Notifications
You must be signed in to change notification settings - Fork 75
patch node-module-loader dynamic require #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2018756
patch node-module-loader dynamic require
HyperKiko f1cc83c
only require javascript files in node module loader patch
HyperKiko 1a073ff
clean up node module loader patch
HyperKiko 4c7b8f4
add pages api route e2e test
HyperKiko 6c84a10
normalize id variable and escape file path
HyperKiko d87c9c1
fix up node module loader rule and merge with require page plugin
HyperKiko 6afac13
pnpm fix and change whitespace in getRequires
HyperKiko 4a30231
fix node module loader rule and add changeset
HyperKiko df638ff
fixup! path separator
vicb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/cloudflare/src/cli/build/patches/plugins/node-module-loader.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { readFile } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
|
|
||
| import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js"; | ||
| import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|
|
||
| import { normalizePath } from "../../utils/normalize-path.js"; | ||
| import { patchCode, type RuleConfig } from "../ast/util.js"; | ||
| import type { ContentUpdater } from "./content-updater.js"; | ||
|
|
||
| export function inlineNodeModuleLoader(updater: ContentUpdater, buildOpts: BuildOptions) { | ||
| return updater.updateContent( | ||
| "inline-node-module-loader", | ||
| { | ||
| filter: getCrossPlatformPathRegex( | ||
| String.raw`/next/dist/server/lib/module-loader/node-module-loader\.js$`, | ||
| { escape: false } | ||
| ), | ||
| contentFilter: /class NodeModuleLoader {/, | ||
| }, | ||
| async ({ contents }) => patchCode(contents, await getRule(buildOpts)) | ||
| ); | ||
| } | ||
|
|
||
| async function getRule(buildOpts: BuildOptions) { | ||
| const { outputDir } = buildOpts; | ||
| const serverDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server"); | ||
|
|
||
| const pagesManifestFile = join(serverDir, "pages-manifest.json"); | ||
|
|
||
| let pagesManifests: string[] = []; | ||
| try { | ||
| pagesManifests = Object.values(JSON.parse(await readFile(pagesManifestFile, "utf-8"))); | ||
| } catch { | ||
| // The file does not exist | ||
| pagesManifests = []; | ||
| } | ||
|
|
||
| const manifests = pagesManifests.map((path) => normalizePath(path)); | ||
|
|
||
| const files = manifests.filter((file) => file.endsWith(".js")); | ||
HyperKiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Inline fs access and dynamic requires that are not supported by workerd. | ||
| const fnBody = ` | ||
| ${files | ||
| .map( | ||
| (file) => `if (id.endsWith("${file}")) { | ||
HyperKiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return require(${JSON.stringify(join(serverDir, file))}); | ||
| }` | ||
| ) | ||
| .join("\n")} | ||
| `; | ||
|
|
||
| return { | ||
| rule: { | ||
| pattern: `class NodeModuleLoader { | ||
| async load($ID) { | ||
HyperKiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $$$_BODY | ||
| } | ||
| }`, | ||
| }, | ||
| fix: `class NodeModuleLoader { | ||
| async load($ID) { | ||
| ${fnBody} | ||
| } | ||
| }`, | ||
| } satisfies RuleConfig; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.