Skip to content

Commit

Permalink
adjust wrangler patching logic to use the correct `node_modules/next/…
Browse files Browse the repository at this point in the history
…dist` directory
  • Loading branch information
dario-piotrowicz authored and vicb committed Oct 3, 2024
1 parent 3628be6 commit cf5113b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ import path from "node:path";
export function patchWranglerDeps(config: Config) {
console.log("# patchWranglerDeps");

const distPath = getDistPath(config);
// Patch .next/standalone/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js
//
// Remove the need for an alias in wrangler.toml:
//
// [alias]
// # critters is `require`d from `pages.runtime.prod.js` when running wrangler dev, so we need to stub it out
// "critters" = "./.next/standalone/node_modules/cf/templates/shims/empty.ts"
const pagesRuntimeFile = path.join(
config.paths.standaloneApp,
"node_modules",
"next",
"dist",
"compiled",
"next-server",
"pages.runtime.prod.js"
);
const pagesRuntimeFile = path.join(distPath, "compiled", "next-server", "pages.runtime.prod.js");

const patchedPagesRuntime = fs
.readFileSync(pagesRuntimeFile, "utf-8")
Expand All @@ -38,20 +31,36 @@ export function patchWranglerDeps(config: Config) {
// # try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
// # causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
// #"@opentelemetry/api" = "./.next/standalone/node_modules/cf/templates/shims/throw.ts"
const tracerFile = path.join(
config.paths.standaloneApp,
"node_modules",
"next",
"dist",
"server",
"lib",
"trace",
"tracer.js"
);
const tracerFile = path.join(distPath, "server", "lib", "trace", "tracer.js");

const pacthedTracer = fs
.readFileSync(tracerFile, "utf-8")
.replaceAll(/\w+\s*=\s*require\([^/]*opentelemetry.*\)/g, `throw new Error("@opentelemetry/api")`);

writeFileSync(tracerFile, pacthedTracer);
}

/**
* Next.js saves the node_modules/next/dist directory in either the standaloneApp path or in the
* standaloneRoot path, this depends on where the next dependency is actually saved (
* https://github.com/vercel/next.js/blob/39e06c75/packages/next/src/build/webpack-config.ts#L103-L104
* ) and can depend on the package manager used, if it is using workspaces, etc...
*
* This function checks the two potential paths for the dist directory and returns the first that it finds,
* it throws an error if it can't find either
*
* @param config
* @returns the node_modules/next/dist directory path
*/
function getDistPath(config: Config): string {
for (const root of [config.paths.standaloneApp, config.paths.standaloneRoot]) {
try {
const distPath = path.join(root, "node_modules", "next", "dist");
if (fs.statSync(distPath).isDirectory()) return distPath;
} catch {
/* empty */
}
}

throw new Error("Unexpected error: unable to detect the node_modules/next/dist directory");
}
6 changes: 5 additions & 1 deletion packages/cloudflare/src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type Config = {
builderOutput: string;
// Path to the app's `.next` directory (where `next build` saves the build output)
dotNext: string;
// Path to the application standalone root directory
standaloneRoot: string;
// Path to the application standalone directory (where `next build` saves the standalone app)
standaloneApp: string;
// Path to the `.next` directory specific to the standalone application
Expand Down Expand Up @@ -47,7 +49,8 @@ export type Config = {
export function getConfig(appDir: string, outputDir: string): Config {
const dotNext = path.join(outputDir, ".next");
const appPath = getNextjsApplicationPath(dotNext).replace(/\/$/, "");
const standaloneApp = path.join(dotNext, "standalone", appPath);
const standaloneRoot = path.join(dotNext, "standalone");
const standaloneApp = path.join(standaloneRoot, appPath);
const standaloneAppDotNext = path.join(standaloneApp, ".next");
const standaloneAppServer = path.join(standaloneAppDotNext, "server");

Expand All @@ -59,6 +62,7 @@ export function getConfig(appDir: string, outputDir: string): Config {
nextApp: appDir,
builderOutput: outputDir,
dotNext,
standaloneRoot,
standaloneApp,
standaloneAppDotNext,
standaloneAppServer,
Expand Down
9 changes: 2 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf5113b

Please sign in to comment.