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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions js/repl/PluginConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const envPresetDefaults = {
default: "3.21",
},
modules: {
default: false,
default: "false",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Why is this a string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
select.value does not accept false.

Thanks for your reminder, I checked the code and it handles the false of string.
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it seems like it should be done another way. :)

},
} as const;

Expand Down Expand Up @@ -109,7 +109,7 @@ const replDefaults: ReplState = {
compiledSize: 0,
rawSize: 0,
},
modules: false,
modules: "false",
presets: "react,stage-2,env",
prettier: false,
showSidebar: true,
Expand Down
67 changes: 36 additions & 31 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 @@ -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
2 changes: 1 addition & 1 deletion js/repl/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type EnvConfig = {
isSpecEnabled: boolean;
isLooseEnabled: boolean;
builtIns: false | "entry" | "usage";
modules: false | "amd" | "umd" | "systemjs" | "commonjs";
modules: "false" | "amd" | "umd" | "systemjs" | "commonjs";
corejs: string | false;
forceAllTransforms: boolean;
shippedProposals: boolean;
Expand Down