diff --git a/src/Playground.res b/src/Playground.res index 24b582cfd..4ae260543 100644 --- a/src/Playground.res +++ b/src/Playground.res @@ -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) } diff --git a/src/bindings/RescriptCompilerApi.res b/src/bindings/RescriptCompilerApi.res index faf569599..eef8c3550 100644 --- a/src/bindings/RescriptCompilerApi.res +++ b/src/bindings/RescriptCompilerApi.res @@ -36,6 +36,7 @@ module Version = { | V1 | V2 | V3 + | V4 | UnknownVersion(string) // Helps finding the right API version @@ -57,6 +58,7 @@ module Version = { } | list{"2"} => V2 | list{"3"} => V3 + | list{"4"} => V4 | _ => UnknownVersion(apiVersion) } @@ -65,6 +67,7 @@ module Version = { | V1 => "1.0" | V2 => "2.0" | V3 => "3.0" + | V4 => "4.0" | UnknownVersion(version) => version } @@ -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] } } @@ -348,6 +351,7 @@ module Config = { module_system: string, warn_flags: string, uncurried?: bool, + open_modules?: array, } } @@ -423,6 +427,8 @@ module Compiler = { @send external setWarnFlags: (t, string) => bool = "setWarnFlags" + @send external setOpenModules: (t, array) => bool = "setOpenModules" + let setConfig = (t: t, config: Config.t): unit => { let moduleSystem = switch config.module_system { | "nodejs" => #nodejs->Some @@ -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) t->setWarnFlags(config.warn_flags)->ignore } diff --git a/src/bindings/RescriptCompilerApi.resi b/src/bindings/RescriptCompilerApi.resi index f6742488c..cfb3065aa 100644 --- a/src/bindings/RescriptCompilerApi.resi +++ b/src/bindings/RescriptCompilerApi.resi @@ -24,6 +24,7 @@ module Version: { | V1 | V2 | V3 + | V4 | UnknownVersion(string) // Helps finding the right API version @@ -159,6 +160,7 @@ module Config: { warn_flags: string, /** Only available in apiVersion > 3 (= ReScript 11+) */ uncurried?: bool, + open_modules?: array, } } @@ -197,6 +199,7 @@ module Compiler: { let setModuleSystem: (t, [#es6 | #nodejs]) => bool let setWarnFlags: (t, string) => bool + let setOpenModules: (t, array) => bool let setConfig: (t, Config.t) => unit // General format function diff --git a/src/common/CompilerManagerHook.res b/src/common/CompilerManagerHook.res index 65e745f06..fb50d3cbd 100644 --- a/src/common/CompilerManagerHook.res +++ b/src/common/CompilerManagerHook.res @@ -145,6 +145,12 @@ let getLibrariesForVersion = (~version: Semver.t): array => { libraries } +let getOpenModules = (~apiVersion: Version.t, ~libraries: array): option> => + 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. @@ -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 @@ -405,6 +412,7 @@ let useCompilerManager = ( let config = { ...instance->Compiler.getConfig, module_system: "es6", + ?open_modules, } instance->Compiler.setConfig(config) @@ -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, @@ -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 =>