Skip to content

Commit 71d8f6c

Browse files
committed
Extract -bs-package-output into multiple arguments
1 parent b19ae1e commit 71d8f6c

9 files changed

Lines changed: 58 additions & 24 deletions

File tree

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
@@ -294,7 +294,8 @@ let lambda_as_module
294294
: unit =
295295
let package_info = Js_packages_state.get_packages_info () in
296296
if Js_packages_info.is_empty package_info && !Js_config.js_stdout then begin
297-
Js_dump_program.dump_deps_program ~output_prefix Commonjs (lambda_output) stdout
297+
(* Use configured module system instead of hardcoded Commonjs *)
298+
Js_dump_program.dump_deps_program ~output_prefix !Js_config.default_module_system (lambda_output) stdout
298299
end else
299300
Js_packages_info.iter package_info (fun {module_system; path; suffix} ->
300301
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
@@ -532,25 +532,25 @@ pub fn compiler_args(
532532
specs
533533
.iter()
534534
.flat_map(|spec| {
535+
// Pass module system, suffix, and output path as separate flags
535536
vec![
537+
"-bs-module-system".to_string(),
538+
spec.module.clone(),
539+
"-bs-suffix".to_string(),
540+
root_config.get_suffix(spec),
536541
"-bs-package-output".to_string(),
537-
format!(
538-
"{}:{}:{}",
539-
spec.module,
540-
if spec.in_source {
541-
file_path.parent().unwrap().to_str().unwrap().to_string()
542-
} else {
543-
Path::new("lib")
544-
.join(Path::join(
545-
Path::new(&spec.get_out_of_source_dir()),
546-
file_path.parent().unwrap(),
547-
))
548-
.to_str()
549-
.unwrap()
550-
.to_string()
551-
},
552-
root_config.get_suffix(spec),
553-
),
542+
if spec.in_source {
543+
file_path.parent().unwrap().to_str().unwrap().to_string()
544+
} else {
545+
Path::new("lib")
546+
.join(Path::join(
547+
Path::new(&spec.get_out_of_source_dir()),
548+
file_path.parent().unwrap(),
549+
))
550+
.to_str()
551+
.unwrap()
552+
.to_string()
553+
},
554554
]
555555
})
556556
.collect()

0 commit comments

Comments
 (0)