-
Notifications
You must be signed in to change notification settings - Fork 49.9k
[compiler][playground] (3/N) Config override panel #34371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3cbb055
2bb805a
241e5c4
66bff46
f8001e5
6adefd1
ce65eef
94e31de
51547c8
911a27f
3f2c56d
312d621
b5d666d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,94 +8,176 @@ | |
| import MonacoEditor, {loader, type Monaco} from '@monaco-editor/react'; | ||
| import type {editor} 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 {useSnackbar} from 'notistack'; | ||
| import {useStore, useStoreDispatch} from '../StoreContext'; | ||
| import {monacoOptions} from './monacoOptions'; | ||
| import { | ||
| ConfigError, | ||
| generateOverridePragmaFromConfig, | ||
| updateSourceWithOverridePragma, | ||
| } 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}); | ||
|
|
||
| export default function ConfigEditor(): JSX.Element { | ||
| const [, setMonaco] = useState<Monaco | null>(null); | ||
| export default function ConfigEditor(): React.ReactElement { | ||
| const [isExpanded, setIsExpanded] = useState(false); | ||
| const store = useStore(); | ||
| const dispatchStore = useStoreDispatch(); | ||
| const {enqueueSnackbar} = useSnackbar(); | ||
|
|
||
| const handleChange: (value: string | undefined) => void = async value => { | ||
| if (value === undefined) return; | ||
| const toggleExpanded = useCallback(() => { | ||
| setIsExpanded(prev => !prev); | ||
| }, []); | ||
|
|
||
| const handleApplyConfig: () => Promise<void> = async () => { | ||
| 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( | ||
| store.source, | ||
| newPragma, | ||
| ); | ||
|
|
||
| // Update the store with both the new config and updated source | ||
| dispatchStore({ | ||
| type: 'updateFile', | ||
| payload: { | ||
| source: updatedSource, | ||
| config: value, | ||
| }, | ||
| }); | ||
| } catch (_) { | ||
| dispatchStore({ | ||
| type: 'updateFile', | ||
| payload: { | ||
| source: store.source, | ||
| config: value, | ||
| config: config, | ||
| }, | ||
| }); | ||
| } 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what you mean by this comment exactly. As oppose to the source? But that also gets passed in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The source remains unchanged but the config gets updated when users type into the config pane. The source is only update when the button is clicked as of now. |
||
| dispatchStore({ | ||
| type: 'updateFile', | ||
| payload: { | ||
| source: store.source, | ||
| config: value, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const handleMount: ( | ||
| _: editor.IStandaloneCodeEditor, | ||
| monaco: 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, | ||
| }); | ||
|
Comment on lines
+95
to
+111
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great, lots of validation features for free! |
||
|
|
||
| const uri = monaco.Uri.parse(`file:///config.js`); | ||
| const uri = monaco.Uri.parse(`file:///config.ts`); | ||
| const model = monaco.editor.getModel(uri); | ||
| if (model) { | ||
| model.updateOptions({tabSize: 2}); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="relative flex flex-col flex-none border-r border-gray-200"> | ||
| <h2 className="p-4 duration-150 ease-in border-b cursor-default border-grey-200 font-light text-secondary"> | ||
| Config Overrides | ||
| </h2> | ||
| <Resizable | ||
| minWidth={300} | ||
| maxWidth={600} | ||
| defaultSize={{width: 350, height: 'auto'}} | ||
| enable={{right: true}} | ||
| className="!h-[calc(100vh_-_3.5rem_-_4rem)]"> | ||
| <MonacoEditor | ||
| path={'config.js'} | ||
| language={'javascript'} | ||
| value={store.config} | ||
| onMount={handleMount} | ||
| onChange={handleChange} | ||
| options={{ | ||
| ...monacoOptions, | ||
| lineNumbers: 'off', | ||
| folding: false, | ||
| renderLineHighlight: 'none', | ||
| scrollBeyondLastLine: false, | ||
| hideCursorInOverviewRuler: true, | ||
| overviewRulerBorder: false, | ||
| overviewRulerLanes: 0, | ||
| fontSize: 12, | ||
| }} | ||
| /> | ||
| </Resizable> | ||
| <div className="flex flex-row relative"> | ||
| {isExpanded ? ( | ||
| <> | ||
| <Resizable | ||
| className="border-r" | ||
| minWidth={300} | ||
| maxWidth={600} | ||
| defaultSize={{width: 350, height: 'auto'}} | ||
| enable={{right: true}}> | ||
| <h2 | ||
| title="Minimize config editor" | ||
| aria-label="Minimize config editor" | ||
| onClick={toggleExpanded} | ||
| className="p-4 duration-150 ease-in border-b cursor-pointer border-grey-200 font-light text-secondary hover:text-link"> | ||
| - Config Overrides | ||
| </h2> | ||
| <div className="h-[calc(100vh_-_3.5rem_-_4rem)]"> | ||
| <MonacoEditor | ||
| path={'config.ts'} | ||
| language={'typescript'} | ||
| value={store.config} | ||
| onMount={handleMount} | ||
| onChange={handleChange} | ||
| options={{ | ||
| ...monacoOptions, | ||
| lineNumbers: 'off', | ||
| folding: false, | ||
| renderLineHighlight: 'none', | ||
| 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> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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>);`; | ||
|
Comment on lines
+19
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This default has to be maintained as we change the config schema or defaults, right? If we can't load automatically from the plugin options themselves, we at least should have a unit test. Basically if this default goes out of date we should fail a test so the next person who changes an option can come update this string.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, planning to add better testing soon |
||
|
|
||
| export const defaultStore: Store = { | ||
| source: index, | ||
| config: '', | ||
| config: defaultConfig, | ||
| }; | ||
|
|
||
| export const emptyStore: Store = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chatted offline about abstracting a CollapsiblePane type component that can be used across all the panes as a follow up so we don't have to recreate this layout logic each time.