Skip to content

Commit 9230dab

Browse files
authored
Merge pull request #729 from fhammerschmidt/auto-open-rescript-core
Playground: auto-open @rescript/core when loaded
2 parents 811eb6b + 931b9f5 commit 9230dab

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

src/Playground.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,19 @@ module Settings = {
902902

903903
let onResetClick = evt => {
904904
ReactEvent.Mouse.preventDefault(evt)
905+
906+
let open_modules = switch readyState.selected.apiVersion {
907+
| V1 | V2 | V3 | UnknownVersion(_) => None
908+
| V4 =>
909+
readyState.selected.libraries->Belt.Array.some(el => el === "@rescript/core")
910+
? Some(["RescriptCore"])
911+
: None
912+
}
913+
905914
let defaultConfig = {
906915
Api.Config.module_system: "nodejs",
907916
warn_flags: "+a-4-9-20-40-41-42-50-61-102-109",
917+
?open_modules,
908918
}
909919
setConfig(defaultConfig)
910920
}

src/bindings/RescriptCompilerApi.res

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module Version = {
3636
| V1
3737
| V2
3838
| V3
39+
| V4
3940
| UnknownVersion(string)
4041

4142
// Helps finding the right API version
@@ -57,6 +58,7 @@ module Version = {
5758
}
5859
| list{"2"} => V2
5960
| list{"3"} => V3
61+
| list{"4"} => V4
6062
| _ => UnknownVersion(apiVersion)
6163
}
6264

@@ -65,6 +67,7 @@ module Version = {
6567
| V1 => "1.0"
6668
| V2 => "2.0"
6769
| V3 => "3.0"
70+
| V4 => "4.0"
6871
| UnknownVersion(version) => version
6972
}
7073

@@ -73,7 +76,7 @@ module Version = {
7376
let availableLanguages = t =>
7477
switch t {
7578
| V1 => [Lang.Reason, Res]
76-
| V2 | V3 => [Lang.Res]
79+
| V2 | V3 | V4 => [Lang.Res]
7780
| UnknownVersion(_) => [Res]
7881
}
7982
}
@@ -348,6 +351,7 @@ module Config = {
348351
module_system: string,
349352
warn_flags: string,
350353
uncurried?: bool,
354+
open_modules?: array<string>,
351355
}
352356
}
353357

@@ -423,6 +427,8 @@ module Compiler = {
423427

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

430+
@send external setOpenModules: (t, array<string>) => bool = "setOpenModules"
431+
426432
let setConfig = (t: t, config: Config.t): unit => {
427433
let moduleSystem = switch config.module_system {
428434
| "nodejs" => #nodejs->Some
@@ -431,6 +437,7 @@ module Compiler = {
431437
}
432438

433439
Belt.Option.forEach(moduleSystem, moduleSystem => t->setModuleSystem(moduleSystem)->ignore)
440+
Belt.Option.forEach(config.open_modules, modules => t->setOpenModules(modules)->ignore)
434441

435442
t->setWarnFlags(config.warn_flags)->ignore
436443
}

src/bindings/RescriptCompilerApi.resi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Version: {
2424
| V1
2525
| V2
2626
| V3
27+
| V4
2728
| UnknownVersion(string)
2829

2930
// Helps finding the right API version
@@ -159,6 +160,7 @@ module Config: {
159160
warn_flags: string,
160161
/** Only available in apiVersion > 3 (= ReScript 11+) */
161162
uncurried?: bool,
163+
open_modules?: array<string>,
162164
}
163165
}
164166

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

199201
let setWarnFlags: (t, string) => bool
202+
let setOpenModules: (t, array<string>) => bool
200203
let setConfig: (t, Config.t) => unit
201204

202205
// General format function

src/common/CompilerManagerHook.res

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ let getLibrariesForVersion = (~version: Semver.t): array<string> => {
145145
libraries
146146
}
147147

148+
let getOpenModules = (~apiVersion: Version.t, ~libraries: array<string>): option<array<string>> =>
149+
switch apiVersion {
150+
| V1 | V2 | V3 | UnknownVersion(_) => None
151+
| V4 => libraries->Belt.Array.some(el => el === "@rescript/core") ? Some(["RescriptCore"]) : None
152+
}
153+
148154
/*
149155
This function loads the compiler, plus a defined set of libraries that are available
150156
on our bs-platform-js-releases channel.
@@ -396,6 +402,7 @@ let useCompilerManager = (
396402
| Ok() =>
397403
let instance = Compiler.make()
398404
let apiVersion = apiVersion->Version.fromString
405+
let open_modules = getOpenModules(~apiVersion, ~libraries)
399406

400407
// Note: The compiler bundle currently defaults to
401408
// commonjs when initiating the compiler, but our playground
@@ -405,6 +412,7 @@ let useCompilerManager = (
405412
let config = {
406413
...instance->Compiler.getConfig,
407414
module_system: "es6",
415+
?open_modules,
408416
}
409417
instance->Compiler.setConfig(config)
410418

@@ -453,7 +461,10 @@ let useCompilerManager = (
453461

454462
let instance = Compiler.make()
455463
let apiVersion = apiVersion->Version.fromString
456-
let config = instance->Compiler.getConfig
464+
let open_modules = getOpenModules(~apiVersion, ~libraries)
465+
466+
let config = {...instance->Compiler.getConfig, ?open_modules}
467+
instance->Compiler.setConfig(config)
457468

458469
let selected = {
459470
id: version,
@@ -488,7 +499,7 @@ let useCompilerManager = (
488499
| Lang.Reason => instance->Compiler.reasonCompile(code)
489500
| Lang.Res => instance->Compiler.resCompile(code)
490501
}
491-
| V2 | V3 =>
502+
| V2 | V3 | V4 =>
492503
switch lang {
493504
| Lang.OCaml => instance->Compiler.ocamlCompile(code)
494505
| Lang.Reason =>

0 commit comments

Comments
 (0)