(outputFolder, hookVariant, options = {})
| 188 | * `node --import @sentry/node/import` injects the channels unconditionally. |
| 189 | */ |
| 190 | export function makeOtelLoaders(outputFolder, hookVariant, options = {}) { |
| 191 | if (hookVariant !== 'otel' && hookVariant !== 'sentry-node') { |
| 192 | throw new Error('hookVariant is neither "otel" nor "sentry-node". Pick one.'); |
| 193 | } |
| 194 | |
| 195 | const { injectDiagnosticsChannel = false } = options; |
| 196 | if (injectDiagnosticsChannel && hookVariant !== 'otel') { |
| 197 | throw new Error('injectDiagnosticsChannel is only supported with the "otel" hookVariant.'); |
| 198 | } |
| 199 | |
| 200 | const expectedRegisterLoaderLocation = `${outputFolder}/import-hook.mjs`; |
| 201 | const foundRegisterLoaderExport = Object.keys(packageDotJSON.exports ?? {}).some(key => { |
| 202 | return packageDotJSON?.exports?.[key]?.import?.default === expectedRegisterLoaderLocation; |
| 203 | }); |
| 204 | if (!foundRegisterLoaderExport) { |
| 205 | throw new Error( |
| 206 | `You used the makeOtelLoaders() rollup utility without specifying the import hook inside \`exports[something].import.default\`. Please add "${expectedRegisterLoaderLocation}" as a value there (maybe check for typos - it needs to be "${expectedRegisterLoaderLocation}" exactly).`, |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | const expectedHooksLoaderLocation = `${outputFolder}/loader-hook.mjs`; |
| 211 | const foundHookLoaderExport = Object.keys(packageDotJSON.exports ?? {}).some(key => { |
| 212 | return packageDotJSON?.exports?.[key]?.import?.default === expectedHooksLoaderLocation; |
| 213 | }); |
| 214 | if (!foundHookLoaderExport) { |
| 215 | throw new Error( |
| 216 | `You used the makeOtelLoaders() rollup utility without specifying the loader hook inside \`exports[something].import.default\`. Please add "${expectedHooksLoaderLocation}" as a value there (maybe check for typos - it needs to be "${expectedHooksLoaderLocation}" exactly).`, |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | const requiredDep = hookVariant === 'otel' ? '@opentelemetry/instrumentation' : '@sentry/node'; |
| 221 | const foundImportInTheMiddleDep = |
| 222 | Object.keys(packageDotJSON.dependencies ?? {}).some(key => { |
| 223 | return key === requiredDep; |
| 224 | }) || |
| 225 | Object.keys(packageDotJSON.devDependencies ?? {}).some(key => { |
| 226 | return key === requiredDep; |
| 227 | }); |
| 228 | |
| 229 | if (!foundImportInTheMiddleDep) { |
| 230 | throw new Error( |
| 231 | `You used the makeOtelLoaders() rollup utility but didn't specify the "${requiredDep}" dependency in ${path.resolve( |
| 232 | process.cwd(), |
| 233 | 'package.json', |
| 234 | )}. Please add it to the dependencies.`, |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | return defineConfig([ |
| 239 | // register() hook |
| 240 | { |
| 241 | input: path.join( |
| 242 | __dirname, |
| 243 | 'code', |
| 244 | hookVariant === 'otel' |
| 245 | ? injectDiagnosticsChannel |
| 246 | ? 'otelEsmImportHookWithDiagnosticsChannelTemplate.js' |
| 247 | : 'otelEsmImportHookTemplate.js' |
no test coverage detected