@@ -5,22 +5,15 @@ import path from "node:path";
5
5
export function patchWranglerDeps ( config : Config ) {
6
6
console . log ( "# patchWranglerDeps" ) ;
7
7
8
+ const distPath = getDistPath ( config ) ;
8
9
// Patch .next/standalone/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js
9
10
//
10
11
// Remove the need for an alias in wrangler.toml:
11
12
//
12
13
// [alias]
13
14
// # critters is `require`d from `pages.runtime.prod.js` when running wrangler dev, so we need to stub it out
14
15
// "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" ) ;
24
17
25
18
const patchedPagesRuntime = fs
26
19
. readFileSync ( pagesRuntimeFile , "utf-8" )
@@ -38,20 +31,36 @@ export function patchWranglerDeps(config: Config) {
38
31
// # try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
39
32
// # causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
40
33
// #"@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" ) ;
51
35
52
36
const pacthedTracer = fs
53
37
. readFileSync ( tracerFile , "utf-8" )
54
38
. replaceAll ( / \w + \s * = \s * r e q u i r e \( [ ^ / ] * o p e n t e l e m e t r y .* \) / g, `throw new Error("@opentelemetry/api")` ) ;
55
39
56
40
writeFileSync ( tracerFile , pacthedTracer ) ;
57
41
}
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
+ }
0 commit comments