Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lift the restriction that -bs-package-name is mandatory for -bs-package-output #384

Merged
merged 23 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
06ff546
Lift the restriction that `-bs-package-name` is mandatory for `-bs-pa…
anmonteiro Oct 26, 2022
a61e5bb
fix previous test
anmonteiro Oct 26, 2022
8215d02
Fix `-bs-module-type`, get the suffix from the `-o <filename>.bs.js`
anmonteiro Oct 26, 2022
e5b0939
fix
anmonteiro Oct 26, 2022
b37e524
update flake.lock
anmonteiro Oct 27, 2022
224c106
fix package output
anmonteiro Oct 27, 2022
c1bbfdf
no `-nopervasives`
anmonteiro Oct 27, 2022
766ba50
fix some code review items
anmonteiro Oct 27, 2022
0a75eb2
Merge remote-tracking branch 'origin/main' into anmonteiro/no-bs-pack…
anmonteiro Oct 27, 2022
f93e1a6
Separate `Js_packages_info` into "Batch" and "Separate" compilation
anmonteiro Oct 27, 2022
9791702
Wait until `[@@@bs.config { flags = [| ... |] }]` is processed before
anmonteiro Oct 27, 2022
6926995
wip (#388)
anmonteiro Oct 28, 2022
77486ae
Merge remote-tracking branch 'origin/main' into anmonteiro/no-bs-pack…
anmonteiro Oct 28, 2022
172c22e
Merge branch 'anmonteiro/no-bs-package-name' of github.com:melange-re…
anmonteiro Oct 28, 2022
3768ce6
update flake lock
anmonteiro Oct 28, 2022
e3f02b3
bundle output_info instead of passing suffix and module_system around
anmonteiro Oct 28, 2022
5948f8c
refactor: clearer definition of query_package_infos
anmonteiro Oct 28, 2022
832b84e
fix: type error
anmonteiro Oct 28, 2022
3d8a41a
fix test
anmonteiro Oct 28, 2022
52095b2
refactor: less bindings in `Js_name_of_module_id.string_of_module_id`
anmonteiro Oct 28, 2022
f9f9692
refactor: Js_name_of_module_id.get_runtime_module_path
anmonteiro Oct 28, 2022
8ea2bad
rename function
anmonteiro Oct 28, 2022
78cb849
refactor: improve readability of `Js_name_of_module_id.string_of_modu…
anmonteiro Oct 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
724 changes: 362 additions & 362 deletions dune.mel

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions jscomp/core/config_util.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand All @@ -17,20 +17,20 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

(** A simple wrapper around [Config] module in compiler-libs, so that the search path
(* A simple wrapper around [Config] module in compiler-libs, so that the search path
is the same
*)

val find_opt : string -> string option
(** [find filename] Input is a file name, output is absolute path *)
(* [find filename] Input is a file name, output is absolute path *)

val output_prefix : string -> string
(** given the input, calculate the output prefix
(* given the input, calculate the output prefix

in: src/hello.ast
out: src/hello
Expand Down
40 changes: 36 additions & 4 deletions jscomp/core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@
(source_tree ../../ocaml-tree))
(action
;; requires `npm install` in the `ocaml-tree` directory... what a mess
(run node --no-experimental-fetch ../../ocaml-tree/wasm.js -fold -i %{deps} -o %{targets})))
(run
node
--no-experimental-fetch
../../ocaml-tree/wasm.js
-fold
-i
%{deps}
-o
%{targets})))

(rule
(targets js_record_iter.ml)
Expand All @@ -34,7 +42,15 @@
j.ml
(source_tree ../../ocaml-tree))
(action
(run node --no-experimental-fetch ../../ocaml-tree/wasm.js -record-iter -i %{deps} -o %{targets})))
(run
node
--no-experimental-fetch
../../ocaml-tree/wasm.js
-record-iter
-i
%{deps}
-o
%{targets})))

(rule
(targets js_record_map.ml)
Expand All @@ -43,7 +59,15 @@
j.ml
(source_tree ../../ocaml-tree))
(action
(run node --no-experimental-fetch ../../ocaml-tree/wasm.js -record-map -i %{deps} -o %{targets})))
(run
node
--no-experimental-fetch
../../ocaml-tree/wasm.js
-record-map
-i
%{deps}
-o
%{targets})))

