Skip to content

Playground: auto-open @rescript/core when loaded #729

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

Merged
merged 4 commits into from
Nov 2, 2023
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
10 changes: 10 additions & 0 deletions src/Playground.res
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,19 @@ module Settings = {

let onResetClick = evt => {
ReactEvent.Mouse.preventDefault(evt)

let open_modules = switch readyState.selected.apiVersion {
| V1 | V2 | V3 | UnknownVersion(_) => None
| V4 =>
readyState.selected.libraries->Belt.Array.some(el => el === "@rescript/core")
? Some(["RescriptCore"])
: None
}

let defaultConfig = {
Api.Config.module_system: "nodejs",
warn_flags: "+a-4-9-20-40-41-42-50-61-102-109",
?open_modules,
}
setConfig(defaultConfig)
}
Expand Down
9 changes: 8 additions & 1 deletion src/bindings/RescriptCompilerApi.res
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Version = {
| V1
| V2
| V3
| V4
| UnknownVersion(string)

// Helps finding the right API version
Expand All @@ -57,6 +58,7 @@ module Version = {
}
| list{"2"} => V2
| list{"3"} => V3
| list{"4"} => V4
| _ => UnknownVersion(apiVersion)
}

Expand All @@ -65,6 +67,7 @@ module Version = {
| V1 => "1.0"
| V2 => "2.0"
| V3 => "3.0"
| V4 => "4.0"
| UnknownVersion(version) => version
}

Expand All @@ -73,7 +76,7 @@ module Version = {
let availableLanguages = t =>
switch t {
| V1 => [Lang.Reason, Res]
| V2 | V3 => [Lang.Res]
| V2 | V3 | V4 => [Lang.Res]
| UnknownVersion(_) => [Res]
}
}
Expand Down Expand Up @@ -348,6 +351,7 @@ module Config = {
module_system: string,
warn_flags: string,
uncurried?: bool,
open_modules?: array<string>,
}
}

Expand Down Expand Up @@ -423,6 +427,8 @@ module Compiler = {

@send external setWarnFlags: (t, string) => bool = "setWarnFlags"

@send external setOpenModules: (t, array<string>) => bool = "setOpenModules"

let setConfig = (t: t, config: Config.t): unit => {
let moduleSystem = switch config.module_system {
| "nodejs" => #nodejs->Some
Expand All @@ -431,6 +437,7 @@ module Compiler = {
}

Belt.Option.forEach(moduleSystem, moduleSystem => t->setModuleSystem(moduleSystem)->ignore)
Belt.Option.forEach(config.open_modules, modules => t->setOpenModules(modules)->ignore)
Copy link
Member

@ryyppy ryyppy Nov 1, 2023

Choose a reason for hiding this comment

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

I guess here one needs to check for compiler bundle version to be V4 as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Did that but at a different spot (see commit).


t->setWarnFlags(config.warn_flags)->ignore
}
Expand Down
3 changes: 3 additions & 0 deletions src/bindings/RescriptCompilerApi.resi
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Version: {
| V1
| V2
| V3
| V4
| UnknownVersion(string)

// Helps finding the right API version
Expand Down Expand Up @@ -159,6 +160,7 @@ module Config: {
warn_flags: string,
/** Only available in apiVersion > 3 (= ReScript 11+) */
uncurried?: bool,
open_modules?: array<string>,
}
}

Expand Down Expand Up @@ -197,6 +199,7 @@ module Compiler: {
let setModuleSystem: (t, [#es6 | #nodejs]) => bool

let setWarnFlags: (t, string) => bool
let setOpenModules: (t, array<string>) => bool
let setConfig: (t, Config.t) => unit

// General format function
Expand Down
15 changes: 13 additions & 2 deletions src/common/CompilerManagerHook.res
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ let getLibrariesForVersion = (~version: Semver.t): array<string> => {
libraries
}

let getOpenModules = (~apiVersion: Version.t, ~libraries: array<string>): option<array<string>> =>
switch apiVersion {
| V1 | V2 | V3 | UnknownVersion(_) => None
| V4 => libraries->Belt.Array.some(el => el === "@rescript/core") ? Some(["RescriptCore"]) : None
}

/*
This function loads the compiler, plus a defined set of libraries that are available
on our bs-platform-js-releases channel.
Expand Down Expand Up @@ -396,6 +402,7 @@ let useCompilerManager = (
| Ok() =>
let instance = Compiler.make()
let apiVersion = apiVersion->Version.fromString
let open_modules = getOpenModules(~apiVersion, ~libraries)

// Note: The compiler bundle currently defaults to
// commonjs when initiating the compiler, but our playground
Expand All @@ -405,6 +412,7 @@ let useCompilerManager = (
let config = {
...instance->Compiler.getConfig,
module_system: "es6",
?open_modules,
}
instance->Compiler.setConfig(config)

Expand Down Expand Up @@ -453,7 +461,10 @@ let useCompilerManager = (

let instance = Compiler.make()
let apiVersion = apiVersion->Version.fromString
let config = instance->Compiler.getConfig
let open_modules = getOpenModules(~apiVersion, ~libraries)

let config = {...instance->Compiler.getConfig, ?open_modules}
instance->Compiler.setConfig(config)

let selected = {
id: version,
Expand Down Expand Up @@ -488,7 +499,7 @@ let useCompilerManager = (
| Lang.Reason => instance->Compiler.reasonCompile(code)
| Lang.Res => instance->Compiler.resCompile(code)
}
| V2 | V3 =>
| V2 | V3 | V4 =>
switch lang {
| Lang.OCaml => instance->Compiler.ocamlCompile(code)
| Lang.Reason =>
Expand Down