Skip to content

Commit e4cd9b2

Browse files
authored
Merge pull request #4691 from rescript-lang/wip_file_extension_per_module_system
prepare customized file extension support
2 parents edd9431 + a0d7ce5 commit e4cd9b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2590
-2267
lines changed

docs/docson/build-schema.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
],
1010
"description": "es6-global generate relative `require` paths instead of relying on NodeJS' module resolution. Default: commonjs."
1111
},
12+
"suffix-spec" : {
13+
"enum" : [
14+
".js",
15+
".bs.js",
16+
".mjs",
17+
".cjs"
18+
],
19+
"description": "suffix of generated js files, default to [.js] "
20+
},
1221
"module-format-object": {
1322
"type": "object",
1423
"properties": {
@@ -18,6 +27,9 @@
1827
"in-source": {
1928
"type": "boolean",
2029
"description": "Default: false."
30+
},
31+
"suffix" : {
32+
"$ref" : "#/definitions/suffix-spec"
2133
}
2234
},
2335
"required": [
@@ -484,11 +496,7 @@
484496
"description": "(Not needed usually) arguments to pass to `refmt` above. Default: `[\"--print\", \"binary\"]`."
485497
},
486498
"suffix" : {
487-
"enum" : [
488-
".js",
489-
".bs.js"
490-
],
491-
"description": "suffix of generated js files, default to [.js] "
499+
"$ref" : "#/definitions/suffix-spec"
492500
}
493501
},
494502
"additionalProperties": false,

jscomp/bsb/bsb_config_parse.ml

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@ let package_specs_from_bsconfig () =
4747
let json = Ext_json_parse.parse_json_from_file Literals.bsconfig_json in
4848
begin match json with
4949
| Obj {map} ->
50-
begin
51-
match Map_string.find_opt map Bsb_build_schemas.package_specs with
52-
| Some x ->
53-
Bsb_package_specs.from_json x
54-
| None ->
55-
Bsb_package_specs.default_package_specs
56-
end
50+
Bsb_package_specs.from_map map
5751
| _ -> assert false
5852
end
5953

@@ -148,13 +142,14 @@ let extract_bs_suffix_exn (map : json_map) : Ext_js_suffix.t =
148142
match Map_string.find_opt map Bsb_build_schemas.suffix with
149143
| None -> Js
150144
| Some (Str {str} as config ) ->
151-
if str = Literals.suffix_js then Js
152-
else if str = Literals.suffix_bs_js then Bs_js
153-
else Bsb_exception.config_error config
154-
"expect .bs.js or .js string here"
145+
let s = Ext_js_suffix.of_string str in
146+
if s = Unknown_extension then
147+
Bsb_exception.config_error config
148+
"expect .bs.js, .js, .cjs, .mjs here"
149+
else s
155150
| Some config ->
156151
Bsb_exception.config_error config
157-
"expect .bs.js or .js string here"
152+
"expect a string exteion like \".js\" here"
158153

159154
let extract_gentype_config (map : json_map) cwd
160155
: Bsb_config_types.gentype_config option =
@@ -365,17 +360,13 @@ let interpret_json
365360
extract_package_name_and_namespace map in
366361
let refmt = extract_refmt map per_proj_dir in
367362
let gentype_config = extract_gentype_config map per_proj_dir in
368-
let bs_suffix = extract_bs_suffix_exn map in
369363
(* This line has to be before any calls to Bsb_global_backend.backend, because it'll read the entries
370364
array from the bsconfig and set the backend_ref to the first entry, if any. *)
371365