(rule
(targets js_record_fold.ml)
Expand All @@ -52,4 +76,12 @@
j.ml
(source_tree ../../ocaml-tree))
(action
(run node --no-experimental-fetch ../../ocaml-tree/wasm.js -record-fold -i %{deps} -o %{targets})))
(run
node
--no-experimental-fetch
../../ocaml-tree/wasm.js
-record-fold
-i
%{deps}
-o
%{targets})))
20 changes: 11 additions & 9 deletions jscomp/core/js_dump_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,26 @@ let dump_program (x : J.program) oc =
let[@inline] is_default (x : Js_op.kind) =
match x with External { default } -> default | _ -> false

let node_program ~output_dir f (x : J.deps_program) =
let node_program ~package_info ~output_dir f (x : J.deps_program) =
P.string f L.strict_directive;
P.newline f;
let cxt =
Js_dump_import_export.requires L.require Ext_pp_scope.empty f
(Ext_list.map x.modules (fun x ->
( x.id,
Js_name_of_module_id.string_of_module_id x ~output_dir NodeJS,
Js_name_of_module_id.string_of_module_id ~package_info x
~output_dir NodeJS,
is_default x.kind )))
in
program f cxt x.program

let es6_program ~output_dir fmt f (x : J.deps_program) =
let es6_program ~package_info ~output_dir fmt f (x : J.deps_program) =
let cxt =
Js_dump_import_export.imports Ext_pp_scope.empty f
(Ext_list.map x.modules (fun x ->
( x.id,
Js_name_of_module_id.string_of_module_id x ~output_dir fmt,
Js_name_of_module_id.string_of_module_id ~package_info x
~output_dir fmt,
is_default x.kind )))
in
let () = P.at_least_two_lines f in
Expand All @@ -99,7 +101,7 @@ let es6_program ~output_dir fmt f (x : J.deps_program) =
]}
*)

