-
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 (#287)
- Loading branch information
Showing
6 changed files
with
147 additions
and
59 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
88 changes: 88 additions & 0 deletions
88
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,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 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. | ||
* | ||
* 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 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.