@@ -55,6 +55,10 @@ let string_of_format (x : Ext_module_system.t) =
5555 | Es6 -> Literals. es6
5656 | Es6_global -> Literals. es6_global
5757
58+ let js_suffix_regexp = Str. regexp " [A-Za-z0-9-_.]*\\ .[cm]?js"
59+
60+ let validate_js_suffix suffix = Str. string_match js_suffix_regexp suffix 0
61+
5862let rec from_array suffix (arr : Ext_json_types.t array ) : Spec_set.t =
5963 let spec = ref Spec_set. empty in
6064 let has_in_source = ref false in
@@ -83,15 +87,14 @@ and from_json_single suffix (x : Ext_json_types.t) : Bsb_spec_set.spec =
8387 | Some _ | None -> false
8488 in
8589 let suffix =
86- match map.?(" suffix" ) with
87- | Some (Str { str = suffix ; loc } ) ->
88- let s = Ext_js_suffix. of_string suffix in
89- if s = Unknown_extension then
90- Bsb_exception. errorf ~loc " expected .js, .mjs, .cjs or .bs.js, .bs.mjs, .bs.cjs"
91- else s
90+ match map.?(Bsb_build_schemas. suffix) with
91+ | Some (Str { str = suffix ; _ } ) when validate_js_suffix suffix -> suffix
92+ | Some (Str {str; loc} ) ->
93+ Bsb_exception. errorf ~loc
94+ (" invalid suffix \" %s\" . The suffix and may contain letters, digits, \" -\" , \" _\" and \" .\" and must end with .js, .mjs or .cjs." ) str
9295 | Some _ ->
93- Bsb_exception. errorf ~loc: (Ext_json. loc_of x)
94- " expected a string field "
96+ Bsb_exception. errorf ~loc: (Ext_json. loc_of x)
97+ " expected a string extension like \" .js \" "
9598 | None -> suffix
9699 in
97100 { format = supported_format format loc; in_source; suffix }
@@ -128,7 +131,7 @@ let package_flag ({ format; in_source; suffix } : Bsb_spec_set.spec) dir =
128131 (if in_source then dir
129132 else Bsb_config. top_prefix_of_format format // dir)
130133 Ext_string. single_colon
131- ( Ext_js_suffix. to_string suffix) )
134+ suffix)
132135
133136(* FIXME: we should adapt it *)
134137let package_flag_of_package_specs (package_specs : t ) ~(dirname : string ) :
@@ -166,8 +169,7 @@ let get_list_of_output_js (package_specs : t)
166169 Spec_set. fold
167170 (fun (spec : Bsb_spec_set.spec ) acc ->
168171 let basename =
169- Ext_namespace. change_ext_ns_suffix output_file_sans_extension
170- (Ext_js_suffix. to_string spec.suffix)
172+ Ext_namespace. change_ext_ns_suffix output_file_sans_extension spec.suffix
171173 in
172174 (if spec.in_source then Bsb_config. rev_lib_bs_prefix basename
173175 else Bsb_config. lib_bs_prefix_of_format spec.format // basename)
@@ -182,21 +184,19 @@ let list_dirs_by (package_specs : t) (f : string -> unit) =
182184
183185type json_map = Ext_json_types .t Map_string .t
184186
185- let extract_bs_suffix_exn (map : json_map ) : Ext_js_suffix.t =
187+ let extract_js_suffix_exn (map : json_map ) : string =
186188 match map.?(Bsb_build_schemas. suffix) with
187- | None -> Js
188- | Some (Str { str; loc } ) ->
189- let s = Ext_js_suffix. of_string str in
190- if s = Unknown_extension then
191- Bsb_exception. errorf ~loc
192- " expected .js, .mjs, .cjs or .bs.js, .bs.mjs, .bs.cjs"
193- else s
189+ | None -> Literals. suffix_js
190+ | Some (Str { str = suffix ; _ } ) when validate_js_suffix suffix -> suffix
191+ | Some ((Str {str; _} ) as config ) ->
192+ Bsb_exception. config_error config
193+ (" invalid suffix \" " ^ str ^ " \" . The suffix and may contain letters, digits, \" -\" , \" _\" and \" .\" and must end with .js, .mjs or .cjs." )
194194 | Some config ->
195- Bsb_exception. config_error config
196- " expected a string extension like \" .js\" "
195+ Bsb_exception. config_error config
196+ " expected a string extension like \" .js\" "
197197
198198let from_map ~(cwd : string ) map =
199- let suffix = extract_bs_suffix_exn map in
199+ let suffix = extract_js_suffix_exn map in
200200 let modules =
201201 match map.?(Bsb_build_schemas. package_specs) with
202202 | Some x -> from_json suffix x
0 commit comments