Add a logger option for logging compiler errors from Babel
Add a `logger` option so compiler errors are surfaced in our metrics collection pipeline. We can probably later merge the global `log` function with it.
Tianyu Yao committed
Mar 2, 2023 at 16:59 UTC
de6444b4f5c2a15c70992489d8801f0cab6f788d
3 files changed
+20
-5
compiler/forget/src/Babel/BabelPlugin.ts
+12
-5
@@ -73,11 +73,18 @@ export default function ReactForgetBabelPlugin(
73
// prior to B, if A does not have a Program visitor and B does, B will run first. We always
74
// want Forget to run true to source as possible.
75
Program(path, pass) {
76
- const flags = parsePluginOptions(pass.opts);
77
- path.traverse(visitor, {
78
- ...pass,
79
- opts: { ...pass.opts, ...flags },
80
- });
76
+ const options = parsePluginOptions(pass.opts);
77
+ try {
78
+ path.traverse(visitor, {
79
+ ...pass,
80
+ opts: { ...pass.opts, ...options },
81
+ });
82
+ } catch (err) {
83
+ if (options.logger && err) {
84
+ options.logger.logEvent("err", err);
85
+ }
86
+ throw err;
87
+ }
88
},
89
},
90
};
compiler/forget/src/Babel/PluginOptions.ts
+7
@@ -14,11 +14,18 @@ export type PluginOptions = {
14
enableOnlyOnUseForgetDirective: boolean;
15
16
environment: Partial<EnvironmentOptions> | null;
17
+
18
+ logger: Logger | null;
19
+};
20
+
21
+export type Logger = {
22
+ logEvent(name: string, data: any): void;
23
};
24
25
export const defaultOptions: PluginOptions = {
26
enableOnlyOnUseForgetDirective: false,
27
environment: null,
28
+ logger: null,
29
} as const;
30
31
export function parsePluginOptions(obj: unknown): PluginOptions {
compiler/forget/src/__tests__/hir-test.ts
+1
@@ -76,6 +76,7 @@ describe("React Forget (HIR version)", () => {
76
],
77
]),
78
},
79
+ logger: null,
80
}).code,
81
});
82
} catch (e) {