this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Remove the need for aliases in wrangler.toml

+73 -25
-10
TODO.txt
··· 46 46 47 47 # Use the new Workers + Assets to host the static frontend files 48 48 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" 59 49 ``` 60 50 61 51 - Build the builder
+5 -9
builder/src/build/build-worker/index.ts
··· 11 11 import { patchFindDir } from "./patches/to-investigate/patchFindDir"; 12 12 import { inlineNextRequire } from "./patches/to-investigate/inlineNextRequire"; 13 13 import { inlineEvalManifest } from "./patches/to-investigate/inlineEvalManifest"; 14 + import { patchWranglerDeps } from "./patches/to-investigate/wranglerDeps"; 14 15 15 16 /** 16 17 * Using the Next.js build output in the `.next` directory builds a workerd compatible output ··· 33 34 )?.[1] ?? {}; 34 35 35 36 console.log(`\x1b[35m⚙️ Bundling the worker file...\n\x1b[0m`); 37 + 38 + patchWranglerDeps(nextjsAppPaths); 39 + updateWebpackChunksFile(nextjsAppPaths); 40 + 36 41 await build({ 37 42 entryPoints: [workerEntrypoint], 38 43 bundle: true, ··· 51 56 // which comes from https://github.com/vercel/edge-runtime/blob/6e96b55f/packages/primitives/src/primitives/load.js#L57-L63 52 57 // QUESTION: Why did I encountered this but mhart didn't? 53 58 "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`, 61 59 // `@next/env` is a library Next.js uses for loading dotenv files, for obvious reasons we need to stub it here 62 60 // source: https://github.com/vercel/next.js/tree/0ac10d79720/packages/next-env 63 61 "@next/env": `${templateDir}/shims/env.ts`, ··· 97 95 }); 98 96 99 97 await updateWorkerBundledCode(workerOutputFile, nextjsAppPaths); 100 - 101 - updateWebpackChunksFile(nextjsAppPaths); 102 98 103 99 console.log(`\x1b[35m⚙️ Copying asset files...\n\x1b[0m`); 104 100 await cp(
+2 -2
builder/src/build/build-worker/patches/investigated/copyTemplates.ts
··· 1 1 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"; 4 4 5 5 /** 6 6 * Copy templates in the standalone folder.
+1 -1
builder/src/build/build-worker/patches/to-investigate/inlineEvalManifest.ts
··· 1 1 import { globSync } from "glob"; 2 - import { NextjsAppPaths } from "builder/src/nextjsPaths"; 2 + import { NextjsAppPaths } from "../../../../nextjsPaths"; 3 3 4 4 /** 5 5 * `evalManifest` relies on readFileSync so we need to patch the function so that it instead returns the content of the manifest files
+1 -1
builder/src/build/build-worker/patches/to-investigate/inlineNextRequire.ts
··· 1 1 import { readFileSync, existsSync } from "node:fs"; 2 - import { NextjsAppPaths } from "builder/src/nextjsPaths"; 2 + import { NextjsAppPaths } from "../../../../nextjsPaths"; 3 3 4 4 /** 5 5 * The following avoid various Next.js specific files `require`d at runtime since we can just read
+1 -1
builder/src/build/build-worker/patches/to-investigate/patchFindDir.ts
··· 1 - import { NextjsAppPaths } from "builder/src/nextjsPaths"; 1 + import { NextjsAppPaths } from "../../../../nextjsPaths"; 2 2 import { existsSync } from "node:fs"; 3 3 4 4 /**
+1 -1
builder/src/build/build-worker/patches/to-investigate/patchReadFile.ts
··· 1 1 import { readFileSync } from "node:fs"; 2 2 import { globSync } from "glob"; 3 - import { NextjsAppPaths } from "builder/src/nextjsPaths"; 3 + import { NextjsAppPaths } from "../../../../nextjsPaths"; 4 4 5 5 export function patchReadFile( 6 6 code: string,
+62
builder/src/build/build-worker/patches/to-investigate/wranglerDeps.ts
··· 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 + }