Skip to content
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

fix: REPL with preset-env in Babel 8 #2970

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 deletions js/repl/ReplOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ class ExpandedContainer extends Component<Props, State> {
/>
Enabled
</label>

<label className={styles.envPresetRow}>
<LinkToDocs
className={`${styles.envPresetLabel} ${styles.highlight}`}
Expand Down Expand Up @@ -636,7 +635,7 @@ class ExpandedContainer extends Component<Props, State> {
Modules
</LinkToDocs>
<select
value={envConfig.modules}
value={envConfig.modules + ""}
className={styles.envPresetSelect}
onChange={this._onEnvPresetSettingChange("modules")}
disabled={!envConfig.isEnvPresetEnabled}
Expand All @@ -648,36 +647,42 @@ class ExpandedContainer extends Component<Props, State> {
<option value="commonjs">commonjs</option>
</select>
</label>
<label className={styles.envPresetRow}>
<LinkToDocs
className={`${styles.envPresetLabel} ${styles.highlight}`}
section="spec"
>
Spec
</LinkToDocs>
<input
checked={envConfig.isSpecEnabled}
className={styles.envPresetCheckbox}
disabled={!envConfig.isEnvPresetEnabled}
onChange={this._onEnvPresetSettingCheck("isSpecEnabled")}
type="checkbox"
/>
</label>
<label className={styles.envPresetRow}>
<LinkToDocs
className={`${styles.envPresetLabel} ${styles.highlight}`}
section="loose"
>
Loose
</LinkToDocs>
<input
checked={envConfig.isLooseEnabled}
className={styles.envPresetCheckbox}
disabled={!envConfig.isEnvPresetEnabled}
onChange={this._onEnvPresetSettingCheck("isLooseEnabled")}
type="checkbox"
/>
</label>
{(!babelVersion ||
compareVersions(babelVersion, "8.0.0") === -1) && (
<label className={styles.envPresetRow}>
<LinkToDocs
className={`${styles.envPresetLabel} ${styles.highlight}`}
section="spec"
>
Spec
</LinkToDocs>
<input
checked={envConfig.isSpecEnabled}
className={styles.envPresetCheckbox}
disabled={!envConfig.isEnvPresetEnabled}
onChange={this._onEnvPresetSettingCheck("isSpecEnabled")}
type="checkbox"
/>
</label>
)}
{(!babelVersion ||
compareVersions(babelVersion, "8.0.0") === -1) && (
<label className={styles.envPresetRow}>
<LinkToDocs
className={`${styles.envPresetLabel} ${styles.highlight}`}
section="loose"
>
Loose
</LinkToDocs>
<input
checked={envConfig.isLooseEnabled}
className={styles.envPresetCheckbox}
disabled={!envConfig.isEnvPresetEnabled}
onChange={this._onEnvPresetSettingCheck("isLooseEnabled")}
type="checkbox"
/>
</label>
)}
{isBugfixesSupported && (
<label className={styles.envPresetRow}>
<LinkToDocs
Expand Down
41 changes: 23 additions & 18 deletions js/repl/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,24 @@ export default function compile(code: string, config: CompileConfig): Return {
let envPresetDebugInfo = null;
let sourceMap = null;
let useBuiltIns: false | "entry" | "usage" = false;
let spec = false;
let loose = false;
let bugfixes = false;
let corejs = "3.21";
const transitions = new Transitions();
const meta = {
compiledSize: 0,
rawSize: new Blob([code], { type: "text/plain" }).size,
};

let presetEnvOptions = {};
let presetEnvOptions: {
targets: any;
modules: any;
forceAllTransforms: boolean;
shippedProposals: boolean;
useBuiltIns: false | "entry" | "usage";
corejs: string;
spec?: boolean;
loose?: boolean;
bugfixes?: boolean;
};

if (envConfig && envConfig.isEnvPresetEnabled) {
const targets: any = {};
Expand All @@ -93,15 +100,6 @@ export default function compile(code: string, config: CompileConfig): Return {
if (envConfig.isNodeEnabled) {
targets.node = envConfig.node;
}
if (envConfig.isSpecEnabled) {
spec = envConfig.isSpecEnabled;
}
if (envConfig.isLooseEnabled) {
loose = envConfig.isLooseEnabled;
}
if (envConfig.isBugfixesEnabled) {
bugfixes = envConfig.isBugfixesEnabled;
}

presetEnvOptions = {
targets,
Expand All @@ -110,16 +108,23 @@ export default function compile(code: string, config: CompileConfig): Return {
shippedProposals,
useBuiltIns,
corejs: undefined,
spec,
loose,
};

if (Babel.version && compareVersions(Babel.version, "8.0.0") === -1) {
presetEnvOptions.spec = envConfig.isSpecEnabled;
presetEnvOptions.loose = envConfig.isLooseEnabled;
}

if (useBuiltIns) {
(presetEnvOptions as any).corejs = corejs;
presetEnvOptions.corejs = corejs;
}

if (Babel.version && compareVersions(Babel.version, "7.9.0") !== -1) {
(presetEnvOptions as any).bugfixes = bugfixes;
if (
envConfig.isBugfixesEnabled &&
Babel.version &&
compareVersions(Babel.version, "7.9.0") !== -1
) {
presetEnvOptions.bugfixes = envConfig.isBugfixesEnabled;
}
}

Expand Down