Skip to content

Commit d7fd9d8

Browse files
committed
fuse bsconfig into package.json
1 parent a459fcc commit d7fd9d8

File tree

7 files changed

+111
-69
lines changed

7 files changed

+111
-69
lines changed

jscomp/bsb/bsb_build_schemas.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,8 @@ let language = "language"
123123
let path = "path"
124124

125125
let ignored_dirs = "ignored-dirs"
126+
127+
module PackageJson = struct
128+
let dependencies = "dependencies"
129+
let devDependencies = "devDependencies"
130+
end

jscomp/bsb/bsb_build_util.ml

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,7 @@ let get_list_string_acc (s : Ext_json_types.t array) acc =
125125

126126
let get_list_string s = get_list_string_acc s []
127127

128-
(* Key is the path *)
129-
let ( |? ) m (key, cb) = m |> Ext_json.test key cb
130-
131128
type top = Expect_none | Expect_name of string
132-
133129
type package_context = { proj_dir : string; top : top }
134130

135131
(**
@@ -148,8 +144,8 @@ let pp_packages_rev ppf lst =
148144

149145
let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
150146
~(top : top) (dir : string) (queue : _ Queue.t) ~pinned_dependencies =
151-
let bsconfig_json = dir // Literals.bsconfig_json in
152-
match Ext_json_parse.parse_json_from_file bsconfig_json with
147+
let package_json = dir // Literals.package_json in
148+
match Ext_json_parse.parse_json_from_file package_json with
153149
| Obj { map; loc } ->
154150
let cur_package_name =
155151
match Map_string.find_opt map Bsb_build_schemas.name with
@@ -162,7 +158,7 @@ let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
162158
"package name is expected to be %s but got %s" s str);
163159
str
164160
| Some _ | None ->
165-
Bsb_exception.errorf ~loc "package name missing in %s/bsconfig.json"
161+
Bsb_exception.errorf ~loc "package name missing in %s/package.json"
166162
dir
167163
in
168164
if Ext_list.mem_string paths cur_package_name then (
@@ -174,31 +170,40 @@ let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
174170
if Hash_string.mem visited cur_package_name then
175171
Bsb_log.info "@{<info>Visited before@} %s@." cur_package_name
176172
else
177-
let explore_deps (deps : string) =
178-
map
179-
|? ( deps,
180-
`Arr
181-
(fun (new_packages : Ext_json_types.t array) ->
182-
Ext_array.iter new_packages (fun js ->
183-
match js with
184-
| Str { str = new_package } ->
185-
let package_dir =
186-
Bsb_pkg.resolve_bs_package ~cwd:dir
187-
(Bsb_pkg_types.string_as_package new_package)
188-
in
189-
walk_all_deps_aux visited package_stacks
190-
~top:(Expect_name new_package) package_dir queue
191-
~pinned_dependencies
192-
| _ ->
193-
Bsb_exception.errorf ~loc "%s expect an array" deps))
194-
)
195-
|> ignore
173+
let explore_deps_in_package_json (field : string) =
174+
match Map_string.find_opt map field with
175+
| Some (Obj { map = dependencies }) ->
176+
let bsb_deps =
177+
Map_string.keys dependencies
178+
|> List.filter (fun dep ->
179+
let dep_dir =
180+
Bsb_pkg.resolve_bs_package ~cwd:dir
181+
(Bsb_pkg_types.string_as_package dep)
182+
in
183+
let bsconfig = dep_dir // Literals.bsconfig_json in
184+
(* NOTE: "rescript" package should be ignore *)
185+
dep <> Literals.rescript && Sys.file_exists bsconfig)
186+
in
187+
List.iter
188+
(fun dep ->
189+
let package_dir =
190+
Bsb_pkg.resolve_bs_package ~cwd:dir
191+
(Bsb_pkg_types.string_as_package dep)
192+
in
193+
walk_all_deps_aux visited package_stacks
194+
~top:(Expect_name dep) package_dir queue
195+
~pinned_dependencies)
196+
bsb_deps
197+
| _ -> ()
196198
in
197-
explore_deps Bsb_build_schemas.bs_dependencies;
199+
explore_deps_in_package_json Bsb_build_schemas.PackageJson.dependencies;
198200
(match top with
199-
| Expect_none -> explore_deps Bsb_build_schemas.bs_dev_dependencies
201+
| Expect_none ->
202+
explore_deps_in_package_json
203+
Bsb_build_schemas.PackageJson.devDependencies
200204
| Expect_name n when Set_string.mem pinned_dependencies n ->
201-
explore_deps Bsb_build_schemas.bs_dev_dependencies
205+
explore_deps_in_package_json
206+
Bsb_build_schemas.PackageJson.devDependencies
202207
| Expect_name _ -> ());
203208
Queue.add { top; proj_dir = dir } queue;
204209
Hash_string.add visited cur_package_name dir

jscomp/bsb/bsb_config_parse.ml

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,30 @@ type json_map = Ext_json_types.t Map_string.t
3636

3737
(* Key is the path *)
3838
let ( |? ) m (key, cb) = m |> Ext_json.test key cb
39-
4039
let ( .?() ) = Map_string.find_opt
4140

4241
(*TODO: it is a little mess that [cwd] and [project dir] are shared*)
4342

44-
let extract_package_name_and_namespace (map : json_map) : string * string option
45-
=
46-
let package_name =
47-
match map.?(Bsb_build_schemas.name) with
48-
| Some (Str { str = "_" } as config) ->
49-
Bsb_exception.config_error config "_ is a reserved package name"
50-
| Some (Str { str = name }) -> name
51-
| Some config ->
52-
Bsb_exception.config_error config "name expect a string field"
53-
| None -> Bsb_exception.invalid_spec "field name is required"
54-
in
55-
let namespace =
56-
match map.?(Bsb_build_schemas.namespace) with
57-
| None | Some (False _) -> None
58-
| Some (True _) ->
59-
Some (Ext_namespace.namespace_of_package_name package_name)
60-
| Some (Str { str }) ->
61-
(*TODO : check the validity of namespace *)
62-
Some (Ext_namespace.namespace_of_package_name str)
63-
| Some x ->
64-
Bsb_exception.config_error x "namespace field expects string or boolean"
65-
in
66-
(package_name, namespace)
43+
let extract_package_name (map : json_map) =
44+
match map.?(Bsb_build_schemas.name) with
45+
| Some (Str { str = "_" } as config) ->
46+
Bsb_exception.package_json_config_error config
47+
"_ is a reserved package name"
48+
| Some (Str { str = name }) -> name
49+
| Some config ->
50+
Bsb_exception.package_json_config_error config
51+
"name expect a string field"
52+
| None -> Bsb_exception.invalid_package_json_spec "field name is required"
53+
54+
let extract_namespace (map : json_map) package_name =
55+
match map.?(Bsb_build_schemas.namespace) with
56+
| None | Some (False _) -> None
57+
| Some (True _) -> Some (Ext_namespace.namespace_of_package_name package_name)
58+
| Some (Str { str }) ->
59+
(*TODO : check the validity of namespace *)
60+
Some (Ext_namespace.namespace_of_package_name str)
61+
| Some x ->
62+
Bsb_exception.config_error x "namespace field expects string or boolean"
6763

6864
(**
6965
There are two things to check:
@@ -190,14 +186,24 @@ let extract_generators (map : json_map) =
190186
(Bsb_build_schemas.generators ^ " expect an array field"));
191187
!generators
192188

193-
let extract_dependencies (map : json_map) cwd (field : string) :
194-
Bsb_config_types.dependencies =
189+
let extract_dependencies_from_package_json (map : json_map) cwd (field : string)
190+
: Bsb_config_types.dependencies =
195191
match map.?(field) with
196192
| None -> []
197-
| Some (Arr { content = s }) ->
198-
Ext_list.map (Bsb_build_util.get_list_string s) (fun s ->
199-
resolve_package cwd (Bsb_pkg_types.string_as_package s))
200-
| Some config -> Bsb_exception.config_error config (field ^ " expect an array")
193+
| Some (Obj { map = dependencies }) ->
194+
let bsb_deps =
195+
Map_string.keys dependencies
196+
|> List.filter (fun dep ->
197+
let bsconfig =
198+
Bsb_pkg.resolve_bs_package ~cwd
199+
(Bsb_pkg_types.string_as_package dep)
200+
in
201+
Sys.file_exists bsconfig)
202+
in
203+
Ext_list.map bsb_deps (fun dep ->
204+
resolve_package cwd (Bsb_pkg_types.string_as_package dep))
205+
| Some config ->
206+
Bsb_exception.package_json_config_error config (field ^ " expect a object")
201207

202208
(* return an empty array if not found *)
203209
let extract_string_list (map : json_map) (field : string) : string list =
@@ -251,6 +257,11 @@ let extract_js_post_build (map : json_map) cwd : string option =
251257
|> ignore;
252258
!js_post_build_cmd
253259

260+
let read_packge_json cwd =
261+
match Ext_json_parse.parse_json_from_file (cwd // Literals.package_json) with
262+
| Obj { map } -> map
263+
| _ -> Bsb_exception.invalid_package_json_spec "expect a json object {}"
264+
254265
(** ATT: make sure such function is re-entrant.
255266
With a given [cwd] it works anywhere*)
256267
let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
@@ -273,7 +284,11 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
273284
Ext_json_parse.parse_json_from_file (per_proj_dir // Literals.bsconfig_json)
274285
with
275286
| Obj { map } -> (
276-
let package_name, namespace = extract_package_name_and_namespace map in
287+
let map_json = read_packge_json per_proj_dir in
288+
289+
let package_name = extract_package_name map_json in
290+
let namespace = extract_namespace map package_name in
291+
277292
let gentype_config = extract_gentype_config map per_proj_dir in
278293

279294
(* This line has to be before any calls to Bsb_global_backend.backend, because it'll read the entries
@@ -294,13 +309,14 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
294309
in
295310
let reason_react_jsx = extract_reason_react_jsx map in
296311
let bs_dependencies =
297-
extract_dependencies map per_proj_dir Bsb_build_schemas.bs_dependencies
312+
extract_dependencies_from_package_json map_json per_proj_dir
313+
Bsb_build_schemas.PackageJson.dependencies
298314
in
299315
let bs_dev_dependencies =
300316
match package_kind with
301317
| Toplevel | Pinned_dependency _ ->
302-
extract_dependencies map per_proj_dir
303-
Bsb_build_schemas.bs_dev_dependencies
318+
extract_dependencies_from_package_json map_json per_proj_dir
319+
Bsb_build_schemas.PackageJson.devDependencies
304320
| Dependency _ -> []
305321
in
306322
let pinned_dependencies = extract_pinned_dependencies map in

jscomp/bsb/bsb_exception.ml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@
2525
type error =
2626
| Package_not_found of Bsb_pkg_types.t * string option (* json file *)
2727
| Json_config of Ext_position.t * string
28+
| PackageJson_config of Ext_position.t * string
2829
| Invalid_json of string
2930
| Invalid_spec of string
31+
| Invalid_package_json_spec of string
3032
| Conflict_module of string * string * string
3133
| No_implementation of string
3234
| Not_consistent of string
3335

3436
exception Error of error
3537

3638
let error err = raise (Error err)
37-
3839
let package_not_found ~pkg ~json = error (Package_not_found (pkg, json))
3940

4041
let print (fmt : Format.formatter) (x : error) =
@@ -58,13 +59,13 @@ let print (fmt : Format.formatter) (x : error) =
5859
let name = Bsb_pkg_types.to_string name in
5960
if Ext_string.equal name !Bs_version.package_name then
6061
Format.fprintf fmt
61-
"File \"bsconfig.json\", line 1\n\
62+
"File \"package.json\", line 1\n\
6263
@{<error>Error:@} package @{<error>%s@} is not found %s\n\
6364
It's the basic, required package. If you have it installed globally,\n\
6465
Please run `npm link rescript` to make it available" name in_json
6566
else
6667
Format.fprintf fmt
67-
"File \"bsconfig.json\", line 1\n\
68+
"File \"package.json\", line 1\n\
6869
@{<error>Error:@} package @{<error>%s@} not found or built %s\n\
6970
- Did you install it?\n\
7071
- If you did, did you run `rescript build -with-deps`?" name in_json
@@ -75,8 +76,13 @@ let print (fmt : Format.formatter) (x : error) =
7576
For more details, please checkout the schema \
7677
https://rescript-lang.org/docs/manual/latest/build-configuration-schema"
7778
pos.pos_fname pos.pos_lnum s
79+
| PackageJson_config (pos, s) ->
80+
Format.fprintf fmt "File %S, line %d:\n@{<error>Error:@} %s" pos.pos_fname
81+
pos.pos_lnum s
7882
| Invalid_spec s ->
7983
Format.fprintf fmt "@{<error>Error: Invalid bsconfig.json %s@}" s
84+
| Invalid_package_json_spec s ->
85+
Format.fprintf fmt "@{<error>Error: Invalid package.json %s@}" s
8086
| Invalid_json s ->
8187
Format.fprintf fmt
8288
"File %S, line 1\n@{<error>Error: Invalid json format@}" s
@@ -85,7 +91,6 @@ let conflict_module modname dir1 dir2 =
8591
Error (Conflict_module (modname, dir1, dir2))
8692

8793
let no_implementation modname = error (No_implementation modname)
88-
8994
let not_consistent modname = error (Not_consistent modname)
9095

9196
let errorf ~loc fmt =
@@ -96,8 +101,13 @@ let config_error config fmt =
96101

97102
error (Json_config (loc, fmt))
98103

99-
let invalid_spec s = error (Invalid_spec s)
104+
let package_json_config_error config fmt =
105+
let loc = Ext_json.loc_of config in
106+
107+
error (PackageJson_config (loc, fmt))
100108

109+
let invalid_spec s = error (Invalid_spec s)
110+
let invalid_package_json_spec s = error (Invalid_package_json_spec s)
101111
let invalid_json s = error (Invalid_json s)
102112

103113
let () =

jscomp/bsb/bsb_exception.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ val errorf : loc:Ext_position.t -> ('a, unit, string, 'b) format4 -> 'a
3939

4040
val config_error : Ext_json_types.t -> string -> 'a
4141

42+
val package_json_config_error : Ext_json_types.t -> string -> 'a
43+
4244
val invalid_spec : string -> 'a
4345

4446
val invalid_json : string -> 'a
4547

48+
val invalid_package_json_spec: string -> 'a
49+
4650
val no_implementation : string -> 'a
4751

4852
val not_consistent : string -> 'a

jscomp/ext/literals.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,5 @@ let lazy_done = "LAZY_DONE"
178178
let lazy_val = "VAL"
179179

180180
let pure = "@__PURE__"
181+
182+
let rescript = "rescript"

jscomp/super_errors/super_typetexp.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ let report_error env ppf = function
7171
- Did you list it in bsconfig.json?@,\
7272
- @[Did you run `rescript build` instead of `rescript build -with-deps`@ (latter builds third-parties)@]?\
7373
@]@,\
74-
- Did you include the file's directory in bsconfig.json?@]\
74+
- Did you include the file's directory in package.json?@]\
7575
@]"
7676
Printtyp.longident lid
7777
end

0 commit comments

Comments
 (0)