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

Playground: implicitly opened modules #6446

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Allow coercing unboxed variants with only strings (now including with a single payload of string) to the primitive string. https://github.com/rescript-lang/rescript-compiler/pull/6441
- Allow coercing strings to unboxed variants that has a catch-all unboxed string case. https://github.com/rescript-lang/rescript-compiler/pull/6443
- Allow coercing `int` to `float`. https://github.com/rescript-lang/rescript-compiler/pull/6448
- Playground: Add support for implicitly opened modules. https://github.com/rescript-lang/rescript-compiler/pull/6446

#### :bug: Bug Fix

Expand Down
19 changes: 17 additions & 2 deletions jscomp/jsoo/jsoo_playground_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
* v3: Switched to Uncurried mode by default (requires third party packages
to be built with uncurried: true in bsconfig.json). Also added
`config.uncurried` to the BundleConfig.
* v4: Added `config.open_modules` to the BundleConfig to enable implicitly opened
* modules in the playground.
* *)
let apiVersion = "3"
let apiVersion = "4"

module Js = Js_of_ocaml.Js

Expand All @@ -75,6 +77,7 @@ module BundleConfig = struct
mutable module_system: Js_packages_info.module_system;
mutable filename: string option;
mutable warn_flags: string;
mutable open_modules: string list;

(* This one can't be mutated since we only provide
third-party packages that were compiled for uncurried
Expand All @@ -86,6 +89,7 @@ module BundleConfig = struct
module_system=Js_packages_info.NodeJS;
filename=None;
warn_flags=Bsc_warnings.defaults_w;
open_modules=[];
uncurried=(!Config.uncurried = Uncurried);
}

Expand Down Expand Up @@ -462,7 +466,7 @@ module Compile = struct
Js.array (!acc |> Array.of_list)

let implementation ~(config: BundleConfig.t) ~lang str =
let {BundleConfig.module_system; warn_flags} = config in
let {BundleConfig.module_system; warn_flags; open_modules} = config in
try
reset_compiler ();
Warnings.parse_options false warn_flags;
Expand All @@ -472,6 +476,7 @@ module Compile = struct
| Lang.OCaml -> ocaml_parse ~filename
| Res -> rescript_parse ~filename
in
Clflags.open_modules := open_modules;
(* let env = !Toploop.toplevel_env in *)
(* Res_compmisc.init_path (); *)
(* let modulename = module_of_filename ppf sourcefile outputprefix in *)
Expand Down Expand Up @@ -613,6 +618,9 @@ module Export = struct
let set_warn_flags value =
config.warn_flags <- value; true
in
let set_open_modules value =
config.open_modules <- value; true
in
let convert_syntax ~(fromLang: string) ~(toLang: string) (src: string) =
let open Lang in
match (fromString fromLang, fromString toLang) with
Expand Down Expand Up @@ -658,6 +666,12 @@ module Export = struct
(fun _ value ->
(Js.bool (set_warn_flags (Js.to_string value)))
);
"setOpenModules",
inject @@
Js.wrap_meth_callback
(fun _ (value) ->
(Js.bool (set_open_modules (value |> Js.to_array |> Array.map Js.to_string |> Array.to_list)))
);
"getConfig",
inject @@
Js.wrap_meth_callback
Expand All @@ -673,6 +687,7 @@ module Export = struct
"warn_flags",
inject @@ (Js.string config.warn_flags);
"uncurried", inject @@ (Js.bool config.uncurried);
"open_modules", inject @@ (config.open_modules |> Array.of_list |> Js.array);
|]))
);
|])
Expand Down
4 changes: 2 additions & 2 deletions playground/playground_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ require("./packages/@rescript/core/cmij.js")

let compiler = rescript_compiler.make()

compiler.setOpenModules(["RescriptCore"])

let result = compiler.rescript.compile(`
@@jsxConfig({ version: 4, mode: "automatic" })

open RescriptCore

module A = {
@react.component
let make = (~a) => {
Expand Down