Skip to content

Commit 7b1362b

Browse files
committed
Clean up jsx config.
1 parent 5b61285 commit 7b1362b

8 files changed

+1175
-1181
lines changed

jscomp/bsb/bsb_config_parse.ml

+2-53
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ let check_stdlib (map : json_map) : bool =
8888
| Some (False _) -> false
8989
| None | Some _ -> true
9090

91-
let extract_gentype_config (map : json_map) :
92-
Bsb_config_types.gentype_config =
91+
let extract_gentype_config (map : json_map) : Bsb_config_types.gentype_config =
9392
match map.?(Bsb_build_schemas.gentypeconfig) with
9493
| None -> false
9594
| Some (Obj _) -> true
@@ -130,53 +129,6 @@ let extract_reason_react_jsx (map : json_map) =
130129
|> ignore;
131130
!default
132131

133-
let extract_jsx (map : json_map) =
134-
let version : Bsb_config_types.jsx_version option ref = ref None in
135-
let module_ : Bsb_config_types.jsx_module option ref = ref None in
136-
let mode : Bsb_config_types.jsx_mode option ref = ref None in
137-
map
138-
|? ( Bsb_build_schemas.jsx,
139-
`Obj
140-
(fun m ->
141-
match m.?(Bsb_build_schemas.jsx_version) with
142-
| Some (Flo { loc; flo }) -> (
143-
match flo with
144-
| "3" -> version := Some Jsx_v3
145-
| "4" -> version := Some Jsx_v4
146-
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-version %s" flo
147-
)
148-
| Some x ->
149-
Bsb_exception.config_error x
150-
"Unexpected input (expect a version number) for jsx-version"
151-
| None -> ()) )
152-
|? ( Bsb_build_schemas.jsx,
153-
`Obj
154-
(fun m ->
155-
match m.?(Bsb_build_schemas.jsx_module) with
156-
| Some (Str { loc; str }) -> (
157-
match str with
158-
| "react" -> module_ := Some React
159-
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-module %s" str)
160-
| Some x ->
161-
Bsb_exception.config_error x
162-
"Unexpected input (jsx module name) for jsx-mode"
163-
| None -> ()) )
164-
|? ( Bsb_build_schemas.jsx,
165-
`Obj
166-
(fun m ->
167-
match m.?(Bsb_build_schemas.jsx_mode) with
168-
| Some (Str { loc; str }) -> (
169-
match str with
170-
| "classic" -> mode := Some Classic
171-
| "automatic" -> mode := Some Automatic
172-
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-mode %s" str)
173-
| Some x ->
174-
Bsb_exception.config_error x
175-
"Unexpected input (expect classic or automatic) for jsx-mode"
176-
| None -> ()) )
177-
|> ignore;
178-
(!version, !module_, !mode)
179-
180132
let extract_warning (map : json_map) =
181133
match map.?(Bsb_build_schemas.warnings) with
182134
| None -> Bsb_warning.use_default
@@ -324,7 +276,6 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
324276
.path)
325277
in
326278
let reason_react_jsx = extract_reason_react_jsx map in
327-
let jsx_version, jsx_module, jsx_mode = extract_jsx map in
328279
let bs_dependencies =
329280
extract_dependencies map per_proj_dir Bsb_build_schemas.bs_dependencies
330281
in
@@ -381,9 +332,7 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
381332
generate_merlin =
382333
extract_boolean map Bsb_build_schemas.generate_merlin false;
383334
reason_react_jsx;
384-
jsx_version;
385-
jsx_module;
386-
jsx_mode;
335+
jsx = Bsb_jsx.from_map map;
387336
generators = extract_generators map;
388337
cut_generators;
389338
}

jscomp/bsb/bsb_config_types.ml

+1-6
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ type dependencies = dependency list
3131
type reason_react_jsx = Jsx_v3
3232
(* string option *)
3333

34-
type jsx_version = Jsx_v3 | Jsx_v4
35-
type jsx_module = React
36-
type jsx_mode = Classic | Automatic
3734
type gentype_config = bool
3835
type command = string
3936
type ppx = { name : string; args : string list }
@@ -62,9 +59,7 @@ type t = {
6259
files_to_install : Bsb_db.module_info Queue.t;
6360
generate_merlin : bool;
6461
reason_react_jsx : reason_react_jsx option;
65-
jsx_version : jsx_version option;
66-
jsx_module : jsx_module option;
67-
jsx_mode : jsx_mode option;
62+
jsx: Bsb_jsx.t;
6863
(* whether apply PPX transform or not*)
6964
generators : command Map_string.t;
7065
cut_generators : bool;

jscomp/bsb/bsb_jsx.ml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
type version = Jsx_v3 | Jsx_v4
2+
type module_ = React
3+
type mode = Classic | Automatic
4+
5+
type t = {
6+
version : version option;
7+
module_ : module_ option;
8+
mode : mode option;
9+
}
10+
11+
let ( .?() ) = Map_string.find_opt
12+
let ( |? ) m (key, cb) = m |> Ext_json.test key cb
13+
14+
let from_map map =
15+
let version : version option ref = ref None in
16+
let module_ : module_ option ref = ref None in
17+
let mode : mode option ref = ref None in
18+
map
19+
|? ( Bsb_build_schemas.jsx,
20+
`Obj
21+
(fun m ->
22+
match m.?(Bsb_build_schemas.jsx_version) with
23+
| Some (Flo { loc; flo }) -> (
24+
match flo with
25+
| "3" -> version := Some Jsx_v3
26+
| "4" -> version := Some Jsx_v4
27+
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-version %s" flo
28+
)
29+
| Some x ->
30+
Bsb_exception.config_error x
31+
"Unexpected input (expect a version number) for jsx-version"
32+
| None -> ()) )
33+
|? ( Bsb_build_schemas.jsx,
34+
`Obj
35+
(fun m ->
36+
match m.?(Bsb_build_schemas.jsx_module) with
37+
| Some (Str { loc; str }) -> (
38+
match str with
39+
| "react" -> module_ := Some React
40+
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-module %s" str)
41+
| Some x ->
42+
Bsb_exception.config_error x
43+
"Unexpected input (jsx module name) for jsx-mode"
44+
| None -> ()) )
45+
|? ( Bsb_build_schemas.jsx,
46+
`Obj
47+
(fun m ->
48+
match m.?(Bsb_build_schemas.jsx_mode) with
49+
| Some (Str { loc; str }) -> (
50+
match str with
51+
| "classic" -> mode := Some Classic
52+
| "automatic" -> mode := Some Automatic
53+
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-mode %s" str)
54+
| Some x ->
55+
Bsb_exception.config_error x
56+
"Unexpected input (expect classic or automatic) for jsx-mode"
57+
| None -> ()) )
58+
|> ignore;
59+
{ version = !version; module_ = !module_; mode = !mode }

jscomp/bsb/bsb_ninja_gen.ml

+3-6
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ let output_ninja_and_namespace_map ~per_proj_dir ~package_kind
160160
files_to_install;
161161
built_in_dependency;
162162
reason_react_jsx;
163-
jsx_version;
164-
jsx_module;
165-
jsx_mode;
163+
jsx;
166164
generators;
167165
namespace;
168166
warning;
@@ -209,9 +207,8 @@ let output_ninja_and_namespace_map ~per_proj_dir ~package_kind
209207
let rules : Bsb_ninja_rule.builtin =
210208
Bsb_ninja_rule.make_custom_rules ~gentype_config
211209
~has_postbuild:js_post_build_cmd ~pp_file ~has_builtin:built_in_dependency
212-
~reason_react_jsx ~jsx_version ~jsx_module ~jsx_mode ~package_specs
213-
~namespace ~digest ~package_name ~warnings ~ppx_files ~bsc_flags
214-
~dpkg_incls (* dev dependencies *)
210+
~reason_react_jsx ~jsx ~package_specs ~namespace ~digest ~package_name
211+
~warnings ~ppx_files ~bsc_flags ~dpkg_incls (* dev dependencies *)
215212
~lib_incls (* its own libs *)
216213
~dev_incls (* its own devs *)
217214
~bs_dependencies ~bs_dev_dependencies generators

jscomp/bsb/bsb_ninja_rule.ml

+9-13
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,11 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
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-
~(jsx_version : Bsb_config_types.jsx_version option)
95-
~(jsx_module : Bsb_config_types.jsx_module option)
96-
~(jsx_mode : Bsb_config_types.jsx_mode option) ~(digest : string)
97-
~(package_specs : Bsb_package_specs.t) ~(namespace : string option)
98-
~package_name ~warnings ~(ppx_files : Bsb_config_types.ppx list) ~bsc_flags
99-
~(dpkg_incls : string) ~(lib_incls : string) ~(dev_incls : string)
100-
~bs_dependencies ~bs_dev_dependencies (custom_rules : command Map_string.t)
101-
: builtin =
94+
~(jsx : Bsb_jsx.t) ~(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 =
10299
let bs_dep = Ext_filename.maybe_quote Bsb_global_paths.vendor_bsdep in
103100
let bsc = Ext_filename.maybe_quote Bsb_global_paths.vendor_bsc in
104101
(* FIXME: We don't need set [-o ${out}] when building ast
@@ -123,8 +120,7 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
123120
*)
124121
(match gentype_config with
125122
| false -> ()
126-
| true ->
127-
Ext_buffer.add_string buf " -bs-gentype");
123+
| true -> Ext_buffer.add_string buf " -bs-gentype");
128124
if read_cmi <> `is_cmi then (
129125
Ext_buffer.add_string buf " -bs-package-name ";
130126
Ext_buffer.add_string buf (Ext_filename.maybe_quote package_name);
@@ -163,15 +159,15 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
163159
| None -> ()
164160
| Some flag ->
165161
Ext_buffer.add_char_string buf ' ' (Bsb_build_util.pp_flag flag));
166-
(match (reason_react_jsx, jsx_version) with
162+
(match (reason_react_jsx, jsx.version) with
167163
| _, Some Jsx_v3 -> Ext_buffer.add_string buf " -bs-jsx 3"
168164
| _, Some Jsx_v4 -> Ext_buffer.add_string buf " -bs-jsx 4"
169165
| Some Jsx_v3, None -> Ext_buffer.add_string buf " -bs-jsx 3"
170166
| None, None -> ());
171-
(match jsx_module with
167+
(match jsx.module_ with
172168
| None -> ()
173169
| Some React -> Ext_buffer.add_string buf " -bs-jsx-module react");
174-
(match jsx_mode with
170+
(match jsx.mode with
175171
| None -> ()
176172
| Some Classic -> Ext_buffer.add_string buf " -bs-jsx-mode classic"
177173
| Some Automatic -> Ext_buffer.add_string buf " -bs-jsx-mode automatic");

jscomp/bsb/bsb_ninja_rule.mli

+1-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ 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-
jsx_version:Bsb_config_types.jsx_version option ->
75-
jsx_module:Bsb_config_types.jsx_module option ->
76-
jsx_mode:Bsb_config_types.jsx_mode option ->
74+
jsx:Bsb_jsx.t ->
7775
digest:string ->
7876
package_specs:Bsb_package_specs.t ->
7977
namespace:string option ->

0 commit comments

Comments
 (0)