-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathinjectLoader.ts
More file actions
46 lines (37 loc) · 1.4 KB
/
injectLoader.ts
File metadata and controls
46 lines (37 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { InstrumentationConfig } from '@apm-js-collab/code-transformer';
import ModulePatch from '@apm-js-collab/tracing-hooks';
import { debug, GLOBAL_OBJ } from '@sentry/core';
import * as moduleModule from 'module';
import { supportsEsmLoaderHooks } from '../utils/detection';
let instrumentationConfigs: InstrumentationConfig[] | undefined;
/**
* Add an instrumentation config to be used by the injection loader.
*/
export function addInstrumentationConfig(config: InstrumentationConfig): void {
if (!supportsEsmLoaderHooks()) {
return;
}
if (!instrumentationConfigs) {
instrumentationConfigs = [];
}
instrumentationConfigs.push(config);
GLOBAL_OBJ._sentryInjectLoaderHookRegister = () => {
if (GLOBAL_OBJ._sentryInjectLoaderHookRegistered) {
return;
}
GLOBAL_OBJ._sentryInjectLoaderHookRegistered = true;
const instrumentations = instrumentationConfigs || [];
// Patch require to support CJS modules
const requirePatch = new ModulePatch({ instrumentations });
requirePatch.patch();
// Add ESM loader to support ESM modules
try {
// @ts-expect-error register is available in these versions
moduleModule.register('@apm-js-collab/tracing-hooks/hook.mjs', import.meta.url, {
data: { instrumentations },
});
} catch (error) {
debug.warn("Failed to register '@apm-js-collab/tracing-hooks' hook", error);
}
};
}