diff --git a/TODO.txt b/TODO.txt index 56a2232fc..a69eab6ad 100644 --- a/TODO.txt +++ b/TODO.txt @@ -46,16 +46,6 @@ minify = false # Use the new Workers + Assets to host the static frontend files experimental_assets = { directory = ".worker-next/assets", binding = "ASSETS" } - -# The aliases below should not be needed (we don't want users to have to define the aliases themselves) -[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" -# @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out -# IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the -# 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" ``` - Build the builder diff --git a/builder/src/build/build-worker/index.ts b/builder/src/build/build-worker/index.ts index e7668ef62..0e6187af3 100644 --- a/builder/src/build/build-worker/index.ts +++ b/builder/src/build/build-worker/index.ts @@ -11,6 +11,7 @@ import { patchReadFile } from "./patches/to-investigate/patchReadFile"; import { patchFindDir } from "./patches/to-investigate/patchFindDir"; import { inlineNextRequire } from "./patches/to-investigate/inlineNextRequire"; import { inlineEvalManifest } from "./patches/to-investigate/inlineEvalManifest"; +import { patchWranglerDeps } from "./patches/to-investigate/wranglerDeps"; /** * Using the Next.js build output in the `.next` directory builds a workerd compatible output @@ -33,6 +34,10 @@ export async function buildWorker( )?.[1] ?? {}; console.log(`\x1b[35m⚙️ Bundling the worker file...\n\x1b[0m`); + + patchWranglerDeps(nextjsAppPaths); + updateWebpackChunksFile(nextjsAppPaths); + await build({ entryPoints: [workerEntrypoint], bundle: true, @@ -51,13 +56,6 @@ export async function buildWorker( // which comes from https://github.com/vercel/edge-runtime/blob/6e96b55f/packages/primitives/src/primitives/load.js#L57-L63 // QUESTION: Why did I encountered this but mhart didn't? "next/dist/compiled/edge-runtime": `${templateDir}/shims/empty.ts`, - // Note: we need to stub out `@opentelemetry/api` as that is problematic and doesn't get properly bundled... - critters: `${templateDir}/shims/empty.ts`, - // Note: we need to stub out `@opentelemetry/api` as it is problematic - // IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the - // 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": `${templateDir}/shims/throw.ts`, // `@next/env` is a library Next.js uses for loading dotenv files, for obvious reasons we need to stub it here // source: https://github.com/vercel/next.js/tree/0ac10d79720/packages/next-env "@next/env": `${templateDir}/shims/env.ts`, @@ -98,8 +96,6 @@ export async function buildWorker( await updateWorkerBundledCode(workerOutputFile, nextjsAppPaths); - updateWebpackChunksFile(nextjsAppPaths); - console.log(`\x1b[35m⚙️ Copying asset files...\n\x1b[0m`); await cp( `${nextjsAppPaths.dotNextDir}/static`, diff --git a/builder/src/build/build-worker/patches/investigated/copyTemplates.ts b/builder/src/build/build-worker/patches/investigated/copyTemplates.ts index ebbe850ce..73ce99036 100644 --- a/builder/src/build/build-worker/patches/investigated/copyTemplates.ts +++ b/builder/src/build/build-worker/patches/investigated/copyTemplates.ts @@ -1,6 +1,6 @@ import path from "node:path"; -import { NextjsAppPaths } from "builder/src/nextjsPaths"; -import { cpSync, mkdirSync } from "node:fs"; +import { NextjsAppPaths } from "../../../../nextjsPaths"; +import { cpSync } from "node:fs"; /** * Copy templates in the standalone folder. diff --git a/builder/src/build/build-worker/patches/to-investigate/inlineEvalManifest.ts b/builder/src/build/build-worker/patches/to-investigate/inlineEvalManifest.ts index 2ac37ac7b..c56bb25fb 100644 --- a/builder/src/build/build-worker/patches/to-investigate/inlineEvalManifest.ts +++ b/builder/src/build/build-worker/patches/to-investigate/inlineEvalManifest.ts @@ -1,5 +1,5 @@ import { globSync } from "glob"; -import { NextjsAppPaths } from "builder/src/nextjsPaths"; +import { NextjsAppPaths } from "../../../../nextjsPaths"; /** * `evalManifest` relies on readFileSync so we need to patch the function so that it instead returns the content of the manifest files diff --git a/builder/src/build/build-worker/patches/to-investigate/inlineNextRequire.ts b/builder/src/build/build-worker/patches/to-investigate/inlineNextRequire.ts index 90e877326..135947a3c 100644 --- a/builder/src/build/build-worker/patches/to-investigate/inlineNextRequire.ts +++ b/builder/src/build/build-worker/patches/to-investigate/inlineNextRequire.ts @@ -1,5 +1,5 @@ import { readFileSync, existsSync } from "node:fs"; -import { NextjsAppPaths } from "builder/src/nextjsPaths"; +import { NextjsAppPaths } from "../../../../nextjsPaths"; /** * The following avoid various Next.js specific files `require`d at runtime since we can just read diff --git a/builder/src/build/build-worker/patches/to-investigate/patchFindDir.ts b/builder/src/build/build-worker/patches/to-investigate/patchFindDir.ts index 6a8030d29..2ce88ad7d 100644 --- a/builder/src/build/build-worker/patches/to-investigate/patchFindDir.ts +++ b/builder/src/build/build-worker/patches/to-investigate/patchFindDir.ts @@ -1,4 +1,4 @@ -import { NextjsAppPaths } from "builder/src/nextjsPaths"; +import { NextjsAppPaths } from "../../../../nextjsPaths"; import { existsSync } from "node:fs"; /** diff --git a/builder/src/build/build-worker/patches/to-investigate/patchReadFile.ts b/builder/src/build/build-worker/patches/to-investigate/patchReadFile.ts index e7efdfeab..cb2fdce87 100644 --- a/builder/src/build/build-worker/patches/to-investigate/patchReadFile.ts +++ b/builder/src/build/build-worker/patches/to-investigate/patchReadFile.ts @@ -1,6 +1,6 @@ import { readFileSync } from "node:fs"; import { globSync } from "glob"; -import { NextjsAppPaths } from "builder/src/nextjsPaths"; +import { NextjsAppPaths } from "../../../../nextjsPaths"; export function patchReadFile( code: string, diff --git a/builder/src/build/build-worker/patches/to-investigate/wranglerDeps.ts b/builder/src/build/build-worker/patches/to-investigate/wranglerDeps.ts new file mode 100644 index 000000000..0fd554fff --- /dev/null +++ b/builder/src/build/build-worker/patches/to-investigate/wranglerDeps.ts @@ -0,0 +1,62 @@ +import path from "node:path"; +import fs, { writeFileSync } from "node:fs"; +import { NextjsAppPaths } from "../../../../nextjsPaths"; + +export function patchWranglerDeps(paths: NextjsAppPaths) { + console.log("# patchWranglerDeps"); + + console.log({ base: paths.standaloneAppDotNextDir }); + + // 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( + paths.standaloneAppDir, + "node_modules", + "next", + "dist", + "compiled", + "next-server", + "pages.runtime.prod.js" + ); + + const patchedPagesRuntime = fs + .readFileSync(pagesRuntimeFile, "utf-8") + .replace(`e.exports=require("critters")`, `e.exports={}`); + + fs.writeFileSync(pagesRuntimeFile, patchedPagesRuntime); + + // Patch .next/standalone/node_modules/next/dist/server/lib/trace/tracer.js + // + // Remove the need for an alias in wrangler.toml: + // + // [alias] + // # @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out + // # IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the + // # 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( + paths.standaloneAppDir, + "node_modules", + "next", + "dist", + "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); +} diff --git a/next/api/package.json b/next/api/package.json index fb847bfef..c9c6dacf1 100644 --- a/next/api/package.json +++ b/next/api/package.json @@ -22,6 +22,6 @@ "@playwright/test": "^1.46.1", "@types/node": "^22.2.0", "node-url": "npm:url@^0.11.4", - "wrangler": "3.76.0" + "wrangler": "3.77.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index aaeb6b200..a71250603 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -58,8 +58,8 @@ importers: specifier: npm:url@^0.11.4 version: url@0.11.4 wrangler: - specifier: 3.76.0 - version: 3.76.0 + specifier: 3.77.0 + version: 3.77.0 packages: @@ -67,38 +67,38 @@ packages: resolution: {integrity: sha512-YLPHc8yASwjNkmcDMQMY35yiWjoKAKnhUbPRszBRS0YgH+IXtsMp61j+yTcnCE3oO2DgP0U3iejLC8FTtKDC8Q==} engines: {node: '>=16.13'} - '@cloudflare/workerd-darwin-64@1.20240821.1': - resolution: {integrity: sha512-CDBpfZKrSy4YrIdqS84z67r3Tzal2pOhjCsIb63IuCnvVes59/ft1qhczBzk9EffeOE2iTCrA4YBT7Sbn7USew==} + '@cloudflare/workerd-darwin-64@1.20240909.0': + resolution: {integrity: sha512-nJ8jm/6PR8DPzVb4QifNAfSdrFZXNblwIdOhLTU5FpSvFFocmzFX5WgzQagvtmcC9/ZAQyxuf7WynDNyBcoe0Q==} engines: {node: '>=16'} cpu: [x64] os: [darwin] - '@cloudflare/workerd-darwin-arm64@1.20240821.1': - resolution: {integrity: sha512-Q+9RedvNbPcEt/dKni1oN94OxbvuNAeJkgHmrLFTGF8zu21wzOhVkQeRNxcYxrMa9mfStc457NAg13OVCj2kHQ==} + '@cloudflare/workerd-darwin-arm64@1.20240909.0': + resolution: {integrity: sha512-gJqKa811oSsoxy9xuoQn7bS0Hr1sY+o3EUORTcEnulG6Kz9NQ6nd8QNdp2Hrk2jmmSqwrNkn+a6PZkWzk6Q0Gw==} engines: {node: '>=16'} cpu: [arm64] os: [darwin] - '@cloudflare/workerd-linux-64@1.20240821.1': - resolution: {integrity: sha512-j6z3KsPtawrscoLuP985LbqFrmsJL6q1mvSXOXTqXGODAHIzGBipHARdOjms3UQqovzvqB2lQaQsZtLBwCZxtA==} + '@cloudflare/workerd-linux-64@1.20240909.0': + resolution: {integrity: sha512-sJrmtccfMg73sZljiBpe4R+lhF58TqzqhF2pQG8HRjyxkzkM1sjpZqfEFaIkNUDqd3/Ibji49fklhPCGXljKSg==} engines: {node: '>=16'} cpu: [x64] os: [linux] - '@cloudflare/workerd-linux-arm64@1.20240821.1': - resolution: {integrity: sha512-I9bHgZOxJQW0CV5gTdilyxzTG7ILzbTirehQWgfPx9X77E/7eIbR9sboOMgyeC69W4he0SKtpx0sYZuTJu4ERw==} + '@cloudflare/workerd-linux-arm64@1.20240909.0': + resolution: {integrity: sha512-dTbSdceyRXPOSER+18AwYRbPQG0e/Dwl2trmfMMCETkfJhNLv1fU3FFMJPjfILijKnhTZHSnHCx0+xwHdon2fg==} engines: {node: '>=16'} cpu: [arm64] os: [linux] - '@cloudflare/workerd-windows-64@1.20240821.1': - resolution: {integrity: sha512-keC97QPArs6LWbPejQM7/Y8Jy8QqyaZow4/ZdsGo+QjlOLiZRDpAenfZx3CBUoWwEeFwQTl2FLO+8hV1SWFFYw==} + '@cloudflare/workerd-windows-64@1.20240909.0': + resolution: {integrity: sha512-/d4BT0kcWFa7Qc0K4K9+cwVQ1qyPNKiO42JZUijlDlco+TYTPkLO3qGEohmwbfMq+BieK7JTMSgjO81ZHpA0HQ==} engines: {node: '>=16'} cpu: [x64] os: [win32] - '@cloudflare/workers-shared@0.4.1': - resolution: {integrity: sha512-nYh4r8JwOOjYIdH2zub++CmIKlkYFlpxI1nBHimoiHcytJXD/b7ldJ21TtfzUZMCgI78mxVlymMHA/ReaOxKlA==} + '@cloudflare/workers-shared@0.5.0': + resolution: {integrity: sha512-qLJzGKwl2SiaaieoCFdj2q85hthPgmF5KjRaZzsPdMKtP+PQnzSWfARsd5mZh71ut5/0grYkUCYWedFe4bY++g==} engines: {node: '>=16.7.0'} '@cspotcode/source-map-support@0.8.1': @@ -955,8 +955,8 @@ packages: resolution: {integrity: sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==} engines: {node: '>=6'} - miniflare@3.20240821.2: - resolution: {integrity: sha512-mgWekp437zD5l2Rz/in/OGBAISNB3rWr69DhR5Iq3WoToUNeAnkbW/CWPBpJiw5WHzZfHHOT+sVjSksAGRJitg==} + miniflare@3.20240909.0: + resolution: {integrity: sha512-20eJCCToBvxohLW0vWZHPRL6JPbqJQ4nkCK6MelgTRkw4CLd7MUYbY70M8olJjTuBDK/84/1CkNC4Q/OrbHIDA==} engines: {node: '>=16.13'} hasBin: true @@ -1359,17 +1359,17 @@ packages: engines: {node: '>= 8'} hasBin: true - workerd@1.20240821.1: - resolution: {integrity: sha512-y4phjCnEG96u8ZkgkkHB+gSw0i6uMNo23rBmixylWpjxDklB+LWD8dztasvsu7xGaZbLoTxQESdEw956F7VJDA==} + workerd@1.20240909.0: + resolution: {integrity: sha512-NwuYh/Fgr/MK0H+Ht687sHl/f8tumwT5CWzYR0MZMHri8m3CIYu2IaY4tBFWoKE/tOU1Z5XjEXECa9zXY4+lwg==} engines: {node: '>=16'} hasBin: true - wrangler@3.76.0: - resolution: {integrity: sha512-HQWJm5/RUHVr+Vg2KM55pjDSbcEh5WxR6Swcpz1jQ5o+ytoLoj31IHMsl4cJFfM/JtzjBXSpRbS00lDnGfFc2A==} + wrangler@3.77.0: + resolution: {integrity: sha512-XdzStAdB6c87cc9CJRinMzAAFqqSZ19pQ+m6Gzx5fHeWXSclrOFsfD2iSmil8C3/LsrtwoIXghibK98FN0HhhQ==} engines: {node: '>=16.17.0'} hasBin: true peerDependencies: - '@cloudflare/workers-types': ^4.20240821.1 + '@cloudflare/workers-types': ^4.20240909.0 peerDependenciesMeta: '@cloudflare/workers-types': optional: true @@ -1409,22 +1409,25 @@ snapshots: dependencies: mime: 3.0.0 - '@cloudflare/workerd-darwin-64@1.20240821.1': + '@cloudflare/workerd-darwin-64@1.20240909.0': optional: true - '@cloudflare/workerd-darwin-arm64@1.20240821.1': + '@cloudflare/workerd-darwin-arm64@1.20240909.0': optional: true - '@cloudflare/workerd-linux-64@1.20240821.1': + '@cloudflare/workerd-linux-64@1.20240909.0': optional: true - '@cloudflare/workerd-linux-arm64@1.20240821.1': + '@cloudflare/workerd-linux-arm64@1.20240909.0': optional: true - '@cloudflare/workerd-windows-64@1.20240821.1': + '@cloudflare/workerd-windows-64@1.20240909.0': optional: true - '@cloudflare/workers-shared@0.4.1': {} + '@cloudflare/workers-shared@0.5.0': + dependencies: + mime: 3.0.0 + zod: 3.23.8 '@cspotcode/source-map-support@0.8.1': dependencies: @@ -2104,7 +2107,7 @@ snapshots: mimic-fn@2.1.0: {} - miniflare@3.20240821.2: + miniflare@3.20240909.0: dependencies: '@cspotcode/source-map-support': 0.8.1 acorn: 8.12.1 @@ -2114,7 +2117,7 @@ snapshots: glob-to-regexp: 0.4.1 stoppable: 1.1.0 undici: 5.28.4 - workerd: 1.20240821.1 + workerd: 1.20240909.0 ws: 8.18.0 youch: 3.3.3 zod: 3.23.8 @@ -2507,25 +2510,25 @@ snapshots: dependencies: isexe: 2.0.0 - workerd@1.20240821.1: + workerd@1.20240909.0: optionalDependencies: - '@cloudflare/workerd-darwin-64': 1.20240821.1 - '@cloudflare/workerd-darwin-arm64': 1.20240821.1 - '@cloudflare/workerd-linux-64': 1.20240821.1 - '@cloudflare/workerd-linux-arm64': 1.20240821.1 - '@cloudflare/workerd-windows-64': 1.20240821.1 + '@cloudflare/workerd-darwin-64': 1.20240909.0 + '@cloudflare/workerd-darwin-arm64': 1.20240909.0 + '@cloudflare/workerd-linux-64': 1.20240909.0 + '@cloudflare/workerd-linux-arm64': 1.20240909.0 + '@cloudflare/workerd-windows-64': 1.20240909.0 - wrangler@3.76.0: + wrangler@3.77.0: dependencies: '@cloudflare/kv-asset-handler': 0.3.4 - '@cloudflare/workers-shared': 0.4.1 + '@cloudflare/workers-shared': 0.5.0 '@esbuild-plugins/node-globals-polyfill': 0.2.3(esbuild@0.17.19) '@esbuild-plugins/node-modules-polyfill': 0.2.2(esbuild@0.17.19) blake3-wasm: 2.1.5 chokidar: 3.6.0 date-fns: 3.6.0 esbuild: 0.17.19 - miniflare: 3.20240821.2 + miniflare: 3.20240909.0 nanoid: 3.3.7 path-to-regexp: 6.2.2 resolve: 1.22.8 @@ -2533,7 +2536,7 @@ snapshots: selfsigned: 2.4.1 source-map: 0.6.1 unenv: unenv-nightly@2.0.0-1724863496.70db6f1 - workerd: 1.20240821.1 + workerd: 1.20240909.0 xxhash-wasm: 1.0.2 optionalDependencies: fsevents: 2.3.3