2525
2626let (// ) = Ext_path. combine
2727
28- let common_js_prefix p = Bsb_config. lib_js // p
29- let es6_prefix p = Bsb_config. lib_es6 // p
30- let es6_global_prefix p = Bsb_config. lib_es6_global // p
3128
3229
30+ (* TODO: sync up with {!Js_packages_info.module_system} *)
31+ type format =
32+ | NodeJS | Es6 | Es6_global
33+
3334type spec = {
34- format : string ;
35+ format : format ;
3536 in_source : bool
3637}
3738
@@ -42,19 +43,30 @@ module Spec_set = Set.Make( struct type t = spec
4243type t = Spec_set .t
4344
4445
45-
46- let supported_format x =
47- x = Literals. commonjs ||
48- x = Literals. es6 ||
49- x = Literals. es6_global
50-
5146let bad_module_format_message_exn ~loc format =
5247 Bsb_exception. errorf ~loc " package-specs: `%s` isn't a valid output module format. It has to be one of: %s, %s or %s"
5348 format
5449 Literals. commonjs
5550 Literals. es6
5651 Literals. es6_global
5752
53+ let supported_format (x : string ) loc =
54+ if x = Literals. commonjs then NodeJS
55+ else if x = Literals. es6 then Es6
56+ else if x = Literals. es6_global then Es6_global
57+ else bad_module_format_message_exn ~loc x
58+
59+ let string_of_format (x : format ) =
60+ match x with
61+ | NodeJS -> Literals. commonjs
62+ | Es6 -> Literals. es6
63+ | Es6_global -> Literals. es6_global
64+
65+ let prefix_of_format (x : format ) =
66+ (match x with
67+ | NodeJS -> Bsb_config. lib_js
68+ | Es6 -> Bsb_config. lib_es6
69+ | Es6_global -> Bsb_config. lib_es6_global )
5870
5971let rec from_array (arr : Ext_json_types.t array ) : Spec_set.t =
6072 let spec = ref Spec_set. empty in
@@ -77,11 +89,8 @@ let rec from_array (arr : Ext_json_types.t array) : Spec_set.t =
7789(* TODO: FIXME: better API without mutating *)
7890and from_json_single (x : Ext_json_types.t ) : spec =
7991 match x with
80- | Str {str = format ; loc } ->
81- if supported_format format then
82- {format ; in_source = false }
83- else
84- (bad_module_format_message_exn ~loc format)
92+ | Str {str = format ; loc } ->
93+ {format = supported_format format loc ; in_source = false }
8594 | Obj {map; loc} ->
8695 begin match String_map. find_exn map " module" with
8796 | Str {str = format } ->
@@ -90,11 +99,8 @@ and from_json_single (x : Ext_json_types.t) : spec =
9099 | Some (True _ ) -> true
91100 | Some _
92101 | None -> false
93- in
94- if supported_format format then
95- {format ; in_source }
96- else
97- bad_module_format_message_exn ~loc format
102+ in
103+ {format = supported_format format loc ; in_source }
98104 | Arr _ ->
99105 Bsb_exception. errorf ~loc
100106 " package-specs: when the configuration is an object, `module` field should be a string, not an array. If you want to pass multiple module specs, try turning package-specs into an array of objects (or strings) instead."
@@ -123,16 +129,10 @@ let package_flag ({format; in_source } : spec) dir =
123129 Ext_string. inter2
124130 bs_package_output
125131 (Ext_string. concat3
126- format
132+ (string_of_format format)
127133 Ext_string. single_colon
128134 (if in_source then dir else
129- if format = Literals. commonjs then
130- common_js_prefix dir
131- else if format = Literals. es6 then
132- es6_prefix dir
133- else if format = Literals. es6_global then
134- es6_global_prefix dir
135- else assert false ))
135+ prefix_of_format format // dir))
136136
137137let package_flag_of_package_specs (package_specs : t )
138138 (dirname : string ) : string =
@@ -142,36 +142,36 @@ let package_flag_of_package_specs (package_specs : t)
142142
143143let default_package_specs =
144144 Spec_set. singleton
145- { format = Literals. commonjs ; in_source = false }
146- (* * js output for each package *)
147- let package_output ({format; in_source } : spec ) output =
148-
149- let prefix =
150- if in_source then fun x -> x
151- else
152- (if format = Literals. commonjs then
153- common_js_prefix
154- else if format = Literals. es6 then
155- es6_prefix
156- else if format = Literals. es6_global then
157- es6_global_prefix
158- else assert false )
159- in
160- (Bsb_config. proj_rel @@ prefix output )
145+ { format = NodeJS ; in_source = false }
146+
147+
161148
162149(* *
163150 [get_list_of_output_js specs "src/hi/hello"]
164151
165152*)
166153let get_list_of_output_js
167- package_specs
168- bs_suffix
169- output_file_sans_extension =
154+ (package_specs : Spec_set.t )
155+ (bs_suffix : bool )
156+ (output_file_sans_extension : string )
157+ =
170158 Spec_set. fold
171- (fun format acc ->
172- package_output format
173- ( Ext_namespace. js_name_of_basename bs_suffix
174- output_file_sans_extension)
159+ (fun (spec : spec ) acc ->
160+ let basename = Ext_namespace. change_ext_ns_suffix
161+ output_file_sans_extension
162+ (if bs_suffix then Literals. suffix_bs_js else Literals. suffix_js)
163+ in
164+ (Bsb_config. proj_rel @@ (if spec.in_source then basename
165+ else prefix_of_format spec.format // basename))
175166 :: acc
176167 ) package_specs []
177168
169+
170+ let list_dirs_by
171+ (package_specs : Spec_set.t )
172+ (f : string -> unit )
173+ =
174+ Spec_set. iter (fun (spec : spec ) ->
175+ if not spec.in_source then
176+ f (prefix_of_format spec.format)
177+ ) package_specs
0 commit comments