Skip to content

Commit 886ea1c

Browse files
committed
part 2: support external-stdlib config
1 parent 6d78909 commit 886ea1c

18 files changed

+1931
-1776
lines changed

docs/docson/build-schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@
481481
"type": "boolean",
482482
"description": "(Experimental) whether to use the OCaml standard library. Default: true"
483483
},
484+
"external-stdlib" : {
485+
"type" : "string",
486+
"description": "Use the external stdlib library instead of the one shipped with the compiler package"
487+
},
484488
"bs-external-includes": {
485489
"type": "array",
486490
"items": {

jscomp/bsb/bsb_build_schemas.ml

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let export_none = "none"
6565

6666

6767
let use_stdlib = "use-stdlib"
68+
let external_stdlib = "external-stdlib"
6869
let reason = "reason"
6970
let react_jsx = "react-jsx"
7071

jscomp/bsb/bsb_config_parse.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ let interpret_json
388388
js_post_build_cmd = (extract_js_post_build map per_proj_dir);
389389
package_specs =
390390
(match package_kind with
391-
| Toplevel -> Bsb_package_specs.from_map map
391+
| Toplevel -> Bsb_package_specs.from_map ~cwd:per_proj_dir map
392392
| Pinned_dependency x
393393
| Dependency x -> x);
394394
file_groups = groups;
@@ -412,7 +412,7 @@ let package_specs_from_bsconfig () =
412412
let json = Ext_json_parse.parse_json_from_file Literals.bsconfig_json in
413413
begin match json with
414414
| Obj {map} ->
415-
Bsb_package_specs.from_map map,
415+
Bsb_package_specs.from_map ~cwd:Bsb_global_paths.cwd map,
416416
extract_pinned_dependencies map
417417
| _ -> assert false
418418
end

jscomp/bsb/bsb_package_specs.ml

+19-5
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ type spec = {
3737
suffix : Ext_js_suffix.t
3838
}
3939

40+
(*FIXME: use assoc list instead *)
4041
module Spec_set = Set.Make( struct type t = spec
4142
let compare = Pervasives.compare
4243
end)
4344

4445
type t = {
4546
modules : Spec_set.t;
46-
(* runtime: Bsb_pkg_types.t option; *)
47+
runtime: string option;
48+
(* This has to be resolved as early as possible, since
49+
the path will be inherited in sub projects
50+
*)
4751
}
4852

4953
let (.?()) = Map_string.find_opt
@@ -147,10 +151,13 @@ let package_flag ({format; in_source; suffix } : spec) dir =
147151
(* FIXME: we should adapt it *)
148152
let package_flag_of_package_specs (package_specs : t)
149153
~(dirname : string ) : string =
150-
Spec_set.fold (fun format acc ->
154+
let res = Spec_set.fold (fun format acc ->
151155
Ext_string.inter2 acc (package_flag format dirname )
152-
) package_specs.modules Ext_string.empty
153-
156+
) package_specs.modules Ext_string.empty in
157+
match package_specs.runtime with
158+
| None -> res
159+
| Some x ->
160+
res ^ " -runtime " ^ x
154161
let default_package_specs suffix =
155162
Spec_set.singleton
156163
{ format = NodeJS ; in_source = false; suffix }
@@ -202,13 +209,20 @@ let extract_bs_suffix_exn (map : json_map) : Ext_js_suffix.t =
202209
Bsb_exception.config_error config
203210
"expect a string exteion like \".js\" here"
204211

205-
let from_map map =
212+
let from_map ~(cwd:string) map =
206213
let suffix = extract_bs_suffix_exn map in
207214
let modules = match map.?(Bsb_build_schemas.package_specs) with
208215
| Some x ->
209216
from_json suffix x
210217
| None -> default_package_specs suffix in
218+
let runtime =
219+
match map.?(Bsb_build_schemas.external_stdlib) with
220+
| None -> None
221+
| Some(Str{str; _}) ->
222+
Some (Bsb_pkg.resolve_bs_package ~cwd (Bsb_pkg_types.string_as_package str))
223+
| _ -> assert false in
211224
{
225+
runtime;
212226
modules
213227
}
214228

jscomp/bsb/bsb_package_specs.mli

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type t
2828

2929

3030
val from_map:
31+
cwd:string ->
3132
Ext_json_types.t Map_string.t -> t
3233

3334
val get_list_of_output_js :

jscomp/bsb/bsb_pkg.mli

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ val resolve_bs_package :
3838
cwd:string -> Bsb_pkg_types.t -> string
3939

4040

41+
(** used by watcher *)
4142
val to_list:
4243
(Bsb_pkg_types.t ->
4344
string ->

jscomp/bsb/bsb_pkg_types.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ let string_as_package (s : string) : t =
9595
if v = '@' then
9696
let scope_id =
9797
Ext_string.no_slash_idx s in
98-
assert (scope_id > 0);
98+
assert (scope_id > 0);
99+
(* better-eror message for invalid scope package:
100+
@rescript/std
101+
*)
99102
Scope(
100103
String.sub s (scope_id + 1) (len - scope_id - 1),
101104
String.sub s 0 scope_id

jscomp/common/js_config.ml

+1
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,4 @@ let as_ppx = ref false
9292

9393
let mono_empty_array = ref true
9494

95+
let customize_runtime = ref None

jscomp/common/js_config.mli

+1
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,4 @@ val no_export: bool ref
100100
val as_ppx : bool ref
101101

102102
val mono_empty_array : bool ref
103+
val customize_runtime : string option ref

jscomp/core/js_name_of_module_id.ml

+10-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,13 @@ let get_runtime_module_path
8080
(*Invariant: the package path to bs-platform, it is used to
8181
calculate relative js path
8282
*)
83-
((Filename.dirname (Filename.dirname Sys.executable_name)) // dep_path // js_file)
84-
(*FIXME: the runtime path needs changed when customize runtime*)
83+
(match !Js_config.customize_runtime with
84+
| None ->
85+
((Filename.dirname (Filename.dirname Sys.executable_name)) // dep_path // js_file)
86+
| Some path ->
87+
path //dep_path // js_file
88+
)
89+
8590

8691

8792
(* [output_dir] is decided by the command line argument *)
@@ -142,6 +147,9 @@ let string_of_module_id
142147
which is guaranteed by [-bs-package-output]
143148
*)
144149
else
150+
if Js_packages_info.is_runtime_package dep_package_info then
151+
get_runtime_module_path dep_module_id current_package_info module_system
152+
else
145153
begin match module_system with
146154
| NodeJS | Es6 ->
147155
dep_pkg.pkg_rel_path // js_file

jscomp/core/js_packages_info.ml

+13-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,19 @@ let runtime_test_package_specs : t = {
9494
name = Pkg_runtime;
9595
module_systems = []
9696
}
97-
let same_package_by_name (x : t) (y : t) = x.name = y.name
97+
98+
let same_package_by_name (x : t) (y : t) =
99+
match x.name with
100+
| Pkg_empty ->
101+
y.name = Pkg_empty
102+
| Pkg_runtime ->
103+
y.name = Pkg_runtime
104+
| Pkg_normal s ->
105+
begin match y.name with
106+
| Pkg_normal y -> s = y
107+
| Pkg_empty | Pkg_runtime -> false
108+
end
109+
98110

99111
let is_runtime_package (x : t) =
100112
x.name = Pkg_runtime

jscomp/main/js_main.ml

+14-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ let setup_error_printer (syntax_kind : [ `ml | `reason | `rescript ])=
3232

3333

3434

35-
35+
let setup_runtime_path path =
36+
let u0 = Filename.dirname path in
37+
let std = Filename.basename path in
38+
let _path = Filename.dirname u0 in
39+
let rescript = Filename.basename u0 in
40+
(match rescript.[0] with
41+
| '@' -> (* scoped package *)
42+
Bs_version.package_name := rescript ^ "/" ^ std;
43+
| _ -> Bs_version.package_name := std
44+
| exception _ ->
45+
Bs_version.package_name := std);
46+
Js_config.customize_runtime := Some path
3647

3748
let handle_reason (type a) (kind : a Ml_binary.kind) sourcefile ppf =
3849
setup_error_printer `reason;
@@ -443,6 +454,8 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
443454
"<list> Enable or disable error status for warnings according\n\
444455
to <list>. See option -w for the syntax of <list>.\n\
445456
Default setting is " ^ Bsc_warnings.defaults_warn_error;
457+
"-runtime",string_call setup_runtime_path,
458+
"*internal* Set the runtime directory";
446459
"-make-runtime", unit_call Js_packages_state.make_runtime,
447460
"*internal* make runtime library";
448461
"-make-runtime-test", unit_call Js_packages_state.make_runtime_test,

0 commit comments

Comments
 (0)