Skip to content

Commit 8902a27

Browse files
Playground: Add config options for experimental features and jsx preserve mode (#7865)
1 parent 05c10e3 commit 8902a27

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

compiler/jsoo/jsoo_playground_main.ml

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
`config.uncurried` to the BundleConfig.
5050
* v4: Added `config.open_modules` to the BundleConfig to enable implicitly opened
5151
* modules in the playground.
52+
* v5: Removed .ml support.
53+
* v6: Added `config.experimental_features` and `config.jsx_preserve_mode` to the BundleConfig.
5254
* *)
53-
let api_version = "5"
55+
let api_version = "6"
5456

5557
module Js = Js_of_ocaml.Js
5658

@@ -73,6 +75,8 @@ module BundleConfig = struct
7375
mutable filename: string option;
7476
mutable warn_flags: string;
7577
mutable open_modules: string list;
78+
mutable experimental_features: string list;
79+
mutable jsx_preserve_mode: bool;
7680
}
7781

7882
let make () =
@@ -81,6 +85,8 @@ module BundleConfig = struct
8185
filename = None;
8286
warn_flags = Bsc_warnings.defaults_w;
8387
open_modules = [];
88+
experimental_features = [];
89+
jsx_preserve_mode = false;
8490
}
8591

8692
let default_filename (lang : Lang.t) = "playground." ^ Lang.to_string lang
@@ -467,7 +473,15 @@ module Compile = struct
467473
Js.array (!acc |> Array.of_list)
468474

469475
let implementation ~(config : BundleConfig.t) ~lang str =
470-
let {BundleConfig.module_system; warn_flags; open_modules} = config in
476+
let {
477+
BundleConfig.module_system;
478+
warn_flags;
479+
open_modules;
480+
experimental_features;
481+
jsx_preserve_mode;
482+
} =
483+
config
484+
in
471485
try
472486
reset_compiler ();
473487
Warnings.parse_options false warn_flags;
@@ -485,6 +499,9 @@ module Compile = struct
485499
(* let finalenv = ref Env.empty in *)
486500
let types_signature = ref [] in
487501
Js_config.jsx_version := Some Js_config.Jsx_v4;
502+
Js_config.jsx_preserve := jsx_preserve_mode;
503+
experimental_features
504+
|> List.iter Experimental_features.enable_from_string;
488505
(* default *)
489506
let ast = impl str in
490507
let ast = Ppx_entry.rewrite_implementation ast in
@@ -606,6 +623,14 @@ module Export = struct
606623
config.open_modules <- value;
607624
true
608625
in
626+
let set_experimental_features value =
627+
config.experimental_features <- value;
628+
true
629+
in
630+
let set_jsx_preserve_mode value =
631+
config.jsx_preserve_mode <- value;
632+
true
633+
in
609634
let convert_syntax ~(from_lang : string) ~(to_lang : string) (src : string)
610635
=
611636
let open Lang in
@@ -653,6 +678,17 @@ module Export = struct
653678
(set_open_modules
654679
(value |> Js.to_array |> Array.map Js.to_string
655680
|> Array.to_list))) );
681+
( "setExperimentalFeatures",
682+
inject
683+
@@ Js.wrap_meth_callback (fun _ value ->
684+
Js.bool
685+
(set_experimental_features
686+
(value |> Js.to_array |> Array.map Js.to_string
687+
|> Array.to_list))) );
688+
( "setJsxPreserveMode",
689+
inject
690+
@@ Js.wrap_meth_callback (fun _ value ->
691+
Js.bool (set_jsx_preserve_mode (Js.to_bool value))) );
656692
( "getConfig",
657693
inject
658694
@@ Js.wrap_meth_callback (fun _ ->
@@ -665,6 +701,12 @@ module Export = struct
665701
|> BundleConfig.string_of_module_system
666702
|> Js.string) );
667703
("warn_flags", inject @@ Js.string config.warn_flags);
704+
( "jsx_preserve_mode",
705+
inject @@ (config.jsx_preserve_mode |> Js.bool) );
706+
( "experimental_features",
707+
inject
708+
@@ (config.experimental_features |> Array.of_list
709+
|> Js.array) );
668710
( "open_modules",
669711
inject
670712
@@ (config.open_modules |> Array.of_list |> Js.array)

packages/playground/playground_test.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ require("./packages/@rescript/react/cmij.js");
77

88
const compiler = rescript_compiler.make();
99

10+
console.log("Initial compiler config: ", compiler.getConfig());
11+
12+
compiler.setExperimentalFeatures(["LetUnwrap"]);
13+
compiler.setJsxPreserveMode(true);
14+
15+
console.log("Current compiler config: ", compiler.getConfig());
16+
1017
const result = compiler.rescript.compile(`
1118
@@jsxConfig({ version: 4, mode: "automatic" })
1219

0 commit comments

Comments
 (0)