let pp_deps_program ~(output_prefix : string)
let pp_deps_program ~package_info ~(output_prefix : string)
(kind : Js_packages_info.module_system) (program : J.deps_program)
(f : Ext_pp.t) =
if not !Js_config.no_version_header then (
Expand All @@ -115,8 +117,8 @@ let pp_deps_program ~(output_prefix : string)
let output_dir = Filename.dirname output_prefix in
ignore
(match kind with
| Es6 | Es6_global -> es6_program ~output_dir kind f program
| NodeJS -> node_program ~output_dir f program);
| Es6 | Es6_global -> es6_program ~package_info ~output_dir kind f program
| NodeJS -> node_program ~package_info ~output_dir f program);
P.newline f;
P.string f
(match program.side_effect with
Expand All @@ -125,5 +127,5 @@ let pp_deps_program ~(output_prefix : string)
P.newline f;
P.flush f ()

let dump_deps_program ~output_prefix kind x (oc : out_channel) =
pp_deps_program ~output_prefix kind x (P.from_channel oc)
let dump_deps_program ~package_info ~output_prefix kind x (oc : out_channel) =
pp_deps_program ~package_info ~output_prefix kind x (P.from_channel oc)
6 changes: 4 additions & 2 deletions jscomp/core/js_dump_program.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(* Copyright (C) 2017 Authors of ReScript
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand All @@ -17,7 +17,7 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
Expand All @@ -26,13 +26,15 @@ val dump_program : J.program -> out_channel -> unit
(** only used for debugging purpose *)

val pp_deps_program :
package_info:Js_packages_info.t ->
output_prefix:string ->
Js_packages_info.module_system ->
J.deps_program ->
Ext_pp.t ->
unit

val dump_deps_program :
package_info:Js_packages_info.t ->
output_prefix:string ->
Js_packages_info.module_system ->
J.deps_program ->
Expand Down
3 changes: 2 additions & 1 deletion jscomp/core/js_implementation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ let implementation_cmj _ppf fname =
(* this is needed because the path is used to find other modules path *)
Res_compmisc.init_path ();
let cmj = Js_cmj_format.from_file fname in
Lam_compile_main.lambda_as_module cmj.delayed_program
Lam_compile_main.lambda_as_module ~package_info:cmj.package_spec
cmj.delayed_program
(Config_util.output_prefix fname)

let make_structure_item ~ns cunit : Parsetree.structure_item =
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/js_name_of_module_id.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ let get_runtime_module_path (dep_module_id : Lam_module_ident.t)
| Some path -> path // dep_path // js_file))

(* [output_dir] is decided by the command line argument *)
let string_of_module_id (dep_module_id : Lam_module_ident.t)
let string_of_module_id ~package_info (dep_module_id : Lam_module_ident.t)
~(output_dir : string) (module_system : Js_packages_info.module_system) :
string =
let current_package_info = Js_packages_state.get_packages_info () in
let current_package_info = package_info in
fix_path_for_windows
(match dep_module_id.kind with
| External { name } -> name (* the literal string for external package *)
Expand Down
5 changes: 3 additions & 2 deletions jscomp/core/js_name_of_module_id.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(* Copyright (C) 2017 Authors of ReScript
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand All @@ -17,7 +17,7 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
Expand All @@ -31,6 +31,7 @@
*)

val string_of_module_id :
package_info:Js_packages_info.t ->
Lam_module_ident.t ->
output_dir:string ->
Js_packages_info.module_system ->
Expand Down
77 changes: 50 additions & 27 deletions jscomp/core/js_packages_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ let module_system_of_string package_name : module_system option =
| "es6-global" -> Some Es6_global
| _ -> None

let module_system_to_string = function
anmonteiro marked this conversation as resolved.
Show resolved Hide resolved
| NodeJS -> Literals.commonjs
| Es6 -> Literals.es6
| Es6_global -> Literals.es6_global

let dump_package_info (fmt : Format.formatter)
({ module_system = ms; path = name; suffix } : package_info) =
Format.fprintf fmt "@[%s@ %s@ %s@]"
Expand Down Expand Up @@ -166,7 +171,18 @@ let query_package_infos ({ name; module_systems } as t : t)
let pkg_rel_path = name // rel_path in
Package_found { rel_path; pkg_rel_path; suffix = k.suffix }
| None -> Package_not_found)
| None -> Package_script
| None -> (
(* This represents the "implicit package flow". if `-bs-package-name` is
not present, assume we're all in the same package, and produce
relative imports *)
match
Ext_list.find_first module_systems (fun k ->
compatible k.module_system module_system)
with
| Some k ->
Package_found
{ rel_path = k.path; pkg_rel_path = k.path; suffix = k.suffix }
| None -> Package_script)

let get_js_path (x : t) (module_system : module_system) : string =
match
Expand All @@ -183,32 +199,39 @@ let get_output_dir (info : t) ~package_dir module_system =
Filename.concat package_dir (get_js_path info module_system)

let add_npm_package_path (packages_info : t) (s : string) : t =
if is_empty packages_info then
raise (Arg.Bad "please set package name first using -bs-package-name")
else
let handle_module_system module_system =
match module_system_of_string module_system with
| Some x -> x
| None -> raise (Arg.Bad ("invalid module system " ^ module_system))
in
let m =
match Ext_string.split ~keep_empty:true s ':' with
| [ path ] -> { module_system = NodeJS; path; suffix = Js }
| [ module_system; path ] ->
{
module_system = handle_module_system module_system;
path;
suffix = Js;
}
| [ module_system; path; suffix ] ->
{
module_system = handle_module_system module_system;
path;
suffix = Ext_js_suffix.of_string suffix;
}
| _ -> raise (Arg.Bad ("invalid npm package path: " ^ s))
in
{ packages_info with module_systems = m :: packages_info.module_systems }
let handle_module_system module_system =
match module_system_of_string module_system with
| Some x -> x
| None -> raise (Arg.Bad ("invalid module system " ^ module_system))
in
let m =
match Ext_string.split ~keep_empty:true s ':' with
| [ path ] -> { module_system = NodeJS; path; suffix = Js }
| [ module_system; path ] ->
{
module_system = handle_module_system module_system;
path;
suffix = Js;
}
| [ module_system; path; suffix ] ->
{
module_system = handle_module_system module_system;
path;
suffix = Ext_js_suffix.of_string suffix;
}
| _ -> raise (Arg.Bad ("invalid npm package path: " ^ s))
in
{ packages_info with module_systems = m :: packages_info.module_systems }

let bs_module_type_sentinel = "//MELANGE_INVALID_PACKAGE_OUTPUT//"

let add_npm_module_system ~suffix (packages_info : t) module_system =
let m = { module_system; path = bs_module_type_sentinel; suffix } in
{ packages_info with module_systems = [ m ] }

let is_module_type_flag = function
| { module_systems = [ ms ]; _ } -> ms.path = bs_module_type_sentinel
| _ -> false

(* support es6 modules instead
TODO: enrich ast to support import export
Expand Down