372366
(* The default situation is empty *)
373367
let built_in_package = check_stdlib map per_proj_dir in
374-
let package_specs =
375-
match Map_string.find_opt map Bsb_build_schemas.package_specs with
376-
| Some x ->
377-
Bsb_package_specs.from_json x
378-
| None -> Bsb_package_specs.default_package_specs
368+
let package_specs =
369+
Bsb_package_specs.from_map map
379370
in
380371
let pp_flags : string option =
381372
extract_string map Bsb_build_schemas.pp_flags (fun p ->
@@ -400,12 +391,10 @@ let interpret_json
400391
~toplevel
401392
~root: per_proj_dir
402393
~cut_generators
403-
~bs_suffix
404394
~namespace
405395
sources in
406396
{
407397
gentype_config;
408-
bs_suffix ;
409398
package_name ;
410399
namespace ;
411400
warning = extract_warning map;

jscomp/bsb/bsb_config_types.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ type t =
7474
generate_merlin : bool ;
7575
reason_react_jsx : reason_react_jsx option; (* whether apply PPX transform or not*)
7676
generators : command Map_string.t ;
77-
cut_generators : bool; (* note when used as a dev mode, we will always ignore it *)
78-
bs_suffix : Ext_js_suffix.t ;
77+
cut_generators : bool; (* note when used as a dev mode, we will always ignore it *)
7978
gentype_config : gentype_config option;
8079
}

jscomp/bsb/bsb_ninja_file_groups.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ let emit_module_build
9393
(package_specs : Bsb_package_specs.t)
9494
(is_dev : bool)
9595
oc
96-
~(bs_suffix : Ext_js_suffix.t)
9796
js_post_build_cmd
9897
namespace
9998
(module_info : Bsb_db.module_info)
@@ -117,7 +116,7 @@ let emit_module_build
117116
let output_cmi = output_filename_sans_extension ^ Literals.suffix_cmi in
118117
let output_cmj = output_filename_sans_extension ^ Literals.suffix_cmj in
119118
let output_js =
120-
Bsb_package_specs.get_list_of_output_js package_specs bs_suffix output_filename_sans_extension in
119+
Bsb_package_specs.get_list_of_output_js package_specs output_filename_sans_extension in
121120
let common_shadows =
122121
make_common_shadows package_specs
123122
(Filename.dirname output_cmi)
@@ -187,7 +186,6 @@ let emit_module_build
187186

188187
let handle_files_per_dir
189188
oc
190-
~(bs_suffix : Ext_js_suffix.t)
191189
~(rules : Bsb_ninja_rule.builtin)
192190
~package_specs
193191
~js_post_build_cmd
@@ -212,7 +210,6 @@ let handle_files_per_dir
212210
package_specs
213211
group.dev_index
214212
oc
215-
~bs_suffix
216213
js_post_build_cmd
217214
namespace module_info
218215
)

jscomp/bsb/bsb_ninja_file_groups.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
val handle_files_per_dir :
2929
out_channel ->
30-
bs_suffix:Ext_js_suffix.t ->
3130
rules:Bsb_ninja_rule.builtin ->
3231
package_specs:Bsb_package_specs.t ->
3332
js_post_build_cmd:string option ->

