Skip to content

Commit 0c2bb1a

Browse files
committed
add new configuration for jsx ppx v4
1 parent b7a11a7 commit 0c2bb1a

21 files changed

+4609
-286
lines changed

docs/docson/build-schema.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,19 @@
350350
}
351351
}
352352
},
353+
"react-specs": {
354+
"type": "object",
355+
"properties": {
356+
"jsx": {
357+
"$ref": "#/definitions/jsx-version",
358+
"description": "Whether to apply the [RescriptReact](https://github.com/rescript-lang/rescript-react)-specific JSX PPX transformation."
359+
},
360+
"runtime": {
361+
"$ref": "#/definitions/react-runtime",
362+
"description": "Whether to apply the [RescriptReact](https://github.com/rescript-lang/rescript-react)-specific JSX PPX transformation."
363+
}
364+
}
365+
},
353366
"refmt-specs": {
354367
"oneOf": [
355368
{

jscomp/bsb/bsb_build_schemas.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ let reason = "reason"
9090

9191
let react_jsx = "react-jsx"
9292

93+
let react = "react"
94+
95+
let jsx_version = "jsx"
96+
97+
let react_runtime = "runtime"
98+
9399
let entries = "entries"
94100

95101
let backend = "backend"

jscomp/bsb/bsb_config_parse.ml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ type json_map = Ext_json_types.t Map_string.t
3636

3737
(* Key is the path *)
3838
let ( |? ) m (key, cb) = m |> Ext_json.test key cb
39-
4039
let ( .?() ) = Map_string.find_opt
4140

4241
(*TODO: it is a little mess that [cwd] and [project dir] are shared*)
@@ -146,6 +145,46 @@ let extract_reason_react_jsx (map : json_map) =
146145
|> ignore;
147146
!default
148147

148+
let extract_react_jsx (map : json_map) =
149+
let default : Bsb_config_types.react_jsx option ref = ref None in
150+
map
151+
|? ( Bsb_build_schemas.react,
152+
`Obj
153+
(fun m ->
154+
match m.?(Bsb_build_schemas.jsx_version) with
155+
| Some (Flo { loc; flo }) -> (
156+
match flo with
157+
| "3" -> default := Some Jsx_v3
158+
| "4" -> default := Some Jsx_v4
159+
| _ -> Bsb_exception.errorf ~loc "Unsupported react-jsx %s" flo)
160+
| Some x ->
161+
Bsb_exception.config_error x
162+
"Unexpected input (expect a version number) for jsx"
163+
| None -> ()) )
164+
|> ignore;
165+
!default
166+
167+
let extract_react_runtime (map : json_map) =
168+
let default : Bsb_config_types.react_runtime option ref = ref None in
169+
map
170+
|? ( Bsb_build_schemas.react,
171+
`Obj
172+
(fun m ->
173+
match m.?(Bsb_build_schemas.react_runtime) with
174+
| Some (Str { loc; str }) -> (
175+
match str with
176+
| "classic" -> default := Some Classic
177+
| "automatic" -> default := Some Automatic
178+
| _ ->
179+
Bsb_exception.errorf ~loc "Unsupported react-runtime %s" str)
180+
| Some x ->
181+
Bsb_exception.config_error x
182+
"Unexpected input (expect classic or automatic) for \
183+
react-runtime"
184+
| None -> ()) )
185+
|> ignore;
186+
!default
187+
149188
let extract_warning (map : json_map) =
150189
match map.?(Bsb_build_schemas.warnings) with
151190
| None -> Bsb_warning.use_default
@@ -293,6 +332,8 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
293332
.path)
294333
in
295334
let reason_react_jsx = extract_reason_react_jsx map in
335+
let react_jsx = extract_react_jsx map in
336+
let react_runtime = extract_react_runtime map in
296337
let bs_dependencies =
297338
extract_dependencies map per_proj_dir Bsb_build_schemas.bs_dependencies
298339
in
@@ -349,6 +390,8 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
349390
generate_merlin =
350391
extract_boolean map Bsb_build_schemas.generate_merlin false;
351392
reason_react_jsx;
393+
react_jsx;
394+
react_runtime;
352395
generators = extract_generators map;
353396
cut_generators;
354397
}

jscomp/bsb/bsb_config_types.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ type dependency = {
2828
}
2929

3030
type dependencies = dependency list
31-
3231
type reason_react_jsx = Jsx_v3
3332
(* string option *)
3433

34+
type react_jsx = Jsx_v3 | Jsx_v4
35+
type react_runtime = Classic | Automatic
3536
type gentype_config = { path : string (* resolved *) }
36-
3737
type command = string
38-
3938
type ppx = { name : string; args : string list }
4039

4140
type t = {
@@ -62,6 +61,8 @@ type t = {
6261
files_to_install : Bsb_db.module_info Queue.t;
6362
generate_merlin : bool;
6463
reason_react_jsx : reason_react_jsx option;
64+
react_jsx : react_jsx option;
65+
react_runtime : react_runtime option;
6566
(* whether apply PPX transform or not*)
6667
generators : command Map_string.t;
6768
cut_generators : bool;

jscomp/bsb/bsb_merlin_gen.ml

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
let merlin = ".merlin"
26-
2726
let merlin_header = "####{BSB GENERATED: NO EDIT"
28-
2927
let merlin_trailer = "####BSB GENERATED: NO EDIT}"
30-
3128
let merlin_trailer_length = String.length merlin_trailer
32-
3329
let ( // ) = Ext_path.combine
3430

3531
(** [new_content] should start end finish with newline *)
@@ -75,15 +71,10 @@ let revise_merlin merlin new_content =
7571
merlin generation
7672
*)
7773
let merlin_flg_ppx = "\nFLG -ppx "
78-
7974
let merlin_flg_pp = "\nFLG -pp "
80-
8175
let merlin_s = "\nS "
82-
8376
let merlin_b = "\nB "
84-
8577
let merlin_flg = "\nFLG "
86-
8778
let bs_flg_prefix = "-bs-"
8879

8980
let output_merlin_namespace buffer ns =
@@ -128,6 +119,8 @@ let merlin_file_gen ~(per_proj_dir : string)
128119
built_in_dependency;
129120
external_includes;
130121
reason_react_jsx;
122+
react_jsx;
123+
react_runtime;
131124
namespace;
132125
package_name = _;
133126
warning;
@@ -149,21 +142,37 @@ let merlin_file_gen ~(per_proj_dir : string)
149142
Buffer.add_string buffer (merlin_flg_pp ^ x));
150143
Buffer.add_string buffer
151144
(merlin_flg_ppx
145+
^ (match (reason_react_jsx, react_jsx) with
146+
| None, None ->
147+
let fmt : _ format =
148+
if Ext_sys.is_windows_or_cygwin then "\"%s -as-ppx \""
149+
else "'%s -as-ppx '"
150+
in
151+
Printf.sprintf fmt Bsb_global_paths.vendor_bsc
152+
| Some opt, None ->
153+
let fmt : _ format =
154+
if Ext_sys.is_windows_or_cygwin then "\"%s -as-ppx -bs-jsx %d\""
155+
else "'%s -as-ppx -bs-jsx %d'"
156+
in
157+
Printf.sprintf fmt Bsb_global_paths.vendor_bsc
158+
(match opt with Jsx_v3 -> 3)
159+
| _, Some opt ->
160+
let fmt : _ format =
161+
if Ext_sys.is_windows_or_cygwin then "\"%s -as-ppx -bs-jsx %d\""
162+
else "'%s -as-ppx -bs-jsx %d'"
163+
in
164+
Printf.sprintf fmt Bsb_global_paths.vendor_bsc
165+
(match opt with Jsx_v3 -> 3 | Jsx_v4 -> 4))
152166
^
153-
match reason_react_jsx with
154-
| None ->
155-
let fmt : _ format =
156-
if Ext_sys.is_windows_or_cygwin then "\"%s -as-ppx \""
157-
else "'%s -as-ppx '"
158-
in
159-
Printf.sprintf fmt Bsb_global_paths.vendor_bsc
167+
match react_runtime with
168+
| None -> ""
160169
| Some opt ->
161170
let fmt : _ format =
162-
if Ext_sys.is_windows_or_cygwin then "\"%s -as-ppx -bs-jsx %d\""
163-
else "'%s -as-ppx -bs-jsx %d'"
171+
if Ext_sys.is_windows_or_cygwin then "\"-bs-react-runtime %s\""
172+
else "'-bs-react-runtime %s'"
164173
in
165-
Printf.sprintf fmt Bsb_global_paths.vendor_bsc
166-
(match opt with Jsx_v3 -> 3));
174+
Printf.sprintf fmt
175+
(match opt with Classic -> "classic" | Automatic -> "automatic"));
167176
Ext_list.iter external_includes (fun path ->
168177
Buffer.add_string buffer merlin_s;
169178
Buffer.add_string buffer path;

jscomp/bsb/bsb_ninja_gen.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ let output_ninja_and_namespace_map ~per_proj_dir ~package_kind
160160
files_to_install;
161161
built_in_dependency;
162162
reason_react_jsx;
163+
react_jsx;
164+
react_runtime;
163165
generators;
164166
namespace;
165167
warning;
@@ -206,9 +208,11 @@ let output_ninja_and_namespace_map ~per_proj_dir ~package_kind
206208
let rules : Bsb_ninja_rule.builtin =
207209
Bsb_ninja_rule.make_custom_rules ~gentype_config
208210
~has_postbuild:js_post_build_cmd ~pp_file ~has_builtin:built_in_dependency
209-
~reason_react_jsx ~package_specs ~namespace ~digest ~package_name
210-
~warnings ~ppx_files ~bsc_flags ~dpkg_incls (* dev dependencies *)
211-
~lib_incls (* its own libs *) ~dev_incls (* its own devs *)
211+
~reason_react_jsx ~react_jsx ~react_runtime ~package_specs ~namespace
212+
~digest ~package_name ~warnings ~ppx_files ~bsc_flags
213+
~dpkg_incls (* dev dependencies *)
214+
~lib_incls (* its own libs *)
215+
~dev_incls (* its own devs *)
212216
~bs_dependencies ~bs_dev_dependencies generators
213217
in
214218

jscomp/bsb/bsb_ninja_rule.ml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config option)
9191
~(has_postbuild : string option) ~(pp_file : string option)
9292
~(has_builtin : bool)
9393
~(reason_react_jsx : Bsb_config_types.reason_react_jsx option)
94-
~(digest : string) ~(package_specs : Bsb_package_specs.t)
95-
~(namespace : string option) ~package_name ~warnings
96-
~(ppx_files : Bsb_config_types.ppx list) ~bsc_flags ~(dpkg_incls : string)
97-
~(lib_incls : string) ~(dev_incls : string) ~bs_dependencies
98-
~bs_dev_dependencies (custom_rules : command Map_string.t) : builtin =
94+
~(react_jsx : Bsb_config_types.react_jsx option)
95+
~(react_runtime : Bsb_config_types.react_runtime option) ~(digest : string)
96+
~(package_specs : Bsb_package_specs.t) ~(namespace : string option)
97+
~package_name ~warnings ~(ppx_files : Bsb_config_types.ppx list) ~bsc_flags
98+
~(dpkg_incls : string) ~(lib_incls : string) ~(dev_incls : string)
99+
~bs_dependencies ~bs_dev_dependencies (custom_rules : command Map_string.t)
100+
: builtin =
99101
let bs_dep = Ext_filename.maybe_quote Bsb_global_paths.vendor_bsdep in
100102
let bsc = Ext_filename.maybe_quote Bsb_global_paths.vendor_bsc in
101103
(* FIXME: We don't need set [-o ${out}] when building ast
@@ -164,6 +166,14 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config option)
164166
(match (has_reason_react_jsx, reason_react_jsx) with
165167
| false, _ | _, None -> ()
166168
| _, Some Jsx_v3 -> Ext_buffer.add_string buf " -bs-jsx 3");
169+
(match react_jsx with
170+
| None -> ()
171+
| Some Jsx_v3 -> Ext_buffer.add_string buf " -bs-jsx 3"
172+
| Some Jsx_v4 -> Ext_buffer.add_string buf " -bs-jsx 4");
173+
(match react_runtime with
174+
| None -> ()
175+
| Some Classic -> Ext_buffer.add_string buf " -bs-react-runtime classic"
176+
| Some Automatic -> Ext_buffer.add_string buf " -bs-react-runtime automatic");
167177

168178
Ext_buffer.add_char_string buf ' ' bsc_flags;
169179
Ext_buffer.add_string buf " -absname -bs-ast -o $out $i";

jscomp/bsb/bsb_ninja_rule.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ val make_custom_rules :
7171
pp_file:string option ->
7272
has_builtin:bool ->
7373
reason_react_jsx:Bsb_config_types.reason_react_jsx option ->
74+
react_jsx:Bsb_config_types.react_jsx option ->
75+
react_runtime:Bsb_config_types.react_runtime option ->
7476
digest:string ->
7577
package_specs:Bsb_package_specs.t ->
7678
namespace:string option ->

jscomp/common/js_config.ml

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,79 +23,44 @@
2323
* along with this program; if not, write to the Free Software
2424
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2525

26-
27-
28-
29-
30-
31-
3226
(** Browser is not set via command line only for internal use *)
3327

34-
3528
let no_version_header = ref false
36-
3729
let cross_module_inline = ref false
38-
39-
40-
4130
let diagnose = ref false
42-
let get_diagnose () =
31+
32+
let get_diagnose () =
4333
!diagnose
44-
45-
# 44 "common/js_config.pp.ml"
46-
|| Sys.getenv_opt "RES_DEBUG_FILE" <> None
34+
35+
# 34 "common/js_config.pp.ml"
36+
|| Sys.getenv_opt "RES_DEBUG_FILE" <> None
4737

48-
# 47 "common/js_config.pp.ml"
38+
# 37 "common/js_config.pp.ml"
4939
(* let (//) = Filename.concat *)
5040

5141
(* let get_packages_info () = !packages_info *)
5242

5343
let no_builtin_ppx = ref false
54-
55-
56-
57-
5844
let tool_name = "ReScript"
59-
6045
let check_div_by_zero = ref true
6146
let get_check_div_by_zero () = !check_div_by_zero
62-
63-
64-
6547
let syntax_only = ref false
6648
let binary_ast = ref false
67-
68-
69-
7049
let debug = ref false
71-
72-
let cmi_only = ref false
50+
let cmi_only = ref false
7351
let cmj_only = ref false
74-
7552
let force_cmi = ref false
7653
let force_cmj = ref false
77-
7854
let jsx_version = ref (-1)
79-
80-
55+
let react_runtime = ref "classic"
8156
let js_stdout = ref true
82-
8357
let all_module_aliases = ref false
84-
8558
let no_stdlib = ref false
86-
8759
let no_export = ref false
88-
89-
90-
9160
let as_ppx = ref false
9261

93-
9462
(* option to config `@rescript/std`*)
95-
let customize_runtime : string option ref = ref None
96-
63+
let customize_runtime : string option ref = ref None
9764
let as_pp = ref false
98-
9965
let self_stack : string Stack.t = Stack.create ()
100-
10166
let modules = ref false

jscomp/common/js_config.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ val force_cmj : bool ref
7575

7676
val jsx_version : int ref
7777

78+
val react_runtime: string ref
79+
7880
val js_stdout : bool ref
7981

8082
val all_module_aliases : bool ref

0 commit comments

Comments
 (0)