-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcomponentAnnotationLoader.ts
More file actions
45 lines (39 loc) · 1.4 KB
/
componentAnnotationLoader.ts
File metadata and controls
45 lines (39 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
import { createComponentNameAnnotateHooks } from '@sentry/bundler-plugin-core';
import type { LoaderThis } from './types';
export type ComponentAnnotationLoaderOptions = {
ignoredComponents?: string[];
};
/**
* Turbopack loader that annotates React components with `data-sentry-component`,
* `data-sentry-element`, and `data-sentry-source-file` attributes.
*
* This is the Turbopack equivalent of what `@sentry/webpack-plugin` does
* via the `reactComponentAnnotation` option and `@sentry/babel-plugin-component-annotate`.
*
* Options:
* - `ignoredComponents`: List of component names to exclude from annotation.
*/
export default function componentAnnotationLoader(
this: LoaderThis<ComponentAnnotationLoaderOptions>,
userCode: string,
): void {
const options = 'getOptions' in this ? this.getOptions() : this.query;
const ignoredComponents = options.ignoredComponents ?? [];
// We do not want to cache results across builds
this.cacheable(false);
const callback = this.async() ?? this.callback;
const hooks = createComponentNameAnnotateHooks(ignoredComponents, false);
hooks
.transform(userCode, this.resourcePath)
.then(result => {
if (result) {
callback(null, result.code, result.map);
} else {
callback(null, userCode);
}
})
.catch(() => {
// On error, pass through the original code gracefully
callback(null, userCode);
});
}