jscomp/bsb/bsb_ninja_gen.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ let output_ninja_and_namespace_map
9696
~per_proj_dir
9797
~toplevel
9898
({
99-
bs_suffix;
10099
package_name;
101100
external_includes;
102101
bsc_flags ;
@@ -205,7 +204,6 @@ let output_ninja_and_namespace_map
205204
~has_pp:(pp_file <> None)
206205
~has_builtin:(built_in_dependency <> None)
207206
~reason_react_jsx
208-
~bs_suffix
209207
~digest
210208
generators in
211209
emit_bsc_lib_includes bs_dependencies source_dirs.lib external_includes namespace oc;
@@ -214,7 +212,6 @@ let output_ninja_and_namespace_map
214212
Ext_list.iter bs_file_groups
215213
(fun files_per_dir ->
216214
Bsb_ninja_file_groups.handle_files_per_dir oc
217-
~bs_suffix
218215
~rules
219216
~js_post_build_cmd
220217
~package_specs

jscomp/bsb/bsb_ninja_rule.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ let make_custom_rules
115115
~(has_ppx : bool)
116116
~(has_pp : bool)
117117
~(has_builtin : bool)
118-
~(bs_suffix : Ext_js_suffix.t)
119118
~(reason_react_jsx : Bsb_config_types.reason_react_jsx option)
120119
~(digest : string)
121120
~(refmt : string option) (* set refmt path when needed *)
@@ -131,7 +130,6 @@ let make_custom_rules
131130
Ext_buffer.clear buf;
132131
Ext_buffer.add_string buf "$bsc";
133132
Ext_buffer.add_ninja_prefix_var buf Bsb_ninja_global_vars.g_pkg_flg;
134-
Ext_js_suffix.to_bsc_flag bs_suffix buf;
135133
if read_cmi then
136134
Ext_buffer.add_string buf " -bs-read-cmi";
137135
if is_dev then

jscomp/bsb/bsb_ninja_rule.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ val make_custom_rules :
7474
has_ppx:bool ->
7575
has_pp:bool ->
7676
has_builtin:bool ->
77-
bs_suffix:Ext_js_suffix.t ->
7877
reason_react_jsx : Bsb_config_types.reason_react_jsx option ->
7978
digest:string ->
8079
refmt:string option ->

jscomp/bsb/bsb_package_specs.ml

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ type format =
3333

3434
type spec = {
3535
format : format;
36-
in_source : bool
36+
in_source : bool;
37+
suffix : Ext_js_suffix.t
3738
}
3839

3940
module Spec_set = Set.Make( struct type t = spec
@@ -68,11 +69,11 @@ let prefix_of_format (x : format) =
6869
| Es6 -> Bsb_config.lib_es6
6970
| Es6_global -> Bsb_config.lib_es6_global )
7071

71-
let rec from_array (arr : Ext_json_types.t array) : Spec_set.t =
72+
let rec from_array suffix (arr : Ext_json_types.t array) : Spec_set.t =
7273
let spec = ref Spec_set.empty in
7374
let has_in_source = ref false in
7475
Ext_array.iter arr (fun x ->
75-
let result = from_json_single x in
76+
let result = from_json_single suffix x in
7677
if result.in_source then
7778
(
7879
if not !has_in_source then
@@ -87,10 +88,10 @@ let rec from_array (arr : Ext_json_types.t array) : Spec_set.t =
8788
!spec
8889

8990
(* TODO: FIXME: better API without mutating *)
90-
and from_json_single (x : Ext_json_types.t) : spec =
91+
and from_json_single suffix (x : Ext_json_types.t) : spec =
9192
match x with
9293
| Str {str = format; loc } ->
93-
{format = supported_format format loc ; in_source = false }
94+
{format = supported_format format loc ; in_source = false ; suffix }
9495
| Obj {map; loc} ->
9596
begin match Map_string.find_exn map "module" with
9697
| Str {str = format} ->
@@ -100,7 +101,17 @@ and from_json_single (x : Ext_json_types.t) : spec =
100101
| Some _
101102
| None -> false
102103
in
103-
{format = supported_format format loc ; in_source }
104+
let suffix =
105+
match Map_string.find_opt map "suffix" with
106+
| Some (Str {str = suffix; loc}) ->
107+
let s = Ext_js_suffix.of_string suffix in
108+
if s = Unknown_extension then
109+
Bsb_exception.errorf ~loc "expect .js,.bs.js,.mjs or .cjs"
110+
else s
111+
| Some v ->
112+
Bsb_exception.errorf ~loc:(Ext_json.loc_of x) "expect a string field"
113+
| None -> suffix in
114+
{format = supported_format format loc ; in_source ; suffix}
104115
| Arr _ ->
105116
Bsb_exception.errorf ~loc
106117
"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."
@@ -114,35 +125,38 @@ and from_json_single (x : Ext_json_types.t) : spec =
114125
| _ -> Bsb_exception.errorf ~loc:(Ext_json.loc_of x)
115126
"package-specs: we expect either a string or an object."
116127

117-
let from_json (x : Ext_json_types.t) : Spec_set.t =
128+
let from_json suffix (x : Ext_json_types.t) : Spec_set.t =
118129
match x with
119-
| Arr {content ; _} -> from_array content
120-
| _ -> Spec_set.singleton (from_json_single x )
121-
130+
| Arr {content ; _} -> from_array suffix content
131+
| _ -> Spec_set.singleton (from_json_single suffix x )
122132

123133
let bs_package_output = "-bs-package-output"
124-
134+
[@@@warning "+9"]
125135
(** Assume input is valid
126-
{[ -bs-package-output commonjs:lib/js/jscomp/test ]}
136+
coordinate with command line flag
137+
{[ -bs-package-output commonjs:lib/js/jscomp/test:.js ]}
127138
*)
128-
let package_flag ({format; in_source } : spec) dir =
139+
let package_flag ({format; in_source; suffix } : spec) dir =
129140
Ext_string.inter2
130141
bs_package_output
131-
(Ext_string.concat3
142+
(Ext_string.concat5
132143
(string_of_format format)
133144
Ext_string.single_colon
134145
(if in_source then dir else
135-
prefix_of_format format // dir))
146+
prefix_of_format format // dir)
147+
Ext_string.single_colon
148+
(Ext_js_suffix.to_string suffix)
149+
)
136150

137151
let package_flag_of_package_specs (package_specs : t)
138152
(dirname : string ) : string =
139153
Spec_set.fold (fun format acc ->
140154
Ext_string.inter2 acc (package_flag format dirname )
141155
) package_specs Ext_string.empty
142156

143-
let default_package_specs =
157+
let default_package_specs suffix =
144158
Spec_set.singleton
145-
{ format = NodeJS ; in_source = false }
159+
{ format = NodeJS ; in_source = false; suffix }
146160

147161

148162

@@ -152,14 +166,14 @@ let default_package_specs =
152166
*)
153167
let get_list_of_output_js
154168
(package_specs : Spec_set.t)
155-
(bs_suffix : Ext_js_suffix.t)
156169
(output_file_sans_extension : string)
157170
=
158171
Spec_set.fold
159172
(fun (spec : spec) acc ->
160-
let basename = Ext_namespace.change_ext_ns_suffix
173+
let basename =
174+
Ext_namespace.change_ext_ns_suffix
161175
output_file_sans_extension
162-
(Ext_js_suffix.to_string bs_suffix)
176+
(Ext_js_suffix.to_string spec.suffix)
163177
in
164178
(Bsb_config.proj_rel (if spec.in_source then basename
165179
else prefix_of_format spec.format // basename))
@@ -174,4 +188,27 @@ let list_dirs_by
174188
Spec_set.iter (fun (spec : spec) ->
175189
if not spec.in_source then
176190
f (prefix_of_format spec.format)
177-
) package_specs
191+
) package_specs
192+
193+
type json_map = Ext_json_types.t Map_string.t
194+
195+
let extract_bs_suffix_exn (map : json_map) : Ext_js_suffix.t =
196+
match Map_string.find_opt map Bsb_build_schemas.suffix with
197+
| None -> Js
198+
| Some (Str {str; loc} as config ) ->
199+
let s = Ext_js_suffix.of_string str in
200+
if s = Unknown_extension then
201+
Bsb_exception.errorf ~loc
202+
"expect .bs.js, .js, .cjs, .mjs here"
203+
else s
204+
| Some config ->
205+
Bsb_exception.config_error config
206+
"expect a string exteion like \".js\" here"
207+
208+
let from_map map =
209+
let suffix = extract_bs_suffix_exn map in
210+
match Map_string.find_opt map Bsb_build_schemas.package_specs with
211+
| Some x ->
212+
from_json suffix x
213+
| None -> default_package_specs suffix
214+

jscomp/bsb/bsb_package_specs.mli

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525
type t
2626

2727

28-
val default_package_specs : t
2928

30-
val from_json:
31-
Ext_json_types.t -> t
29+
30+
val from_map:
31+
Ext_json_types.t Map_string.t -> t
3232

3333
val get_list_of_output_js :
3434
t ->
35-
Ext_js_suffix.t ->
3635
string ->
3736
string list
3837

0 commit comments

Comments
 (0)