-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate the requirePage patch to ast-grep
- Loading branch information
Showing
5 changed files
with
95 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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
82 changes: 82 additions & 0 deletions
82
packages/cloudflare/src/cli/build/patches/plugins/require-page.ts
This file contains 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,82 @@ | ||
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 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: /\/next\/dist\/server\/require\.js$/ }, async ({ path }) => { | ||
const jsCode = await readFile(path, "utf8"); | ||
if (/function requirePage\(/.test(jsCode)) { | ||
return { contents: patchCode(jsCode, getRule(buildOpts)) }; | ||
} | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
function getRule(buildOpts: BuildOptions) { | ||
// Load manifests | ||
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")); | ||
|
||
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); | ||
$$$ | ||
}`, | ||
}, | ||
fix: ` | ||
function requirePage($PAGE, $DIST_DIR, $IS_APPP_ATH) { | ||
const pagePath = getPagePath($PAGE); | ||
${fnBody}; | ||
}`, | ||
} satisfies RuleConfig; | ||
} |
1 change: 0 additions & 1 deletion
1
packages/cloudflare/src/cli/build/patches/to-investigate/index.ts
This file contains 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.