-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathbsb_jsx.ml
95 lines (89 loc) · 3.11 KB
/
bsb_jsx.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
type version = Jsx_v3 | Jsx_v4
type module_ = React | Generic of {moduleName: string}
type mode = Classic | Automatic
type dependencies = string list
type t = {
version : version option;
module_ : module_ option;
mode : mode option;
v3_dependencies : dependencies;
}
let encode_no_nl jsx =
(match jsx.version with
| None -> ""
| Some Jsx_v3 -> "3"
| Some Jsx_v4 -> "4")
^ (match jsx.module_ with None -> "" | Some React -> "React" | Some Generic {moduleName} -> moduleName)
^
match jsx.mode with
| None -> ""
| Some Classic -> "Classic"
| Some Automatic -> "Automatic"
let ( .?() ) = Map_string.find_opt
let ( |? ) m (key, cb) = m |> Ext_json.test key cb
let get_list_string_acc (s : Ext_json_types.t array) acc =
Ext_array.to_list_map_acc s acc (fun x ->
match x with Str x -> Some x.str | _ -> None)
let get_list_string s = get_list_string_acc s []
let from_map map =
let version : version option ref = ref None in
let module_ : module_ option ref = ref None in
let mode : mode option ref = ref None in
let v3_dependencies : dependencies ref = ref [] in
map
|? ( Bsb_build_schemas.jsx,
`Obj
(fun m ->
match m.?(Bsb_build_schemas.jsx_version) with
| Some (Flo { loc; flo }) -> (
match flo with
| "3" -> version := Some Jsx_v3
| "4" -> version := Some Jsx_v4
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx version %s" flo
)
| Some x ->
Bsb_exception.config_error x
"Unexpected input (expect a version number) for jsx version"
| None -> ()) )
|? ( Bsb_build_schemas.jsx,
`Obj
(fun m ->
match m.?(Bsb_build_schemas.jsx_module) with
| Some (Str { str }) -> (
match str with
| "react" -> module_ := Some React
| moduleName -> module_ := Some (Generic {moduleName}))
| Some x ->
Bsb_exception.config_error x
"Unexpected input (jsx module name) for jsx module"
| None -> ()) )
|? ( Bsb_build_schemas.jsx,
`Obj
(fun m ->
match m.?(Bsb_build_schemas.jsx_mode) with
| Some (Str { loc; str }) -> (
match str with
| "classic" -> mode := Some Classic
| "automatic" -> mode := Some Automatic
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx mode %s" str)
| Some x ->
Bsb_exception.config_error x
"Unexpected input (expect classic or automatic) for jsx mode"
| None -> ()) )
|? ( Bsb_build_schemas.jsx,
`Obj
(fun m ->
match m.?(Bsb_build_schemas.jsx_v3_dependencies) with
| Some (Arr { content }) ->
v3_dependencies := get_list_string content
| Some x ->
Bsb_exception.config_error x
"Unexpected input for jsx v3-dependencies"
| None -> ()) )
|> ignore;
{
version = !version;
module_ = !module_;
mode = !mode;
v3_dependencies = !v3_dependencies;
}