forked from getsentry/sentry-javascript-bundler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent-annotation-transform.ts
More file actions
43 lines (39 loc) · 1.06 KB
/
component-annotation-transform.ts
File metadata and controls
43 lines (39 loc) · 1.06 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
// Webpack loader for component annotation transform
// Based on unplugin v1.0.1 transform loader pattern
export default async function transform(
this: {
async: () => (err: Error | null, content?: string, sourceMap?: unknown) => void;
resourcePath: string;
query: {
transform?: (
code: string,
id: string
) => Promise<{ code: string; map?: unknown } | null | undefined | string>;
};
},
source: string,
map: unknown
): Promise<void> {
const callback = this.async();
const { transform: transformFn } = this.query;
if (!transformFn) {
return callback(null, source, map);
}
try {
const id = this.resourcePath;
const result = await transformFn(source, id);
if (result == null) {
callback(null, source, map);
} else if (typeof result === "string") {
callback(null, result, map);
} else {
callback(null, result.code, result.map || map);
}
} catch (error) {
if (error instanceof Error) {
callback(error);
} else {
callback(new Error(String(error)));
}
}
}