Skip to content

Commit c3cc9a8

Browse files
committed
Extract -bs-package-output into multiple arguments
1 parent a6b393a commit c3cc9a8

File tree

10 files changed

+67
-30
lines changed

10 files changed

+67
-30
lines changed

compiler/bsb/bsb_package_specs.ml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,18 @@ let from_json suffix (x : Ext_json_types.t) : Spec_set.t =
130130
| Arr {content; _} -> from_array suffix content
131131
| _ -> Spec_set.singleton (from_json_single suffix x)
132132

133-
let bs_package_output = "-bs-package-output"
134-
135133
[@@@warning "+9"]
136134

137135
let package_flag ({format; in_source; suffix} : Bsb_spec_set.spec) dir =
138-
Ext_string.inter2 bs_package_output
139-
(Ext_string.concat5 (string_of_format format) Ext_string.single_colon
140-
(if in_source then dir else Bsb_config.top_prefix_of_format format // dir)
141-
Ext_string.single_colon suffix)
136+
(* Generate separate flags for module system, suffix, and output path *)
137+
let module_system_flag = "-bs-module-system " ^ string_of_format format in
138+
let suffix_flag = "-bs-suffix " ^ suffix in
139+
let output_path =
140+
if in_source then dir else Bsb_config.top_prefix_of_format format // dir
141+
in
142+
let output_flag = "-bs-package-output " ^ output_path in
143+
(* Concatenate all three flags *)
144+
module_system_flag ^ " " ^ suffix_flag ^ " " ^ output_flag
142145

143146
(* FIXME: we should adapt it *)
144147
let package_flag_of_package_specs (package_specs : t) ~(dirname : string) :

compiler/bsc/rescript_compiler_main.ml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,23 @@ let command_line_flags : (string * Bsc_args.spec * string) array =
259259
string_call ignore,
260260
"*internal* Set jsx mode, this is no longer used and is a no-op." );
261261
("-bs-jsx-preserve", set Js_config.jsx_preserve, "*internal* Preserve jsx");
262+
( "-bs-module-system",
263+
string_call (fun s ->
264+
Js_config.default_module_system :=
265+
match Js_packages_info.module_system_of_string s with
266+
| Some ms -> ms
267+
| None ->
268+
Bsc_args.bad_arg
269+
("Invalid module system: " ^ s
270+
^ ". Use: commonjs, esmodule, or es6-global")),
271+
"*internal* Set module system: commonjs, esmodule, es6-global" );
272+
( "-bs-suffix",
273+
string_call (fun s -> Js_config.default_suffix := s),
274+
"*internal* Set import file suffix: .js, .mjs, .cjs" );
262275
( "-bs-package-output",
263276
string_call Js_packages_state.update_npm_package_path,
264-
"*internal* Set npm-output-path: [opt_module]:path, for example: \
265-
'lib/cjs', 'amdjs:lib/amdjs', 'es6:lib/es6' " );
277+
"*internal* Set output path (when combined with -bs-module-system and \
278+
-bs-suffix)" );
266279
( "-bs-ast",
267280
unit_call (fun _ ->
268281
Js_config.binary_ast := true;

compiler/common/js_config.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ let jsx_version = ref None
5252
let jsx_module = ref React
5353
let jsx_preserve = ref false
5454
let js_stdout = ref true
55+
let default_module_system = ref Ext_module_system.Commonjs
56+
let default_suffix = ref Literals.suffix_js
5557
let all_module_aliases = ref false
5658
let no_stdlib = ref false
5759
let no_export = ref false

compiler/common/js_config.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ val jsx_preserve : bool ref
8484

8585
val js_stdout : bool ref
8686

87+
val default_module_system : Ext_module_system.t ref
88+
89+
val default_suffix : string ref
90+
8791
val all_module_aliases : bool ref
8892

8993
val no_stdlib : bool ref

compiler/core/js_name_of_module_id.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ let get_runtime_module_path
5757
let current_info_query =
5858
Js_packages_info.query_package_infos current_package_info
5959
module_system in
60+
(* Runtime package is pre-compiled and always uses .js suffix *)
6061
let js_file =
6162
Ext_namespace.js_name_of_modulename dep_module_id.id.name
6263
Upper Literals.suffix_js in
@@ -177,8 +178,9 @@ let string_of_module_id
177178
end
178179
| Package_script, Package_script
179180
->
181+
(* Use configured suffix instead of hardcoded .js *)
180182
let js_file =
181-
Ext_namespace.js_name_of_modulename dep_module_id.id.name case Literals.suffix_js in
183+
Ext_namespace.js_name_of_modulename dep_module_id.id.name case !Js_config.default_suffix in
182184
match Config_util.find_opt js_file with
183185
| Some file ->
184186
let basename = Filename.basename file in

compiler/core/js_packages_info.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,21 @@ let add_npm_package_path (packages_info : t) (s : string) : t =
192192
in
193193
let m =
194194
match Ext_string.split ~keep_empty:true s ':' with
195-
| [path] -> {module_system = Esmodule; path; suffix = Literals.suffix_js}
195+
(* NEW: Just path - use configured module system and suffix *)
196+
| [path] ->
197+
{
198+
module_system = !Js_config.default_module_system;
199+
path;
200+
suffix = !Js_config.default_suffix;
201+
}
202+
(* OLD: module_system:path - use configured suffix *)
196203
| [module_system; path] ->
197204
{
198205
module_system = handle_module_system module_system;
199206
path;
200-
suffix = Literals.suffix_js;
207+
suffix = !Js_config.default_suffix;
201208
}
209+
(* OLD: Full format - all explicit *)
202210
| [module_system; path; suffix] ->
203211
{module_system = handle_module_system module_system; path; suffix}
204212
| _ -> Bsc_args.bad_arg ("invalid npm package path: " ^ s)

compiler/core/js_packages_info.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ val is_empty : t -> bool
5050

5151
val dump_packages_info : Format.formatter -> t -> unit
5252

53+
val module_system_of_string : string -> module_system option
54+
(** Parse module system from string (commonjs, esmodule, es6, es6-global) *)
55+
5356
val add_npm_package_path : t -> string -> t
5457
(** used by command line option
5558
e.g [-bs-package-output commonjs:xx/path]

compiler/core/lam_compile_main.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ let lambda_as_module
292292
: unit =
293293
let package_info = Js_packages_state.get_packages_info () in
294294
if Js_packages_info.is_empty package_info && !Js_config.js_stdout then begin
295-
Js_dump_program.dump_deps_program ~output_prefix Commonjs (lambda_output) stdout
295+
(* Use configured module system instead of hardcoded Commonjs *)
296+
Js_dump_program.dump_deps_program ~output_prefix !Js_config.default_module_system (lambda_output) stdout
296297
end else
297298
Js_packages_info.iter package_info (fun {module_system; path; suffix} ->
298299
let output_chan chan =

compiler/core/lam_compile_primitive.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ let get_module_system () =
4646
let package_info = Js_packages_state.get_packages_info () in
4747
let module_system =
4848
if Js_packages_info.is_empty package_info && !Js_config.js_stdout then
49-
[Ext_module_system.Commonjs]
49+
(* Use configured module system instead of hardcoded Commonjs *)
50+
[!Js_config.default_module_system]
5051
else
5152
Js_packages_info.map package_info (fun {module_system} -> module_system)
5253
in

rewatch/src/build/compile.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -493,25 +493,25 @@ pub fn compiler_args(
493493
specs
494494
.iter()
495495
.flat_map(|spec| {
496+
// Pass module system, suffix, and output path as separate flags
496497
vec![
498+
"-bs-module-system".to_string(),
499+
spec.module.clone(),
500+
"-bs-suffix".to_string(),
501+
root_config.get_suffix(spec),
497502
"-bs-package-output".to_string(),
498-
format!(
499-
"{}:{}:{}",
500-
spec.module,
501-
if spec.in_source {
502-
file_path.parent().unwrap().to_str().unwrap().to_string()
503-
} else {
504-
Path::new("lib")
505-
.join(Path::join(
506-
Path::new(&spec.get_out_of_source_dir()),
507-
file_path.parent().unwrap(),
508-
))
509-
.to_str()
510-
.unwrap()
511-
.to_string()
512-
},
513-
root_config.get_suffix(spec),
514-
),
503+
if spec.in_source {
504+
file_path.parent().unwrap().to_str().unwrap().to_string()
505+
} else {
506+
Path::new("lib")
507+
.join(Path::join(
508+
Path::new(&spec.get_out_of_source_dir()),
509+
file_path.parent().unwrap(),
510+
))
511+
.to_str()
512+
.unwrap()
513+
.to_string()
514+
},
515515
]
516516
})
517517
.collect()

0 commit comments

Comments
 (0)