Skip to content

Commit 72e9a7b

Browse files
committed
Remove the need for aliases in wrangler.toml
1 parent 01565e0 commit 72e9a7b

File tree

8 files changed

+73
-25
lines changed

8 files changed

+73
-25
lines changed

TODO.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ minify = false
4646

4747
# Use the new Workers + Assets to host the static frontend files
4848
experimental_assets = { directory = ".worker-next/assets", binding = "ASSETS" }
49-
50-
# The aliases below should not be needed (we don't want users to have to define the aliases themselves)
51-
[alias]
52-
# critters is `require`d from `pages.runtime.prod.js` when running wrangler dev, so we need to stub it out
53-
"critters" = "./.next/standalone/node_modules/cf/templates/shims/empty.ts"
54-
# @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out
55-
# IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
56-
# try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
57-
# causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
58-
"@opentelemetry/api" = "./.next/standalone/node_modules/cf/templates/shims/throw.ts"
5949
```
6050

6151
- Build the builder

builder/src/build/build-worker/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { patchReadFile } from "./patches/to-investigate/patchReadFile";
1111
import { patchFindDir } from "./patches/to-investigate/patchFindDir";
1212
import { inlineNextRequire } from "./patches/to-investigate/inlineNextRequire";
1313
import { inlineEvalManifest } from "./patches/to-investigate/inlineEvalManifest";
14+
import { patchWranglerDeps } from "./patches/to-investigate/wranglerDeps";
1415

1516
/**
1617
* Using the Next.js build output in the `.next` directory builds a workerd compatible output
@@ -33,6 +34,10 @@ export async function buildWorker(
3334
)?.[1] ?? {};
3435

3536
console.log(`\x1b[35m⚙️ Bundling the worker file...\n\x1b[0m`);
37+
38+
patchWranglerDeps(nextjsAppPaths);
39+
updateWebpackChunksFile(nextjsAppPaths);
40+
3641
await build({
3742
entryPoints: [workerEntrypoint],
3843
bundle: true,
@@ -51,13 +56,6 @@ export async function buildWorker(
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`,
54-
// Note: we need to stub out `@opentelemetry/api` as that is problematic and doesn't get properly bundled...
55-
critters: `${templateDir}/shims/empty.ts`,
56-
// Note: we need to stub out `@opentelemetry/api` as it is problematic
57-
// IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
58-
// try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
59-
// causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
60-
"@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`,
@@ -98,8 +96,6 @@ export async function buildWorker(
9896

9997
await updateWorkerBundledCode(workerOutputFile, nextjsAppPaths);
10098

101-
updateWebpackChunksFile(nextjsAppPaths);
102-
10399
console.log(`\x1b[35m⚙️ Copying asset files...\n\x1b[0m`);
104100
await cp(
105101
`${nextjsAppPaths.dotNextDir}/static`,

builder/src/build/build-worker/patches/investigated/copyTemplates.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "node:path";
2-
import { NextjsAppPaths } from "builder/src/nextjsPaths";
3-
import { cpSync, mkdirSync } from "node:fs";
2+
import { NextjsAppPaths } from "../../../../nextjsPaths";
3+
import { cpSync } from "node:fs";
44

55
/**
66
* Copy templates in the standalone folder.

builder/src/build/build-worker/patches/to-investigate/inlineEvalManifest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { globSync } from "glob";
2-
import { NextjsAppPaths } from "builder/src/nextjsPaths";
2+
import { NextjsAppPaths } from "../../../../nextjsPaths";
33

44
/**
55
* `evalManifest` relies on readFileSync so we need to patch the function so that it instead returns the content of the manifest files

builder/src/build/build-worker/patches/to-investigate/inlineNextRequire.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { readFileSync, existsSync } from "node:fs";
2-
import { NextjsAppPaths } from "builder/src/nextjsPaths";
2+
import { NextjsAppPaths } from "../../../../nextjsPaths";
33

44
/**
55
* The following avoid various Next.js specific files `require`d at runtime since we can just read

builder/src/build/build-worker/patches/to-investigate/patchFindDir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NextjsAppPaths } from "builder/src/nextjsPaths";
1+
import { NextjsAppPaths } from "../../../../nextjsPaths";
22
import { existsSync } from "node:fs";
33

44
/**

builder/src/build/build-worker/patches/to-investigate/patchReadFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync } from "node:fs";
22
import { globSync } from "glob";
3-
import { NextjsAppPaths } from "builder/src/nextjsPaths";
3+
import { NextjsAppPaths } from "../../../../nextjsPaths";
44

55
export function patchReadFile(
66
code: string,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import path from "node:path";
2+
import fs, { writeFileSync } from "node:fs";
3+
import { NextjsAppPaths } from "../../../../nextjsPaths";
4+
5+
export function patchWranglerDeps(paths: NextjsAppPaths) {
6+
console.log("# patchWranglerDeps");
7+
8+
console.log({ base: paths.standaloneAppDotNextDir });
9+
10+
// Patch .next/standalone/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js
11+
//
12+
// Remove the need for an alias in wrangler.toml:
13+
//
14+
// [alias]
15+
// # critters is `require`d from `pages.runtime.prod.js` when running wrangler dev, so we need to stub it out
16+
// "critters" = "./.next/standalone/node_modules/cf/templates/shims/empty.ts"
17+
const pagesRuntimeFile = path.join(
18+
paths.standaloneAppDir,
19+
"node_modules",
20+
"next",
21+
"dist",
22+
"compiled",
23+
"next-server",
24+
"pages.runtime.prod.js"
25+
);
26+
27+
const patchedPagesRuntime = fs
28+
.readFileSync(pagesRuntimeFile, "utf-8")
29+
.replace(`e.exports=require("critters")`, `e.exports={}`);
30+
31+
fs.writeFileSync(pagesRuntimeFile, patchedPagesRuntime);
32+
33+
// Patch .next/standalone/node_modules/next/dist/server/lib/trace/tracer.js
34+
//
35+
// Remove the need for an alias in wrangler.toml:
36+
//
37+
// [alias]
38+
// # @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out
39+
// # IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
40+
// # try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
41+
// # causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
42+
// #"@opentelemetry/api" = "./.next/standalone/node_modules/cf/templates/shims/throw.ts"
43+
const tracerFile = path.join(
44+
paths.standaloneAppDir,
45+
"node_modules",
46+
"next",
47+
"dist",
48+
"server",
49+
"lib",
50+
"trace",
51+
"tracer.js"
52+
);
53+
54+
const pacthedTracer = fs
55+
.readFileSync(tracerFile, "utf-8")
56+
.replaceAll(
57+
/\w+\s*=\s*require\([^/]*opentelemetry.*\)/g,
58+
`throw new Error("@opentelemetry/api")`
59+
);
60+
61+
writeFileSync(tracerFile, pacthedTracer);
62+
}

0 commit comments

Comments
 (0)