|
1 | 1 | import type { ContentUpdater } from "./content-updater";
|
2 | 2 |
|
3 | 3 | export function fixRequire(updater: ContentUpdater) {
|
4 |
| - return updater.updateContent("fix-require", { filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/ }, ({ contents }) => { |
5 |
| - // `eval(...)` is not supported by workerd. |
6 |
| - contents = contents.replaceAll(`eval("require")`, "require"); |
| 4 | + return updater.updateContent( |
| 5 | + "fix-require", |
| 6 | + { filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/, contentFilter: /.*/ }, |
| 7 | + ({ contents }) => { |
| 8 | + // `eval(...)` is not supported by workerd. |
| 9 | + contents = contents.replaceAll(`eval("require")`, "require"); |
7 | 10 |
|
8 |
| - // `@opentelemetry` has a few issues. |
9 |
| - // |
10 |
| - // Next.js has the following code in `next/dist/server/lib/trace/tracer.js`: |
11 |
| - // |
12 |
| - // try { |
13 |
| - // api = require('@opentelemetry/api'); |
14 |
| - // } catch (err) { |
15 |
| - // api = require('next/dist/compiled/@opentelemetry/api'); |
16 |
| - // } |
17 |
| - // |
18 |
| - // The intent is to allow users to install their own version of `@opentelemetry/api`. |
19 |
| - // |
20 |
| - // The problem is that even when users do not explicitely install `@opentelemetry/api`, |
21 |
| - // `require('@opentelemetry/api')` resolves to the package which is a dependency |
22 |
| - // of Next. |
23 |
| - // |
24 |
| - // The second problem is that when Next traces files, it would not copy the `api/build/esm` |
25 |
| - // folder (used by the `module` conditions in package.json) it would only copy `api/build/src`. |
26 |
| - // This could be solved by updating the next config: |
27 |
| - // |
28 |
| - // const nextConfig: NextConfig = { |
29 |
| - // // ... |
30 |
| - // outputFileTracingIncludes: { |
31 |
| - // "*": ["./node_modules/@opentelemetry/api/build/**/*"], |
32 |
| - // }, |
33 |
| - // }; |
34 |
| - // |
35 |
| - // We can consider doing that when we want to enable users to install their own version |
36 |
| - // of `@opentelemetry/api`. For now we simply use the pre-compiled version. |
37 |
| - contents = contents.replace( |
38 |
| - /require\(.@opentelemetry\/api.\)/g, |
39 |
| - `require("next/dist/compiled/@opentelemetry/api")` |
40 |
| - ); |
| 11 | + // `@opentelemetry` has a few issues. |
| 12 | + // |
| 13 | + // Next.js has the following code in `next/dist/server/lib/trace/tracer.js`: |
| 14 | + // |
| 15 | + // try { |
| 16 | + // api = require('@opentelemetry/api'); |
| 17 | + // } catch (err) { |
| 18 | + // api = require('next/dist/compiled/@opentelemetry/api'); |
| 19 | + // } |
| 20 | + // |
| 21 | + // The intent is to allow users to install their own version of `@opentelemetry/api`. |
| 22 | + // |
| 23 | + // The problem is that even when users do not explicitly install `@opentelemetry/api`, |
| 24 | + // `require('@opentelemetry/api')` resolves to the package which is a dependency |
| 25 | + // of Next. |
| 26 | + // |
| 27 | + // The second problem is that when Next traces files, it would not copy the `api/build/esm` |
| 28 | + // folder (used by the `module` conditions in package.json) it would only copy `api/build/src`. |
| 29 | + // This could be solved by updating the next config: |
| 30 | + // |
| 31 | + // const nextConfig: NextConfig = { |
| 32 | + // // ... |
| 33 | + // outputFileTracingIncludes: { |
| 34 | + // "*": ["./node_modules/@opentelemetry/api/build/**/*"], |
| 35 | + // }, |
| 36 | + // }; |
| 37 | + // |
| 38 | + // We can consider doing that when we want to enable users to install their own version |
| 39 | + // of `@opentelemetry/api`. For now we simply use the pre-compiled version. |
| 40 | + contents = contents.replace( |
| 41 | + /require\(.@opentelemetry\/api.\)/g, |
| 42 | + `require("next/dist/compiled/@opentelemetry/api")` |
| 43 | + ); |
41 | 44 |
|
42 |
| - return contents; |
43 |
| - }); |
| 45 | + return contents; |
| 46 | + } |
| 47 | + ); |
44 | 48 | }
|
0 commit comments