From f9c080ee6a48075fe1834315d3de551fdb572d53 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 16 Nov 2026 03:52:14 +0530 Subject: [PATCH] REPL: preserve env targets that have no sidebar control The REPL only kept the `browsers`, `electron` and `node` keys of the `@babel/preset-env` `targets` option when syncing the config editor with the sidebar. Any other valid target written in the config tab, such as `rhino`, `deno`, `chrome` or `esmodules`, was silently dropped as soon as the config was regenerated (toggling any sidebar option, changing the Babel version, ...), so it was effectively impossible to compile for targets like Rhino, which cannot be expressed as a browserslist query. Move the targets parsing into a new `applyTargetsToEnvConfig` helper in replUtils that stores the extra targets in `envConfig.otherTargets`, and merge them back in `buildTransformOpts`. While here: - reset the Electron/Node toggles when the config's targets object no longer contains them, instead of leaving stale values enabled; - accept `targets` (and `targets.browsers`) given as an array of browserslist queries, instead of discarding them. Fixes #2843 --- website/src/components/repl/Repl.tsx | 20 ++--------- website/src/components/repl/lib/replUtils.ts | 38 ++++++++++++++++++++ website/src/components/repl/lib/types.ts | 3 ++ 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/website/src/components/repl/Repl.tsx b/website/src/components/repl/Repl.tsx index 3c0bddae7c..70daf77e60 100644 --- a/website/src/components/repl/Repl.tsx +++ b/website/src/components/repl/Repl.tsx @@ -22,6 +22,7 @@ import { runtimePolyfillConfig, } from "./lib/pluginConfig"; import { + applyTargetsToEnvConfig, envConfigToTargetsString, replState, pluginConfigArrayToStateMap, @@ -243,24 +244,7 @@ class Repl extends React.Component { envConfig.shippedProposals = presetOptions.shippedProposals || false; - const targets = presetOptions.targets; - if (targets == null) { - envConfig.browsers = ""; - envConfig.isElectronEnabled = envConfig.isNodeEnabled = false; - } else if (typeof targets === "string") { - envConfig.browsers = targets; - envConfig.isElectronEnabled = envConfig.isNodeEnabled = false; - } else if (typeof targets === "object") { - envConfig.browsers = targets.browsers || ""; - if (targets.electron != null) { - envConfig.isElectronEnabled = true; - envConfig.electron = targets.electron; - } - if (targets.node != null) { - envConfig.isNodeEnabled = true; - envConfig.node = targets.node; - } - } + applyTargetsToEnvConfig(envConfig, presetOptions.targets); } return; } diff --git a/website/src/components/repl/lib/replUtils.ts b/website/src/components/repl/lib/replUtils.ts index 97f8dcc33e..70914a0d64 100644 --- a/website/src/components/repl/lib/replUtils.ts +++ b/website/src/components/repl/lib/replUtils.ts @@ -165,6 +165,7 @@ export const persistedStateToEnvConfig = ( isLooseEnabled: !!persistedState.loose, isBugfixesEnabled: !!persistedState.bugfixes, node: envPresetDefaults.node.default, + otherTargets: {}, version: persistedState.version, builtIns: persistedState.builtIns ?? envPresetDefaults.builtIns.default, corejs: persistedState.corejs ?? envPresetDefaults.corejs.default, @@ -210,6 +211,42 @@ export const persistedStateToEnvConfig = ( return envConfig; }; +const browserslistQueryToString = (query: unknown): string => + Array.isArray(query) ? query.join(", ") : String(query ?? ""); + +/** + * Syncs the env preset sidebar state with the `targets` option of a config. + * Targets that have no dedicated control in the sidebar are stored in + * `otherTargets`, so that they survive when the config is regenerated. + */ +export const applyTargetsToEnvConfig = ( + envConfig: EnvConfig, + targets: unknown +) => { + envConfig.browsers = ""; + envConfig.isElectronEnabled = false; + envConfig.isNodeEnabled = false; + envConfig.otherTargets = {}; + + if (typeof targets === "string" || Array.isArray(targets)) { + envConfig.browsers = browserslistQueryToString(targets); + } else if (targets != null && typeof targets === "object") { + const { browsers, electron, node, ...otherTargets } = targets as { + [target: string]: any; + }; + envConfig.browsers = browserslistQueryToString(browsers); + if (electron != null) { + envConfig.isElectronEnabled = true; + envConfig.electron = electron; + } + if (node != null) { + envConfig.isNodeEnabled = true; + envConfig.node = node; + } + envConfig.otherTargets = otherTargets; + } +}; + export const persistedStateToExternalPluginsState = ( persistedState: ReplState ): Array => { @@ -379,6 +416,7 @@ export function buildTransformOpts( if (envConfig.isNodeEnabled) { targets.node = envConfig.node; } + Object.assign(targets, envConfig.otherTargets); if (Object.keys(targets).length === 1 && targets.browsers) { targets = envConfig.browsers; } diff --git a/website/src/components/repl/lib/types.ts b/website/src/components/repl/lib/types.ts index aa719062c0..d051e46871 100644 --- a/website/src/components/repl/lib/types.ts +++ b/website/src/components/repl/lib/types.ts @@ -39,6 +39,9 @@ export type EnvConfig = { shippedProposals: boolean; version?: any; node: string | undefined | null; + // Targets without a dedicated control in the sidebar (e.g. `rhino`, `deno` + // or `esmodules`), preserved as-is from the config. + otherTargets: { [target: string]: unknown }; assumptions?: any; };