Skip to content

Commit 4755d4a

Browse files
authored
Freely configurable suffix for generated .js files (#6472)
* Freely configurable suffix for generated .js files * Better error message * Update description in schema, too
1 parent 41fa202 commit 4755d4a

16 files changed

+51
-92
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
1313
# 11.0.0-rc.6 (Unreleased)
1414

15+
#### :rocket: New Feature
16+
17+
- Freely configurable suffix for generated .js files. https://github.com/rescript-lang/rescript-compiler/pull/6472
18+
1519
#### :bug: Bug Fix
20+
1621
- Fix issue with GenType and `result` introduced in rc.5. https://github.com/rescript-lang/rescript-compiler/pull/6464
1722
- Fix compiler crash when inlining complex constants in pattern matching. https://github.com/rescript-lang/rescript-compiler/pull/6471
1823

docs/docson/build-schema.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"description": "es6-global generate relative `require` paths instead of relying on NodeJS' module resolution. Default: commonjs."
77
},
88
"suffix-spec": {
9-
"enum": [".js", ".mjs", ".cjs", ".bs.js", ".bs.mjs", ".bs.cjs"],
10-
"description": "suffix of generated js files, default to [.js] "
9+
"type": "string",
10+
"description": "Suffix of generated js files. Default: .js. May contain letters, digits, \"-\", \"_\" and \".\" and must end with .js, .mjs or .cjs."
1111
},
1212
"module-format-object": {
1313
"type": "object",

jscomp/bsb/bsb_exception.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ let print (fmt : Format.formatter) (x : error) =
7171
Format.fprintf fmt
7272
"File %S, line %d:\n\
7373
@{<error>Error:@} %s \n\
74-
For more details, please checkout the schema \
74+
For more details, please check out the schema at \
7575
https://rescript-lang.org/docs/manual/latest/build-configuration-schema"
7676
pos.pos_fname pos.pos_lnum s
7777
| Invalid_spec s ->

jscomp/bsb/bsb_package_specs.ml

+22-22
Original file line numberDiff line numberDiff line change
@@ -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+
5862
let 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 *)
134137
let 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

183185
type 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

198198
let 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

jscomp/bsb/bsb_spec_set.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
(* TODO: sync up with {!Js_packages_info.module_system} *)
2828
type format = Ext_module_system.t = NodeJS | Es6 | Es6_global
2929

30-
type spec = { format : format; in_source : bool; suffix : Ext_js_suffix.t }
30+
type spec = { format : format; in_source : bool; suffix : string }
3131

3232
type t = spec list
3333

jscomp/bsb/bsb_spec_set.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424
type format = Ext_module_system.t
2525

26-
type spec = { format : format; in_source : bool; suffix : Ext_js_suffix.t }
26+
type spec = { format : format; in_source : bool; suffix : string }
2727

2828
type t = private spec list
2929

jscomp/core/js_name_of_module_id.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ let get_runtime_module_path
5959
module_system in
6060
let js_file =
6161
Ext_namespace.js_name_of_modulename dep_module_id.id.name
62-
Little Js in
62+
Little Literals.suffix_js in
6363
match current_info_query with
6464
| Package_not_found -> assert false
6565
| Package_script ->
@@ -183,7 +183,7 @@ let string_of_module_id
183183
| Package_script, Package_script
184184
->
185185
let js_file =
186-
Ext_namespace.js_name_of_modulename dep_module_id.id.name case Js in
186+
Ext_namespace.js_name_of_modulename dep_module_id.id.name case Literals.suffix_js in
187187
match Config_util.find_opt js_file with
188188
| Some file ->
189189
let basename = Filename.basename file in

jscomp/core/js_packages_info.ml

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ let compatible (dep : module_system) (query : module_system) =
3838
type package_info = {
3939
module_system : module_system;
4040
path : string;
41-
suffix : Ext_js_suffix.t;
41+
suffix : string;
4242
}
4343

4444
type package_name = Pkg_empty | Pkg_runtime | Pkg_normal of string
@@ -61,8 +61,8 @@ let runtime_package_specs : t =
6161
name = Pkg_runtime;
6262
module_systems =
6363
[
64-
{ module_system = Es6; path = "lib/es6"; suffix = Js };
65-
{ module_system = NodeJS; path = "lib/js"; suffix = Js };
64+
{ module_system = Es6; path = "lib/es6"; suffix = Literals.suffix_js };
65+
{ module_system = NodeJS; path = "lib/js"; suffix = Literals.suffix_js };
6666
];
6767
}
6868

@@ -121,7 +121,7 @@ let dump_package_info (fmt : Format.formatter)
121121
Format.fprintf fmt "@[%s@ %s@ %s@]"
122122
(string_of_module_system ms)
123123
name
124-
(Ext_js_suffix.to_string suffix)
124+
suffix
125125

126126
let dump_package_name fmt (x : package_name) =
127127
match x with
@@ -140,7 +140,7 @@ let dump_packages_info (fmt : Format.formatter)
140140
type package_found_info = {
141141
rel_path : string;
142142
pkg_rel_path : string;
143-
suffix : Ext_js_suffix.t;
143+
suffix : string;
144144
}
145145

