-
Notifications
You must be signed in to change notification settings - Fork 73
refactor: migrate the requirePage patch to ast-grep #287
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
88 changes: 88 additions & 0 deletions
88
packages/cloudflare/src/cli/build/patches/plugins/require-page.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,88 @@ | ||
import { existsSync, readFileSync } from "node:fs"; | ||
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 type { PluginBuild } from "esbuild"; | ||
|
||
import { patchCode, type RuleConfig } from "../ast/util.js"; | ||
|
||
export default function inlineRequirePagePlugin(buildOpts: BuildOptions) { | ||
return { | ||
name: "inline-require-page", | ||
|
||
setup: async (build: PluginBuild) => { | ||
build.onLoad( | ||
{ | ||
filter: getCrossPlatformPathRegex(String.raw`/next/dist/server/require\.js$`, { escape: false }), | ||
}, | ||
async ({ path }) => { | ||
const jsCode = await readFile(path, "utf8"); | ||
if (/function requirePage\(/.test(jsCode)) { | ||
return { contents: patchCode(jsCode, getRule(buildOpts)) }; | ||
} | ||
} | ||
); | ||
}, | ||
}; | ||
} | ||
|
||
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"); | ||
const appPathsManifestFile = join(serverDir, "app-paths-manifest.json"); | ||
|
||
const pagesManifests: string[] = existsSync(pagesManifestFile) | ||
? Object.values(JSON.parse(readFileSync(pagesManifestFile, "utf-8"))) | ||
: []; | ||
const appPathsManifests: string[] = existsSync(appPathsManifestFile) | ||
? Object.values(JSON.parse(readFileSync(appPathsManifestFile, "utf-8"))) | ||
: []; | ||
const manifests = pagesManifests.concat(appPathsManifests); | ||
|
||
const htmlFiles = manifests.filter((file) => file.endsWith(".html")); | ||
const jsFiles = manifests.filter((file) => file.endsWith(".js")); | ||
|
||
// Inline fs access and dynamic require that are not supported by workerd. | ||
const fnBody = ` | ||
// html | ||
${htmlFiles | ||
.map( | ||
(file) => `if (pagePath.endsWith("${file}")) { | ||
return ${JSON.stringify(readFileSync(join(serverDir, file), "utf-8"))}; | ||
}` | ||
) | ||
.join("\n")} | ||
// js | ||
process.env.__NEXT_PRIVATE_RUNTIME_TYPE = isAppPath ? 'app' : 'pages'; | ||
try { | ||
${jsFiles | ||
.map( | ||
(file) => `if (pagePath.endsWith("${file}")) { | ||
return require(${JSON.stringify(join(serverDir, file))}); | ||
}` | ||
) | ||
.join("\n")} | ||
} finally { | ||
process.env.__NEXT_PRIVATE_RUNTIME_TYPE = ''; | ||
} | ||
`; | ||
|
||
return { | ||
rule: { | ||
pattern: ` | ||
function requirePage($PAGE, $DIST_DIR, $IS_APPP_ATH) { | ||
const $_ = getPagePath($$$ARGS); | ||
$$$_BODY | ||
}`, | ||
}, | ||
fix: ` | ||
function requirePage($PAGE, $DIST_DIR, $IS_APPP_ATH) { | ||
const pagePath = getPagePath($$$ARGS); | ||
${fnBody} | ||
}`, | ||
} satisfies RuleConfig; | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/cloudflare/src/cli/build/patches/plugins/wrangler-external.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,43 @@ | ||
/** | ||
* ESBuild plugin to mark files bundled by wrangler as external. | ||
* | ||
* `.wasm` and `.bin` will ultimately be bundled by wrangler. | ||
* We should only mark them as external in the adapter. | ||
* | ||
* However simply marking them as external would copy the import path to the bundle, | ||
* i.e. `import("./file.wasm?module")` and given than the bundle is generated in a | ||
* different location than the input files, the relative path would not be valid. | ||
vicb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* This ESBuild plugin convert relative paths to absolute paths so that they are | ||
* still valid from inside the bundle. | ||
* | ||
* ref: https://developers.cloudflare.com/workers/wrangler/bundling/ | ||
*/ | ||
|
||
import { dirname, resolve } from "node:path"; | ||
|
||
import type { PluginBuild } from "esbuild"; | ||
|
||
export default function setWranglerExternal() { | ||
return { | ||
name: "wrangler-externals", | ||
|
||
setup: async (build: PluginBuild) => { | ||
const namespace = "wrangler-externals-plugin"; | ||
|
||
build.onResolve({ filter: /(\.bin|\.wasm\?module)$/ }, ({ path, importer }) => { | ||
return { | ||
path: resolve(dirname(importer), path), | ||
namespace, | ||
external: true, | ||
}; | ||
}); | ||
|
||
build.onLoad({ filter: /.*/, namespace }, async ({ path }) => { | ||
return { | ||
contents: `export * from '${path}';`, | ||
}; | ||
}); | ||
}, | ||
}; | ||
} |
1 change: 0 additions & 1 deletion
1
packages/cloudflare/src/cli/build/patches/to-investigate/index.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
53 changes: 0 additions & 53 deletions
53
packages/cloudflare/src/cli/build/patches/to-investigate/inline-next-require.ts
This file was deleted.
Oops, something went wrong.
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.