···46464747# Use the new Workers + Assets to host the static frontend files
4848experimental_assets = { directory = ".worker-next/assets", binding = "ASSETS" }
4949-5050-# The aliases below should not be needed (we don't want users to have to define the aliases themselves)
5151-[alias]
5252-# critters is `require`d from `pages.runtime.prod.js` when running wrangler dev, so we need to stub it out
5353-"critters" = "./.next/standalone/node_modules/cf/templates/shims/empty.ts"
5454-# @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out
5555-# IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
5656-# try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
5757-# causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
5858-"@opentelemetry/api" = "./.next/standalone/node_modules/cf/templates/shims/throw.ts"
5949```
60506151- Build the builder
+5-9
builder/src/build/build-worker/index.ts
···1111import { patchFindDir } from "./patches/to-investigate/patchFindDir";
1212import { inlineNextRequire } from "./patches/to-investigate/inlineNextRequire";
1313import { inlineEvalManifest } from "./patches/to-investigate/inlineEvalManifest";
1414+import { patchWranglerDeps } from "./patches/to-investigate/wranglerDeps";
14151516/**
1617 * Using the Next.js build output in the `.next` directory builds a workerd compatible output
···3334 )?.[1] ?? {};
34353536 console.log(`\x1b[35m⚙️ Bundling the worker file...\n\x1b[0m`);
3737+3838+ patchWranglerDeps(nextjsAppPaths);
3939+ updateWebpackChunksFile(nextjsAppPaths);
4040+3641 await build({
3742 entryPoints: [workerEntrypoint],
3843 bundle: true,
···5156 // which comes from https://github.com/vercel/edge-runtime/blob/6e96b55f/packages/primitives/src/primitives/load.js#L57-L63
5257 // QUESTION: Why did I encountered this but mhart didn't?
5358 "next/dist/compiled/edge-runtime": `${templateDir}/shims/empty.ts`,
5454- // Note: we need to stub out `@opentelemetry/api` as that is problematic and doesn't get properly bundled...
5555- critters: `${templateDir}/shims/empty.ts`,
5656- // Note: we need to stub out `@opentelemetry/api` as it is problematic
5757- // IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
5858- // try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
5959- // causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
6060- "@opentelemetry/api": `${templateDir}/shims/throw.ts`,
6159 // `@next/env` is a library Next.js uses for loading dotenv files, for obvious reasons we need to stub it here
6260 // source: https://github.com/vercel/next.js/tree/0ac10d79720/packages/next-env
6361 "@next/env": `${templateDir}/shims/env.ts`,
···9795 });
98969997 await updateWorkerBundledCode(workerOutputFile, nextjsAppPaths);
100100-101101- updateWebpackChunksFile(nextjsAppPaths);
1029810399 console.log(`\x1b[35m⚙️ Copying asset files...\n\x1b[0m`);
104100 await cp(
···11import { globSync } from "glob";
22-import { NextjsAppPaths } from "builder/src/nextjsPaths";
22+import { NextjsAppPaths } from "../../../../nextjsPaths";
3344/**
55 * `evalManifest` relies on readFileSync so we need to patch the function so that it instead returns the content of the manifest files
···11import { readFileSync, existsSync } from "node:fs";
22-import { NextjsAppPaths } from "builder/src/nextjsPaths";
22+import { NextjsAppPaths } from "../../../../nextjsPaths";
3344/**
55 * The following avoid various Next.js specific files `require`d at runtime since we can just read
···11+import path from "node:path";
22+import fs, { writeFileSync } from "node:fs";
33+import { NextjsAppPaths } from "../../../../nextjsPaths";
44+55+export function patchWranglerDeps(paths: NextjsAppPaths) {
66+ console.log("# patchWranglerDeps");
77+88+ console.log({ base: paths.standaloneAppDotNextDir });
99+1010+ // Patch .next/standalone/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js
1111+ //
1212+ // Remove the need for an alias in wrangler.toml:
1313+ //
1414+ // [alias]
1515+ // # critters is `require`d from `pages.runtime.prod.js` when running wrangler dev, so we need to stub it out
1616+ // "critters" = "./.next/standalone/node_modules/cf/templates/shims/empty.ts"
1717+ const pagesRuntimeFile = path.join(
1818+ paths.standaloneAppDir,
1919+ "node_modules",
2020+ "next",
2121+ "dist",
2222+ "compiled",
2323+ "next-server",
2424+ "pages.runtime.prod.js"
2525+ );
2626+2727+ const patchedPagesRuntime = fs
2828+ .readFileSync(pagesRuntimeFile, "utf-8")
2929+ .replace(`e.exports=require("critters")`, `e.exports={}`);
3030+3131+ fs.writeFileSync(pagesRuntimeFile, patchedPagesRuntime);
3232+3333+ // Patch .next/standalone/node_modules/next/dist/server/lib/trace/tracer.js
3434+ //
3535+ // Remove the need for an alias in wrangler.toml:
3636+ //
3737+ // [alias]
3838+ // # @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out
3939+ // # IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
4040+ // # try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
4141+ // # causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
4242+ // #"@opentelemetry/api" = "./.next/standalone/node_modules/cf/templates/shims/throw.ts"
4343+ const tracerFile = path.join(
4444+ paths.standaloneAppDir,
4545+ "node_modules",
4646+ "next",
4747+ "dist",
4848+ "server",
4949+ "lib",
5050+ "trace",
5151+ "tracer.js"
5252+ );
5353+5454+ const pacthedTracer = fs
5555+ .readFileSync(tracerFile, "utf-8")
5656+ .replaceAll(
5757+ /\w+\s*=\s*require\([^/]*opentelemetry.*\)/g,
5858+ `throw new Error("@opentelemetry/api")`
5959+ );
6060+6161+ writeFileSync(tracerFile, pacthedTracer);
6262+}