146146
type info_query =
@@ -201,18 +201,18 @@ let add_npm_package_path (packages_info : t) (s : string) : t =
201201
in
202202
let m =
203203
match Ext_string.split ~keep_empty:true s ':' with
204-
| [ path ] -> { module_system = NodeJS; path; suffix = Js }
204+
| [ path ] -> { module_system = NodeJS; path; suffix = Literals.suffix_js }
205205
| [ module_system; path ] ->
206206
{
207207
module_system = handle_module_system module_system;
208208
path;
209-
suffix = Js;
209+
suffix = Literals.suffix_js;
210210
}
211211
| [ module_system; path; suffix ] ->
212212
{
213213
module_system = handle_module_system module_system;
214214
path;
215-
suffix = Ext_js_suffix.of_string suffix;
215+
suffix;
216216
}
217217
| _ -> Bsc_args.bad_arg ("invalid npm package path: " ^ s)
218218
in

jscomp/core/js_packages_info.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ val runtime_package_path : module_system -> string -> string
3131
type package_info = {
3232
module_system : module_system;
3333
path : string;
34-
suffix : Ext_js_suffix.t;
34+
suffix : string;
3535
}
3636

3737
type t
@@ -64,7 +64,7 @@ val add_npm_package_path : t -> string -> t
6464
type package_found_info = {
6565
rel_path : string;
6666
pkg_rel_path : string;
67-
suffix : Ext_js_suffix.t;
67+
suffix : string;
6868
}
6969

7070
type info_query =

jscomp/core/lam_compile_main.ml

+1-4
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,7 @@ let lambda_as_module
301301
(lambda_output)
302302
chan in
303303
let basename =
304-
Ext_namespace.change_ext_ns_suffix
305-
(Filename.basename
306-
output_prefix)
307-
(Ext_js_suffix.to_string suffix)
304+
Ext_namespace.change_ext_ns_suffix (Filename.basename output_prefix) suffix
308305
in
309306
let target_file =
310307
(Lazy.force Ext_path.package_dir //

jscomp/ext/ext_js_file_kind.ml

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424
type case = Upper | Little
2525

26-
type [@warning "-69"] t = { case : case; suffix : Ext_js_suffix.t }
27-
28-
let any_runtime_kind = { case = Little; suffix = Ext_js_suffix.Js }
26+
type [@warning "-69"] t = { case : case; suffix : string }

jscomp/ext/ext_js_suffix.ml

-28
This file was deleted.

jscomp/ext/ext_namespace.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let js_name_of_modulename s (case : Ext_js_file_kind.case) suffix : string =
4545
let s =
4646
match case with Little -> Ext_string.uncapitalize_ascii s | Upper -> s
4747
in
48-
change_ext_ns_suffix s (Ext_js_suffix.to_string suffix)
48+
change_ext_ns_suffix s suffix
4949

5050
(* https://docs.npmjs.com/files/package.json
5151
Some rules:

jscomp/ext/ext_namespace.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ val try_split_module_name : string -> (string * string) option
3434
val change_ext_ns_suffix : string -> string -> string
3535

3636
val js_name_of_modulename :
37-
string -> Ext_js_file_kind.case -> Ext_js_suffix.t -> string
37+
string -> Ext_js_file_kind.case -> string -> string
3838
(** [js_name_of_modulename ~little A-Ns]
3939
*)
4040

jscomp/ext/literals.ml

-10
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,6 @@ let suffix_d = ".d"
121121

122122
let suffix_js = ".js"
123123

124-
let suffix_bs_js = ".bs.js"
125-
126-
let suffix_mjs = ".mjs"
127-
128-
let suffix_bs_mjs = ".bs.mjs"
129-
130-
let suffix_cjs = ".cjs"
131-
132-
let suffix_bs_cjs = ".bs.cjs"
133-
134124
let suffix_gen_js = ".gen.js"
135125

136126
let suffix_gen_tsx = ".gen.tsx"

jscomp/ounit_tests/ounit_string_tests.ml

+3-6
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,11 @@ let suites =
386386
=~ "a-.js";
387387
Ext_namespace.change_ext_ns_suffix "AA-b" Literals.suffix_js
388388
=~ "AA.js";
389-
Ext_namespace.js_name_of_modulename
390-
"AA-b" Little Js
389+
Ext_namespace.js_name_of_modulename "AA-b" Little Literals.suffix_js
391390
=~ "aA.js";
392-
Ext_namespace.js_name_of_modulename
393-
"AA-b" Upper Js
391+
Ext_namespace.js_name_of_modulename "AA-b" Upper Literals.suffix_js
394392
=~ "AA.js";
395-
Ext_namespace.js_name_of_modulename
396-
"AA-b" Upper Bs_js
393+
Ext_namespace.js_name_of_modulename "AA-b" Upper ".bs.js"
397394
=~ "AA.bs.js";
398395
end;
399396
__LOC__ >:: begin fun _ ->

0 commit comments

Comments
 (0)