···11+---
22+"@opennextjs/cloudflare": patch
33+---
44+55+fix build issues with `@opentelemetry`
66+77+By using the pre-compiled library provided by Next.
···11+import fs from "node:fs/promises";
22+33+import type { PluginBuild } from "esbuild";
44+55+export default function fixRequire() {
66+ return {
77+ name: "fix-require",
88+99+ setup: async (build: PluginBuild) => {
1010+ build.onLoad({ filter: /.*/ }, async ({ path }) => {
1111+ let contents = await fs.readFile(path, "utf-8");
1212+1313+ // `eval(...)` is not supported by workerd.
1414+ contents = contents.replaceAll(`eval("require")`, "require");
1515+1616+ // `@opentelemetry` has a few issues.
1717+ //
1818+ // Next.js has the following code in `next/dist/server/lib/trace/tracer.js`:
1919+ //
2020+ // try {
2121+ // api = require('@opentelemetry/api');
2222+ // } catch (err) {
2323+ // api = require('next/dist/compiled/@opentelemetry/api');
2424+ // }
2525+ //
2626+ // The intent is to allow users to install their own version of `@opentelemetry/api`.
2727+ //
2828+ // The problem is that even when users do not explicitely install `@opentelemetry/api`,
2929+ // `require('@opentelemetry/api')` resolves to the package which is a dependency
3030+ // of Next.
3131+ //
3232+ // The second problem is that when Next traces files, it would not copy the `api/build/esm`
3333+ // folder (used by the `module` conditions in package.json) it would only copy `api/build/src`.
3434+ // This could be solved by updating the next config:
3535+ //
3636+ // const nextConfig: NextConfig = {
3737+ // // ...
3838+ // outputFileTracingIncludes: {
3939+ // "*": ["./node_modules/@opentelemetry/api/build/**/*"],
4040+ // },
4141+ // };
4242+ //
4343+ // We can consider doing that when we want to enable users to install their own version
4444+ // of `@opentelemetry/api`. For now we simply use the pre-compiled version.
4545+ contents = contents.replace(
4646+ /require\(.@opentelemetry\/api.\)/g,
4747+ `require("next/dist/compiled/@opentelemetry/api")`
4848+ );
4949+5050+ return { contents };
5151+ });
5252+ },
5353+ };
5454+}
···44export * from "./patch-find-dir.js";
55export * from "./patch-load-instrumentation-module.js";
66export * from "./patch-read-file.js";
77-export * from "./wrangler-deps.js";