@samitouri / QOS-React / commits / 6bf64df538

[babel] Add a sources option

This allows the plugin to be configured to run on an allowlist, rather than compiling all files helping with an incremental rollout plan. The sources option takes both an array of path strings or a function to be flexible. For now I've left this be optional but we can make it required. ghstack-source-id: 282a33dc8d08d47f699894692e0fcc813dff5b77 Pull Request resolved: https://github.com/facebook/react-forget/pull/2855

Sathya Gunsasekaran committed Apr 16, 2024 at 14:41 UTC 6bf64df5383113d71bf6ebc8823c6a3ef412e6d0
2 files changed +40
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts
+2
@@ -108,6 +108,8 @@ export type PluginOptions = {
108 * Ignore 'use no forget' annotations. Helpful during testing but should not be used in production.
109 */
110 ignoreUseNoForget: boolean;
111 +
112 + sources?: Array<string> | ((filename: string) => boolean) | null;
113 };
114
115 const CompilationModeSchema = z.enum([
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+38
@@ -195,12 +195,50 @@ const DEFAULT_ESLINT_SUPPRESSIONS = [
195 "react-hooks/rules-of-hooks",
196 ];
197
198 +function isFilePartOfSources(
199 + sources: Array<string> | ((filename: string) => boolean),
200 + filename: string
201 +): boolean {
202 + if (typeof sources === "function") {
203 + return sources(filename);
204 + }
205 +
206 + for (const prefix in sources) {
207 + if (filename.indexOf(prefix) !== -1) {
208 + return true;
209 + }
210 + }
211 +
212 + return false;
213 +}
214 +
215 export function compileProgram(
216 program: NodePath<t.Program>,
217 pass: CompilerPass
218 ): void {
219 const options = parsePluginOptions(pass.opts);
220
221 + if (options.sources) {
222 + if (pass.filename === null) {
223 + const error = new CompilerError();
224 + error.pushErrorDetail(
225 + new CompilerErrorDetail({
226 + reason: `Expected a filename but found none.`,
227 + description:
228 + "When the 'sources' config options is specified, the React compiler will only compile files with a name",
229 + severity: ErrorSeverity.InvalidConfig,
230 + loc: null,
231 + })
232 + );
233 + handleError(error, pass, null);
234 + return;
235 + }
236 +
237 + if (!isFilePartOfSources(options.sources, pass.filename)) {
238 + return;
239 + }
240 + }
241 +
242 // Top level "use no forget", skip this file entirely
243 if (
244 findDirectiveDisablingMemoization(program.node.directives, options) != null