Skip to content

Commit cf5113b

Browse files
dario-piotrowiczvicb
authored andcommitted
adjust wrangler patching logic to use the correct node_modules/next/dist directory
1 parent 3628be6 commit cf5113b

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

packages/cloudflare/src/cli/build/patches/to-investigate/wrangler-deps.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@ import path from "node:path";
55
export function patchWranglerDeps(config: Config) {
66
console.log("# patchWranglerDeps");
77

8+
const distPath = getDistPath(config);
89
// Patch .next/standalone/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js
910
//
1011
// Remove the need for an alias in wrangler.toml:
1112
//
1213
// [alias]
1314
// # critters is `require`d from `pages.runtime.prod.js` when running wrangler dev, so we need to stub it out
1415
// "critters" = "./.next/standalone/node_modules/cf/templates/shims/empty.ts"
15-
const pagesRuntimeFile = path.join(
16-
config.paths.standaloneApp,
17-
"node_modules",
18-
"next",
19-
"dist",
20-
"compiled",
21-
"next-server",
22-
"pages.runtime.prod.js"
23-
);
16+
const pagesRuntimeFile = path.join(distPath, "compiled", "next-server", "pages.runtime.prod.js");
2417

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

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

5640
writeFileSync(tracerFile, pacthedTracer);
5741
}
42+
43+
/**
44+
* Next.js saves the node_modules/next/dist directory in either the standaloneApp path or in the
45+
* standaloneRoot path, this depends on where the next dependency is actually saved (
46+
* https://github.com/vercel/next.js/blob/39e06c75/packages/next/src/build/webpack-config.ts#L103-L104
47+
* ) and can depend on the package manager used, if it is using workspaces, etc...
48+
*
49+
* This function checks the two potential paths for the dist directory and returns the first that it finds,
50+
* it throws an error if it can't find either
51+
*
52+
* @param config
53+
* @returns the node_modules/next/dist directory path
54+
*/
55+
function getDistPath(config: Config): string {
56+
for (const root of [config.paths.standaloneApp, config.paths.standaloneRoot]) {
57+
try {
58+
const distPath = path.join(root, "node_modules", "next", "dist");
59+
if (fs.statSync(distPath).isDirectory()) return distPath;
60+
} catch {
61+
/* empty */
62+
}
63+
}
64+
65+
throw new Error("Unexpected error: unable to detect the node_modules/next/dist directory");
66+
}

packages/cloudflare/src/cli/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export type Config = {
1818
builderOutput: string;
1919
// Path to the app's `.next` directory (where `next build` saves the build output)
2020
dotNext: string;
21+
// Path to the application standalone root directory
22+
standaloneRoot: string;
2123
// Path to the application standalone directory (where `next build` saves the standalone app)
2224
standaloneApp: string;
2325
// Path to the `.next` directory specific to the standalone application
@@ -47,7 +49,8 @@ export type Config = {
4749
export function getConfig(appDir: string, outputDir: string): Config {
4850
const dotNext = path.join(outputDir, ".next");
4951
const appPath = getNextjsApplicationPath(dotNext).replace(/\/$/, "");
50-
const standaloneApp = path.join(dotNext, "standalone", appPath);
52+
const standaloneRoot = path.join(dotNext, "standalone");
53+
const standaloneApp = path.join(standaloneRoot, appPath);
5154
const standaloneAppDotNext = path.join(standaloneApp, ".next");
5255
const standaloneAppServer = path.join(standaloneAppDotNext, "server");
5356

@@ -59,6 +62,7 @@ export function getConfig(appDir: string, outputDir: string): Config {
5962
nextApp: appDir,
6063
builderOutput: outputDir,
6164
dotNext,
65+
standaloneRoot,
6266
standaloneApp,
6367
standaloneAppDotNext,
6468
standaloneAppServer,

pnpm-lock.yaml

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)