|
1 | 1 | import replace from '@rollup/plugin-replace'; |
2 | 2 | import { makeBaseNPMConfig, makeNPMConfigVariants, plugins } from '@sentry-internal/rollup-utils'; |
3 | 3 |
|
4 | | -export default makeNPMConfigVariants( |
5 | | - makeBaseNPMConfig({ |
6 | | - entrypoints: ['src/index.ts'], |
7 | | - bundledBuiltins: ['perf_hooks', 'util'], |
8 | | - packageSpecificConfig: { |
9 | | - context: 'globalThis', |
10 | | - output: { |
11 | | - preserveModules: false, |
12 | | - }, |
13 | | - plugins: [ |
14 | | - plugins.makeCommonJSPlugin({ transformMixedEsModules: true }), // Needed because various modules in the OTEL toolchain use CJS (require-in-the-middle, shimmer, etc..) |
15 | | - plugins.makeJsonPlugin(), // Needed because `require-in-the-middle` imports json via require |
16 | | - replace({ |
17 | | - preventAssignment: true, |
18 | | - values: { |
19 | | - 'process.argv0': JSON.stringify(''), // needed because otel relies on process.argv0 for the default service name, but that api is not available in the edge runtime. |
20 | | - }, |
21 | | - }), |
22 | | - { |
23 | | - // This plugin is needed because otel imports `performance` from `perf_hooks` and also uses it via the `performance` global. |
24 | | - // It also imports `inspect` and `promisify` from node's `util` which are not available in the edge runtime so we need to define a polyfill. |
25 | | - // Both of these APIs are not available in the edge runtime so we need to define a polyfill. |
26 | | - // Vercel does something similar in the `@vercel/otel` package: https://github.com/vercel/otel/blob/087601ae585cb116bb2b46c211d014520de76c71/packages/otel/build.ts#L62 |
27 | | - name: 'edge-runtime-polyfills', |
28 | | - banner: ` |
29 | | - { |
30 | | - if (globalThis.performance === undefined) { |
31 | | - globalThis.performance = { |
32 | | - timeOrigin: 0, |
33 | | - now: () => Date.now() |
34 | | - }; |
35 | | - } |
36 | | - } |
37 | | - `, |
38 | | - resolveId: source => { |
39 | | - if (source === 'perf_hooks') { |
40 | | - return '\0perf_hooks_sentry_shim'; |
41 | | - } else if (source === 'util') { |
42 | | - return '\0util_sentry_shim'; |
43 | | - } else { |
44 | | - return null; |
| 4 | +const downlevelLogicalAssignmentsPlugin = { |
| 5 | + name: 'downlevel-logical-assignments', |
| 6 | + renderChunk(code) { |
| 7 | + // ES2021 logical assignment operators (`||=`, `&&=`, `??=`) are not allowed by our ES2020 compatibility check. |
| 8 | + // OTEL currently ships some of these, so we downlevel them in the final output. |
| 9 | + // |
| 10 | + // Note: This is intentionally conservative (only matches property access-like LHS) to avoid duplicating side effects. |
| 11 | + // IMPORTANT: Use regex literals (not `String.raw` + `RegExp(...)`) to avoid accidental double-escaping. |
| 12 | + let out = code; |
| 13 | + |
| 14 | + // ??= |
| 15 | + out = out.replace(/([A-Za-z_$][\w$]*(?:\[[^\]]+\]|\.[A-Za-z_$][\w$]*)+)\s*\?\?=\s*([^;]+);/g, (_m, left, right) => { |
| 16 | + return `${left} = ${left} ?? ${right};`; |
| 17 | + }); |
| 18 | + |
| 19 | + // ||= |
| 20 | + out = out.replace(/([A-Za-z_$][\w$]*(?:\[[^\]]+\]|\.[A-Za-z_$][\w$]*)+)\s*\|\|=\s*([^;]+);/g, (_m, left, right) => { |
| 21 | + return `${left} = ${left} || ${right};`; |
| 22 | + }); |
| 23 | + |
| 24 | + // &&= |
| 25 | + out = out.replace(/([A-Za-z_$][\w$]*(?:\[[^\]]+\]|\.[A-Za-z_$][\w$]*)+)\s*&&=\s*([^;]+);/g, (_m, left, right) => { |
| 26 | + return `${left} = ${left} && ${right};`; |
| 27 | + }); |
| 28 | + |
| 29 | + return { code: out, map: null }; |
| 30 | + }, |
| 31 | +}; |
| 32 | + |
| 33 | +const baseConfig = makeBaseNPMConfig({ |
| 34 | + entrypoints: ['src/index.ts'], |
| 35 | + bundledBuiltins: ['perf_hooks', 'util'], |
| 36 | + packageSpecificConfig: { |
| 37 | + context: 'globalThis', |
| 38 | + output: { |
| 39 | + preserveModules: false, |
| 40 | + }, |
| 41 | + plugins: [ |
| 42 | + plugins.makeCommonJSPlugin({ transformMixedEsModules: true }), // Needed because various modules in the OTEL toolchain use CJS (require-in-the-middle, shimmer, etc..) |
| 43 | + plugins.makeJsonPlugin(), // Needed because `require-in-the-middle` imports json via require |
| 44 | + replace({ |
| 45 | + preventAssignment: true, |
| 46 | + values: { |
| 47 | + 'process.argv0': JSON.stringify(''), // needed because otel relies on process.argv0 for the default service name, but that api is not available in the edge runtime. |
| 48 | + }, |
| 49 | + }), |
| 50 | + { |
| 51 | + // This plugin is needed because otel imports `performance` from `perf_hooks` and also uses it via the `performance` global. |
| 52 | + // It also imports `inspect` and `promisify` from node's `util` which are not available in the edge runtime so we need to define a polyfill. |
| 53 | + // Both of these APIs are not available in the edge runtime so we need to define a polyfill. |
| 54 | + // Vercel does something similar in the `@vercel/otel` package: https://github.com/vercel/otel/blob/087601ae585cb116bb2b46c211d014520de76c71/packages/otel/build.ts#L62 |
| 55 | + name: 'edge-runtime-polyfills', |
| 56 | + banner: ` |
| 57 | + { |
| 58 | + if (globalThis.performance === undefined) { |
| 59 | + globalThis.performance = { |
| 60 | + timeOrigin: 0, |
| 61 | + now: () => Date.now() |
| 62 | + }; |
45 | 63 | } |
46 | | - }, |
47 | | - load: id => { |
48 | | - if (id === '\0perf_hooks_sentry_shim') { |
49 | | - return ` |
50 | | - export const performance = { |
51 | | - timeOrigin: 0, |
52 | | - now: () => Date.now() |
53 | | - } |
54 | | - `; |
55 | | - } else if (id === '\0util_sentry_shim') { |
56 | | - return ` |
57 | | - export const inspect = (object) => |
58 | | - JSON.stringify(object, null, 2); |
| 64 | + } |
| 65 | + `, |
| 66 | + resolveId: source => { |
| 67 | + if (source === 'perf_hooks') { |
| 68 | + return '\0perf_hooks_sentry_shim'; |
| 69 | + } else if (source === 'util') { |
| 70 | + return '\0util_sentry_shim'; |
| 71 | + } else { |
| 72 | + return null; |
| 73 | + } |
| 74 | + }, |
| 75 | + load: id => { |
| 76 | + if (id === '\0perf_hooks_sentry_shim') { |
| 77 | + return ` |
| 78 | + export const performance = { |
| 79 | + timeOrigin: 0, |
| 80 | + now: () => Date.now() |
| 81 | + } |
| 82 | + `; |
| 83 | + } else if (id === '\0util_sentry_shim') { |
| 84 | + return ` |
| 85 | + export const inspect = (object) => |
| 86 | + JSON.stringify(object, null, 2); |
59 | 87 |
|
60 | | - export const promisify = (fn) => { |
61 | | - return (...args) => { |
62 | | - return new Promise((resolve, reject) => { |
63 | | - fn(...args, (err, result) => { |
64 | | - if (err) reject(err); |
65 | | - else resolve(result); |
66 | | - }); |
| 88 | + export const promisify = (fn) => { |
| 89 | + return (...args) => { |
| 90 | + return new Promise((resolve, reject) => { |
| 91 | + fn(...args, (err, result) => { |
| 92 | + if (err) reject(err); |
| 93 | + else resolve(result); |
67 | 94 | }); |
68 | | - }; |
| 95 | + }); |
69 | 96 | }; |
70 | | - `; |
71 | | - } else { |
72 | | - return null; |
73 | | - } |
74 | | - }, |
| 97 | + }; |
| 98 | + `; |
| 99 | + } else { |
| 100 | + return null; |
| 101 | + } |
75 | 102 | }, |
76 | | - ], |
77 | | - }, |
78 | | - }), |
79 | | -); |
| 103 | + }, |
| 104 | + downlevelLogicalAssignmentsPlugin, |
| 105 | + ], |
| 106 | + }, |
| 107 | +}); |
| 108 | + |
| 109 | +// `makeBaseNPMConfig` marks dependencies/peers as external by default. |
| 110 | +// For Edge, we must ensure the OTEL SDK bits which reference `process.argv0` are bundled so our replace() plugin applies. |
| 111 | +const baseExternal = baseConfig.external; |
| 112 | +baseConfig.external = (source, importer, isResolved) => { |
| 113 | + // Never treat these as external - they need to be inlined so `process.argv0` can be replaced. |
| 114 | + if ( |
| 115 | + source === '@opentelemetry/resources' || |
| 116 | + source.startsWith('@opentelemetry/resources/') || |
| 117 | + source === '@opentelemetry/sdk-trace-base' || |
| 118 | + source.startsWith('@opentelemetry/sdk-trace-base/') |
| 119 | + ) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + if (typeof baseExternal === 'function') { |
| 124 | + return baseExternal(source, importer, isResolved); |
| 125 | + } |
| 126 | + |
| 127 | + if (Array.isArray(baseExternal)) { |
| 128 | + return baseExternal.includes(source); |
| 129 | + } |
| 130 | + |
| 131 | + if (baseExternal instanceof RegExp) { |
| 132 | + return baseExternal.test(source); |
| 133 | + } |
| 134 | + |
| 135 | + return false; |
| 136 | +}; |
| 137 | + |
| 138 | +export default makeNPMConfigVariants(baseConfig); |
0 commit comments