import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js"; /** * Replace Turbopack's `loadWebAssemblyModule` with a call to our generated `loadWasmChunk`. * * The original implementation uses `WebAssembly.compileStreaming`, which is not available * in workerd. `loadWasmChunk` resolves the chunk via a static `import()` switch so the * bundler can statically discover and bundle each `.wasm` chunk. */ export declare const replaceLoadWebAssemblyModuleRule = "\nrule:\n kind: function_declaration\n has:\n field: name\n regex: \"^loadWebAssemblyModule$\"\nfix: |-\n function loadWebAssemblyModule(chunkPath, _edgeModule) {\n return loadWasmChunk(chunkPath);\n }\n"; /** * Replace Turbopack's `loadWebAssembly` with a synchronous-instantiation variant. * * The original implementation uses `WebAssembly.instantiateStreaming`, which is not * available in workerd. We load the compiled module via `loadWasmChunk` and then call * the synchronous `WebAssembly.instantiate` to produce the instance's exports. */ export declare const replaceLoadWebAssemblyRule = "\nrule:\n kind: function_declaration\n has:\n field: name\n regex: \"^loadWebAssembly$\"\nfix: |-\n async function loadWebAssembly(chunkPath, _edgeModule, imports) {\n const mod = await loadWasmChunk(chunkPath);\n const { exports } = await WebAssembly.instantiate(mod, imports);\n return exports;\n }\n"; /** * Replace the `compileModule` helper that Next.js 16.3 emits in the chunks. * * Next.js 16.3 moved the wasm loading out of `[turbopack]_runtime.js`: it is now emitted on * demand in the chunks as `[turbopack-wasm]/node/loadWasm.ts`, which reads the `.wasm` file from * the filesystem and compiles it with `WebAssembly.compileStreaming`. Neither is supported by * workerd so the helper is rewritten to use the generated `loadWasmChunk`. * * The emitted code is minified, so the rule matches on the shape of the helper rather than on its * name: an async function taking the chunk path as its only parameter and returning the result of * `WebAssembly.compileStreaming(...)`. * * See https://github.com/opennextjs/opennextjs-cloudflare/issues/1342 */ export declare const replaceCompileModuleRule = "\nrule:\n all:\n - pattern:\n context: async function $NAME($PATH) { $$$BODY }\n selector: function_declaration\n - has:\n field: body\n has:\n kind: return_statement\n has:\n pattern: WebAssembly.compileStreaming($$$)\n stopBy: end\nfix: |-\n async function $NAME($PATH) {\n return loadWasmChunk($PATH);\n }\n"; /** * Replace the `instantiate` helper that Next.js 16.3 emits in the chunks. * * The counterpart of {@link replaceCompileModuleRule} for the helper instantiating the module: * `WebAssembly.instantiateStreaming` is not supported by workerd either, so the chunk is loaded * with the generated `loadWasmChunk` and instantiated with the synchronous `WebAssembly.instantiate`. * * The rule matches an async function taking the chunk path and the imports as parameters, and * declaring a variable from the result of `WebAssembly.instantiateStreaming(...)`. */ export declare const replaceInstantiateModuleRule = "\nrule:\n all:\n - pattern:\n context: async function $NAME($PATH, $IMPORTS) { $$$BODY }\n selector: function_declaration\n - has:\n field: body\n has:\n kind: lexical_declaration\n has:\n pattern: WebAssembly.instantiateStreaming($$$)\n stopBy: end\nfix: |-\n async function $NAME($PATH, $IMPORTS) {\n const module = await loadWasmChunk($PATH);\n const { exports } = await WebAssembly.instantiate(module, $IMPORTS);\n return exports;\n }\n"; /** * Extract the module specifier from the target of a dangling Turbopack external symlink. * * Live links are imported by their hashed id (see {@link discoverExternalModuleMappings}). This is * the fallback for links whose target does not exist in `.open-next`: the specifier is taken from the * part of the path after the **last** `node_modules` segment, so that esbuild has a chance to resolve * it from the application tree: * - npm/yarn: `../../node_modules/shiki` -> `shiki` * - pnpm: `../../../../node_modules/.pnpm/pg@8.23.1/node_modules/pg` -> `pg` * - pnpm, scoped: `.../node_modules/.pnpm/@scope+name@1.0.0/node_modules/@scope/name` -> `@scope/name` * * Both `/` and `\` separators are accepted (`readlink` returns the latter on Windows), as well as * absolute targets and a trailing separator. * * @param target The target of the symlink, as returned by `readlink`. * @returns The module specifier, or `undefined` when the target is not inside a `node_modules` folder. */ export declare function packageNameFromSymlinkTarget(target: string): string | undefined; /** * Inline the dynamic chunk requires of the Turbopack runtime. * * The runtime resolves the chunks it needs at runtime, which workerd does not support. * The chunks are inlined so that they are statically bundled. * * @param code The code of the Turbopack runtime. * @param filePath Path to the Turbopack runtime, in the OpenNext output. * @param tracedFiles The files traced by Next.js, copied next to the runtime. * @returns The patched code. */ export declare function patchTurbopackRuntimeCode({ code, filePath, tracedFiles, }: { code: string; filePath: string; tracedFiles: string[]; }): string; /** * Replace the wasm helpers that Next.js 16.3 emits in the Turbopack chunks. * * Until Next.js 16.2 the wasm loaders lived in `[turbopack]_runtime.js` and were patched by * {@link patchTurbopackRuntimeCode}. Next.js 16.3 emits them on demand in the chunks instead, so * the chunks need to be patched too. * * @param code The code of the Turbopack chunk. * @param tracedFiles The files traced by Next.js. * @returns The patched code, or `code` unchanged when the chunk has no wasm helper. */ export declare function patchTurbopackWasmChunkCode({ code, tracedFiles, }: { code: string; tracedFiles: string[]; }): string; export declare const patchTurbopackRuntime: CodePatcher; /** * Generate a `loadWasmChunk` function that maps a `.next`-relative chunk path to a * statically-importable `.wasm` module. * * The replacement rules for Turbopack's `loadWebAssembly{,Module}` delegate to this * function. Because the imports are emitted as string literals, the bundler can * statically discover every wasm chunk and include them in the final build. */ export declare function loadWasmChunkFn(tracedFiles: string[]): string;