Skip to content

Commit 80915d8

Browse files
committed
extract gentype config parsing
1 parent b34705d commit 80915d8

File tree

4 files changed

+85
-11
lines changed

4 files changed

+85
-11
lines changed

jscomp/bsb/bsb_build_schemas.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ let gentype_module = "module"
8181
let gentype_module_resolution = "moduleResolution"
8282
let gentype_export_interfaces = "exportInterfaces"
8383
let gentype_generated_file_extension = "moduleResolution"
84+
let gentype_shims = "shims"
85+
let gentype_debug = "debug"
8486
let path = "path"
8587
let ignored_dirs = "ignored-dirs"
8688

jscomp/bsb/bsb_manifest.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ let parse ~(filename : string) ~(json : Ext_json_types.t) : Bsb_manifest_types.t
3232
let open Bsb_manifest_fields in
3333
let package_name, namespace = extract_package_name_and_namespace map in
3434
let suffix = extract_suffix map in
35+
let package_specs = extract_package_specs map ~suffix in
3536
{
3637
package_name;
3738
namespace;
39+
suffix;
40+
package_specs;
3841
warning = extract_warning map;
3942
external_includes = extract_string_list map Bsb_build_schemas.bs_external_includes;
4043
bsc_flags = extract_string_list map Bsb_build_schemas.bsc_flags;
@@ -46,14 +49,13 @@ let parse ~(filename : string) ~(json : Ext_json_types.t) : Bsb_manifest_types.t
4649
pp_file = extract_string map Bsb_build_schemas.pp_flags;
4750
js_post_build_cmd = extract_js_post_build map;
4851
ignored_dirs = extract_string_list map Bsb_build_schemas.ignored_dirs;
49-
package_specs = extract_package_specs map ~suffix;
5052
use_stdlib = extract_boolean map Bsb_build_schemas.use_stdlib true;
5153
external_stdlib = extract_string map Bsb_build_schemas.external_stdlib;
52-
suffix;
5354
reason_react = extract_reason_react map;
5455
jsx = extract_jsx map;
5556
cut_generators = extract_boolean map Bsb_build_schemas.cut_generators false;
5657
uncurried = extract_boolean map Bsb_build_schemas.uncurried false;
58+
gentype = extract_gentype map ~package_specs;
5759
}
5860
)
5961
| _ -> Bsb_exception.invalid_spec (filename ^ " expect a json object {}")

jscomp/bsb/bsb_manifest_fields.ml

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,15 @@ let extract_generators (map : json_map) =
131131
!generators
132132

