[other] Make global gating (e.g. DEV) check configurable
Mofei Zhang committed
Feb 29, 2024 at 09:44 UTC
01454d1414c3dbb700fb9a38561868093458f547
3 files changed
+19
-6
compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts
+10
-4
@@ -48,10 +48,16 @@ export const ExternalFunctionSchema = z.object({
48
importSpecifierName: z.string(),
49
});
50
51
-export const InstrumentationSchema = z.object({
52
- fn: ExternalFunctionSchema,
53
- gating: ExternalFunctionSchema.nullish(),
54
-});
51
+export const InstrumentationSchema = z
52
+ .object({
53
+ fn: ExternalFunctionSchema,
54
+ gating: ExternalFunctionSchema.nullish(),
55
+ globalGating: z.string().nullish(),
56
+ })
57
+ .refine(
58
+ (opts) => opts.gating != null || opts.globalGating != null,
59
+ "Expected at least one of gating or globalGating"
60
+ );
61
62
export type ExternalFunction = z.infer<typeof ExternalFunctionSchema>;
63
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+8
-2
@@ -119,11 +119,17 @@ export function codegenFunction(
119
if (emitInstrumentForget.gating != null) {
120
gating = t.logicalExpression(
121
"&&",
122
- t.identifier("__DEV__"),
122
+ t.identifier(emitInstrumentForget.globalGating ?? "__DEV__"),
123
t.identifier(emitInstrumentForget.gating.importSpecifierName)
124
);
125
} else {
126
- gating = t.identifier("__DEV__");
126
+ CompilerError.invariant(emitInstrumentForget.globalGating != null, {
127
+ reason:
128
+ "Bad config not caught! Expected at least one of gating or globalGating",
129
+ loc: null,
130
+ suggestions: null,
131
+ });
132
+ gating = t.identifier(emitInstrumentForget.globalGating);
133
}
134
const test: t.IfStatement = t.ifStatement(
135
gating,
compiler/packages/snap/src/compiler.ts
+1
@@ -73,6 +73,7 @@ function makePluginOptions(
73
source: "react-forget-runtime",
74
importSpecifierName: "shouldInstrument",
75
},
76
+ globalGating: "__DEV__",
77
};
78
}
79
if (firstLine.includes("@enableEmitFreeze")) {