-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmoduleMetadataInjectionLoader.ts
More file actions
44 lines (39 loc) · 2.05 KB
/
moduleMetadataInjectionLoader.ts
File metadata and controls
44 lines (39 loc) · 2.05 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
import type { LoaderThis } from './types';
import { findInjectionIndexAfterDirectives } from './valueInjectionLoader';
export type ModuleMetadataInjectionLoaderOptions = {
applicationKey: string;
};
/**
* Inject `_sentryModuleMetadata` into every module so that the
* `thirdPartyErrorFilterIntegration` can tell first-party code from
* third-party code.
*
* This is the Turbopack equivalent of what `@sentry/webpack-plugin` does
* via its `moduleMetadata` option.
*
* Options:
* - `applicationKey`: The application key used to tag first-party modules.
*/
export default function moduleMetadataInjectionLoader(
this: LoaderThis<ModuleMetadataInjectionLoaderOptions>,
userCode: string,
): string {
const { applicationKey } = 'getOptions' in this ? this.getOptions() : this.query;
// We do not want to cache injected values across builds
this.cacheable(false);
// The snippet mirrors what @sentry/webpack-plugin injects for moduleMetadata.
// It is wrapped in a try-catch IIFE (matching the webpack plugin's CodeInjection pattern)
// so that injection failures in node_modules or unusual environments never break the module.
// The IIFE resolves the global object and stores metadata keyed by (new Error).stack
// so the SDK can map chunk filenames to metadata at runtime.
// Not putting any newlines in the generated code will decrease the likelihood of sourcemaps breaking.
const metadata = JSON.stringify({ [`_sentryBundlerPluginAppKey:${applicationKey}`]: true });
const injectedCode =
';!function(){try{' +
'var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{};' +
'e._sentryModuleMetadata=e._sentryModuleMetadata||{},' +
`e._sentryModuleMetadata[(new e.Error).stack]=Object.assign({},e._sentryModuleMetadata[(new e.Error).stack],${metadata});` +
'}catch(e){}}();';
const injectionIndex = findInjectionIndexAfterDirectives(userCode);
return `${userCode.slice(0, injectionIndex)}${injectedCode}${userCode.slice(injectionIndex)}`;
}