Skip to content

Commit

Permalink
Guard open modules with api version >= 4
Browse files Browse the repository at this point in the history
  • Loading branch information
fhammerschmidt committed Nov 1, 2023
1 parent b268ff4 commit 931b9f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/Playground.res
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,13 @@ module Settings = {
let onResetClick = evt => {
ReactEvent.Mouse.preventDefault(evt)

let open_modules =
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",
Expand Down
14 changes: 9 additions & 5 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,11 +461,7 @@ let useCompilerManager = (

let instance = Compiler.make()
let apiVersion = apiVersion->Version.fromString

let open_modules =
libraries->Belt.Array.some(el => el === "@rescript/core")
? Some(["RescriptCore"])
: None
let open_modules = getOpenModules(~apiVersion, ~libraries)

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

0 comments on commit 931b9f5

Please sign in to comment.