[compiler][playground] (3/N) Config override panel (#34371)

<!--
  Thanks for submitting a pull request!
We appreciate you spending the time to work on these changes. Please
provide enough information so that others can review your pull request.
The three fields below are mandatory.

Before submitting a pull request, please make sure the following is
done:

1. Fork [the repository](https://github.com/facebook/react) and create
your branch from `main`.
  2. Run `yarn` in the repository root.
3. If you've fixed a bug or added code that should be tested, add tests!
4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch
TestName` is helpful in development.
5. Run `yarn test --prod` to test in the production environment. It
supports the same options as `yarn test`.
6. If you need a debugger, run `yarn test --debug --watch TestName`,
open `chrome://inspect`, and press "Inspect".
7. Format your code with
[prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only
check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

Learn more about contributing:
https://reactjs.org/docs/how-to-contribute.html
-->

## Summary

Part 3 of adding a "Config Override" panel to the React compiler
playground. Added a button to apply config changes to the Input panel,
as well as making the tab collapsible. Added validation for the the
PluginOptions type (although comes with a bit more boilerplate) to make
it very obvious what the possible config errors could be. Added some
toasts for trying to apply broken configs.

<!--
Explain the **motivation** for making this change. What existing problem
does the pull request solve?
-->

## How did you test this change?


https://github.com/user-attachments/assets/63ab8636-396f-45ba-aaa5-4136e62ccccc


<!--
Demonstrate the code is solid. Example: The exact commands you ran and
their output, screenshots / videos if the pull request changes the user
interface.
How exactly did you verify that your PR solves the issue you wanted to
solve?
  If you leave this empty, your PR will very likely be closed.
-->
This commit is contained in:
Eugene Choi 2025-09-05 10:12:01 -04:00 committed by GitHub
parent b9a045368b
commit de5a1b203e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 196 additions and 67 deletions

View File

@ -8,58 +8,109 @@
import MonacoEditor, {loader, type Monaco} from '@monaco-editor/react'; import MonacoEditor, {loader, type Monaco} from '@monaco-editor/react';
import type {editor} from 'monaco-editor'; import type {editor} from 'monaco-editor';
import * as monaco from 'monaco-editor'; import * as monaco from 'monaco-editor';
import {useState} from 'react'; import React, {useState, useCallback} from 'react';
import {Resizable} from 're-resizable'; import {Resizable} from 're-resizable';
import {useSnackbar} from 'notistack';
import {useStore, useStoreDispatch} from '../StoreContext'; import {useStore, useStoreDispatch} from '../StoreContext';
import {monacoOptions} from './monacoOptions'; import {monacoOptions} from './monacoOptions';
import { import {
ConfigError,
generateOverridePragmaFromConfig, generateOverridePragmaFromConfig,
updateSourceWithOverridePragma, updateSourceWithOverridePragma,
} from '../../lib/configUtils'; } from '../../lib/configUtils';
// @ts-expect-error - webpack asset/source loader handles .d.ts files as strings
import compilerTypeDefs from 'babel-plugin-react-compiler/dist/index.d.ts';
loader.config({monaco}); loader.config({monaco});
export default function ConfigEditor(): JSX.Element { export default function ConfigEditor(): React.ReactElement {
const [, setMonaco] = useState<Monaco | null>(null); const [isExpanded, setIsExpanded] = useState(false);
const store = useStore(); const store = useStore();
const dispatchStore = useStoreDispatch(); const dispatchStore = useStoreDispatch();
const {enqueueSnackbar} = useSnackbar();
const handleChange: (value: string | undefined) => void = async value => { const toggleExpanded = useCallback(() => {
if (value === undefined) return; setIsExpanded(prev => !prev);
}, []);
const handleApplyConfig: () => Promise<void> = async () => {
try { try {
const newPragma = await generateOverridePragmaFromConfig(value); const config = store.config || '';
if (!config.trim()) {
enqueueSnackbar(
'Config is empty. Please add configuration options first.',
{
variant: 'warning',
},
);
return;
}
const newPragma = await generateOverridePragmaFromConfig(config);
const updatedSource = updateSourceWithOverridePragma( const updatedSource = updateSourceWithOverridePragma(
store.source, store.source,
newPragma, newPragma,
); );
// Update the store with both the new config and updated source
dispatchStore({ dispatchStore({
type: 'updateFile', type: 'updateFile',
payload: { payload: {
source: updatedSource, source: updatedSource,
config: value, config: config,
},
});
} catch (_) {
dispatchStore({
type: 'updateFile',
payload: {
source: store.source,
config: value,
}, },
}); });
} catch (error) {
console.error('Failed to apply config:', error);
if (error instanceof ConfigError && error.message.trim()) {
enqueueSnackbar(error.message, {
variant: 'error',
});
} else {
enqueueSnackbar('Unexpected error: failed to apply config.', {
variant: 'error',
});
}
} }
}; };
const handleChange: (value: string | undefined) => void = value => {
if (value === undefined) return;
// Only update the config
dispatchStore({
type: 'updateFile',
payload: {
source: store.source,
config: value,
},
});
};
const handleMount: ( const handleMount: (
_: editor.IStandaloneCodeEditor, _: editor.IStandaloneCodeEditor,
monaco: Monaco, monaco: Monaco,
) => void = (_, monaco) => { ) => void = (_, monaco) => {
setMonaco(monaco); // Add the babel-plugin-react-compiler type definitions to Monaco
monaco.languages.typescript.typescriptDefaults.addExtraLib(
//@ts-expect-error - compilerTypeDefs is a string
compilerTypeDefs,
'file:///node_modules/babel-plugin-react-compiler/dist/index.d.ts',
);
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.Latest,
allowNonTsExtensions: true,
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
module: monaco.languages.typescript.ModuleKind.ESNext,
noEmit: true,
strict: false,
esModuleInterop: true,
allowSyntheticDefaultImports: true,
jsx: monaco.languages.typescript.JsxEmit.React,
});
const uri = monaco.Uri.parse(`file:///config.js`); const uri = monaco.Uri.parse(`file:///config.ts`);
const model = monaco.editor.getModel(uri); const model = monaco.editor.getModel(uri);
if (model) { if (model) {
model.updateOptions({tabSize: 2}); model.updateOptions({tabSize: 2});
@ -67,35 +118,66 @@ export default function ConfigEditor(): JSX.Element {
}; };
return ( return (
<div className="relative flex flex-col flex-none border-r border-gray-200"> <div className="flex flex-row relative">
<h2 className="p-4 duration-150 ease-in border-b cursor-default border-grey-200 font-light text-secondary"> {isExpanded ? (
Config Overrides <>
</h2> <Resizable
<Resizable className="border-r"
minWidth={300} minWidth={300}
maxWidth={600} maxWidth={600}
defaultSize={{width: 350, height: 'auto'}} defaultSize={{width: 350, height: 'auto'}}
enable={{right: true}} enable={{right: true}}>
className="!h-[calc(100vh_-_3.5rem_-_4rem)]"> <h2
<MonacoEditor title="Minimize config editor"
path={'config.js'} aria-label="Minimize config editor"
language={'javascript'} onClick={toggleExpanded}
value={store.config} className="p-4 duration-150 ease-in border-b cursor-pointer border-grey-200 font-light text-secondary hover:text-link">
onMount={handleMount} - Config Overrides
onChange={handleChange} </h2>
options={{ <div className="h-[calc(100vh_-_3.5rem_-_4rem)]">
...monacoOptions, <MonacoEditor
lineNumbers: 'off', path={'config.ts'}
folding: false, language={'typescript'}
renderLineHighlight: 'none', value={store.config}
scrollBeyondLastLine: false, onMount={handleMount}
hideCursorInOverviewRuler: true, onChange={handleChange}
overviewRulerBorder: false, options={{
overviewRulerLanes: 0, ...monacoOptions,
fontSize: 12, lineNumbers: 'off',
}} folding: false,
/> renderLineHighlight: 'none',
</Resizable> scrollBeyondLastLine: false,
hideCursorInOverviewRuler: true,
overviewRulerBorder: false,
overviewRulerLanes: 0,
fontSize: 12,
}}
/>
</div>
</Resizable>
<button
onClick={handleApplyConfig}
title="Apply config overrides to input"
aria-label="Apply config overrides to input"
className="absolute right-0 top-1/2 transform -translate-y-1/2 translate-x-1/2 z-10 w-8 h-8 bg-blue-500 hover:bg-blue-600 text-white rounded-full border-2 border-white shadow-lg flex items-center justify-center text-sm font-medium transition-colors duration-150">
</button>
</>
) : (
<div className="relative items-center h-full px-1 py-6 align-middle border-r border-grey-200">
<button
title="Expand config editor"
aria-label="Expand config editor"
style={{
transform: 'rotate(90deg) translate(-50%)',
whiteSpace: 'nowrap',
}}
onClick={toggleExpanded}
className="flex-grow-0 w-5 transition-colors duration-150 ease-in font-light text-secondary hover:text-link">
Config Overrides
</button>
</div>
)}
</div> </div>
); );
} }

View File

@ -48,7 +48,6 @@ import {
import {transformFromAstSync} from '@babel/core'; import {transformFromAstSync} from '@babel/core';
import {LoggerEvent} from 'babel-plugin-react-compiler/dist/Entrypoint'; import {LoggerEvent} from 'babel-plugin-react-compiler/dist/Entrypoint';
import {useSearchParams} from 'next/navigation'; import {useSearchParams} from 'next/navigation';
import {parseAndFormatConfig} from '../../lib/configUtils';
function parseInput( function parseInput(
input: string, input: string,
@ -317,16 +316,11 @@ export default function Editor(): JSX.Element {
mountStore = defaultStore; mountStore = defaultStore;
} }
parseAndFormatConfig(mountStore.source).then(config => { dispatchStore({
dispatchStore({ type: 'setStore',
type: 'setStore', payload: {
payload: { store: mountStore,
store: { },
...mountStore,
config,
},
},
});
}); });
}); });

View File

@ -8,8 +8,15 @@
import parserBabel from 'prettier/plugins/babel'; import parserBabel from 'prettier/plugins/babel';
import prettierPluginEstree from 'prettier/plugins/estree'; import prettierPluginEstree from 'prettier/plugins/estree';
import * as prettier from 'prettier/standalone'; import * as prettier from 'prettier/standalone';
import {parsePluginOptions} from 'babel-plugin-react-compiler';
import {parseConfigPragmaAsString} from '../../../packages/babel-plugin-react-compiler/src/Utils/TestUtils'; import {parseConfigPragmaAsString} from '../../../packages/babel-plugin-react-compiler/src/Utils/TestUtils';
export class ConfigError extends Error {
constructor(message: string) {
super(message);
this.name = 'ConfigError';
}
}
/** /**
* Parse config from pragma and format it with prettier * Parse config from pragma and format it with prettier
*/ */
@ -17,7 +24,10 @@ export async function parseAndFormatConfig(source: string): Promise<string> {
const pragma = source.substring(0, source.indexOf('\n')); const pragma = source.substring(0, source.indexOf('\n'));
let configString = parseConfigPragmaAsString(pragma); let configString = parseConfigPragmaAsString(pragma);
if (configString !== '') { if (configString !== '') {
configString = `(${configString})`; configString = `\
import type { PluginOptions } from 'babel-plugin-react-compiler/dist';
(${configString} satisfies Partial<PluginOptions>)`;
} }
try { try {
@ -34,10 +44,10 @@ export async function parseAndFormatConfig(source: string): Promise<string> {
} }
function extractCurlyBracesContent(input: string): string { function extractCurlyBracesContent(input: string): string {
const startIndex = input.indexOf('{'); const startIndex = input.indexOf('({') + 1;
const endIndex = input.lastIndexOf('}'); const endIndex = input.lastIndexOf('}');
if (startIndex === -1 || endIndex === -1 || endIndex <= startIndex) { if (startIndex === -1 || endIndex === -1 || endIndex <= startIndex) {
throw new Error('No outer curly braces found in input'); throw new Error('No outer curly braces found in input.');
} }
return input.slice(startIndex, endIndex + 1); return input.slice(startIndex, endIndex + 1);
} }
@ -49,6 +59,27 @@ function cleanContent(content: string): string {
.trim(); .trim();
} }
/**
* Validate that a config string can be parsed as a valid PluginOptions object
* Throws an error if validation fails.
*/
function validateConfigAsPluginOptions(configString: string): void {
// Validate that config can be parse as JS obj
let parsedConfig: unknown;
try {
parsedConfig = new Function(`return (${configString})`)();
} catch (_) {
throw new ConfigError('Config has invalid syntax.');
}
// Validate against PluginOptions schema
try {
parsePluginOptions(parsedConfig);
} catch (_) {
throw new ConfigError('Config does not match the expected schema.');
}
}
/** /**
* Generate a the override pragma comment from a formatted config object string * Generate a the override pragma comment from a formatted config object string
*/ */
@ -58,6 +89,8 @@ export async function generateOverridePragmaFromConfig(
const content = extractCurlyBracesContent(formattedConfigString); const content = extractCurlyBracesContent(formattedConfigString);
const cleanConfig = cleanContent(content); const cleanConfig = cleanContent(content);
validateConfigAsPluginOptions(cleanConfig);
// Format the config to ensure it's valid // Format the config to ensure it's valid
await prettier.format(`(${cleanConfig})`, { await prettier.format(`(${cleanConfig})`, {
semi: false, semi: false,

View File

@ -13,9 +13,31 @@ export default function MyApp() {
} }
`; `;
export const defaultConfig = `\
import type { PluginOptions } from 'babel-plugin-react-compiler/dist';
({
compilationMode: 'infer',
panicThreshold: 'none',
environment: {},
logger: null,
gating: null,
noEmit: false,
dynamicGating: null,
eslintSuppressionRules: null,
flowSuppressions: true,
ignoreUseNoForget: false,
sources: filename => {
return filename.indexOf('node_modules') === -1;
},
enableReanimatedCheck: true,
customOptOutDirectives: null,
target: '19',
} satisfies Partial<PluginOptions>);`;
export const defaultStore: Store = { export const defaultStore: Store = {
source: index, source: index,
config: '', config: defaultConfig,
}; };
export const emptyStore: Store = { export const emptyStore: Store = {

View File

@ -10,7 +10,7 @@ import {
compressToEncodedURIComponent, compressToEncodedURIComponent,
decompressFromEncodedURIComponent, decompressFromEncodedURIComponent,
} from 'lz-string'; } from 'lz-string';
import {defaultStore} from '../defaultStore'; import {defaultStore, defaultConfig} from '../defaultStore';
/** /**
* Global Store for Playground * Global Store for Playground
@ -68,10 +68,10 @@ export function initStoreFromUrlOrLocalStorage(): Store {
invariant(isValidStore(raw), 'Invalid Store'); invariant(isValidStore(raw), 'Invalid Store');
// Add config property if missing for backwards compatibility // Add config property if missing for backwards compatibility
if (!('config' in raw)) { if (!('config' in raw) || !raw['config']) {
return { return {
...raw, ...raw,
config: '', config: defaultConfig,
}; };
} }

View File

@ -253,8 +253,6 @@ function parseConfigStringAsJS(
}); });
} }
console.log('OVERRIDE:', parsedConfig);
const environment = parseConfigPragmaEnvironmentForTest( const environment = parseConfigPragmaEnvironmentForTest(
'', '',
defaults.environment ?? {}, defaults.environment ?? {},