133133
let extract_package_specs (map : json_map) ~suffix =
134-
let bad_module_format_message_exn ~loc format =
135-
Bsb_exception.errorf ~loc
136-
"package-specs: `%s` isn't a valid output module format. It has to be one \
137-
of: %s, %s or %s"
138-
format Literals.commonjs Literals.es6 Literals.es6_global
139-
in
140134
let supported_format (x : string) loc : Ext_module_system.t =
141135
if x = Literals.commonjs then NodeJS
142136
else if x = Literals.es6 then Es6
143137
else if x = Literals.es6_global then Es6_global
144-
else bad_module_format_message_exn ~loc x
138+
else
139+
Bsb_exception.errorf ~loc
140+
"package-specs: `%s` isn't a valid output module format. It has to be one \
141+
of: %s, %s or %s"
142+
x Literals.commonjs Literals.es6 Literals.es6_global
145143
in
146144
let from_json_single suffix (x : Ext_json_types.t) : Bsb_manifest_types.package_spec =
147145
match x with
@@ -182,7 +180,7 @@ let extract_package_specs (map : json_map) ~suffix =
182180
Bsb_exception.errorf ~loc:(Ext_json.loc_of x)
183181
"package-specs: we expect either a string or an object."
184182
in
185-
let from_json suffix arr =
183+
let from_json_array suffix arr =
186184
let spec = ref Bsb_spec_set.empty in
187185
let has_in_source = ref false in
188186
Ext_array.iter arr (fun x ->
@@ -198,7 +196,7 @@ let extract_package_specs (map : json_map) ~suffix =
198196
(* TODO: FIXME: better API without mutating *)
199197
in
200198
match map.?(Bsb_build_schemas.package_specs) with
201-
| Some (Arr { content }) -> from_json suffix content
199+
| Some (Arr { content }) -> from_json_array suffix content
202200
| Some _ | None -> Bsb_spec_set.singleton ({ format = NodeJS; in_source = false; suffix })
203201

204202
let extract_ppx_specs (map : json_map) =
@@ -308,3 +306,53 @@ let extract_jsx (map : json_map) =
308306
mode = !mode;
309307
v3_dependencies = !v3_dependencies;
310308
}
309+
310+
let extract_gentype (map : json_map) ~package_specs =
311+
let open Bsb_manifest_types.Gentype in
312+
match map.?(Bsb_build_schemas.gentype) with
313+
| None -> None
314+
| Some (Obj { map }) -> (
315+
let { Bsb_manifest_types.format } = List.hd package_specs in
316+
let module_ =
317+
match (extract_string map Bsb_build_schemas.gentype_module, format) with
318+
| Some "commonjs", _ -> CommonJS
319+
| Some "es6", _ -> ES6
320+
| None, NodeJS -> CommonJS
321+
| None, (Es6 | Es6_global) -> ES6
322+
| _ -> ES6
323+
in
324+
let moduleResolution =
325+
match extract_string map Bsb_build_schemas.gentype_module_resolution with
326+
| Some "node" -> Node
327+
| Some "node16" -> Node16
328+
| Some "bundler" -> Bundler
329+
| _ -> Node
330+
in
331+
let shims =
332+
match map.?(Bsb_build_schemas.gentype_shims) with
333+
| None -> Map_string.empty
334+
| Some (Obj { map }) ->
335+
Map_string.map map
336+
(fun x ->
337+
match x with
338+
| Str { str } -> str
339+
| x -> Bsb_exception.manifest_error x "shims expects a record"
340+
)
341+
| Some manifest ->
342+
Bsb_exception.manifest_error manifest "shims expects a record"
343+
in
344+
let debug =
345+
match map.?(Bsb_build_schemas.gentype_debug) with
346+
| Some (Obj { map }) -> Some map
347+
| _ -> None
348+
in
349+
Some {
350+
module_;
351+
moduleResolution;
352+
shims;
353+
debug;
354+
exportInterfaces = extract_boolean map Bsb_build_schemas.gentype_export_interfaces false;
355+
generatedFileExtension = extract_string map Bsb_build_schemas.gentype_generated_file_extension;
356+
}
357+
)
358+
| Some manifest -> Bsb_exception.manifest_error manifest "expect an object"

jscomp/bsb/bsb_manifest_types.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ module Jsx = struct
4242
}
4343
end
4444

45+
module Gentype = struct
46+
type module_ = CommonJS | ES6
47+
48+
(** Compatibility for `compilerOptions.moduleResolution` in TypeScript projects. *)
49+
type moduleResolution =
50+
| Node (** should drop extension on import statements *)
51+
| Node16
52+
(** should use TS output's extension (e.g. `.gen.js`) on import statements *)
53+
| Bundler
54+
(** should use TS input's extension (e.g. `.gen.tsx`) on import statements *)
55+
56+
type t = {
57+
module_ : module_;
58+
moduleResolution : moduleResolution;
59+
exportInterfaces : bool;
60+
generatedFileExtension : string option;
61+
shims : string Map_string.t;
62+
debug : Ext_json_types.t Map_string.t option;
63+
}
64+
end
65+
4566
type t = {
4667
package_name : string;
4768
namespace : string option;
@@ -64,4 +85,5 @@ type t = {
6485
ignored_dirs : string list;
6586
use_stdlib : bool;
6687
external_stdlib : string option;
88+
gentype : Gentype.t option;
6789
}

0 commit comments

Comments
 (0)