Skip to content

Commit

Permalink
Lift the restriction that -bs-package-name is mandatory for `-bs-pa…
Browse files Browse the repository at this point in the history
…ckage-output` (#384)

* Lift the restriction that `-bs-package-name` is mandatory for `-bs-package-output`

* fix previous test

* Fix `-bs-module-type`, get the suffix from the `-o <filename>.bs.js`

* fix

* update flake.lock

* fix package output

* no `-nopervasives`

* fix some code review items

* Separate `Js_packages_info` into "Batch" and "Separate" compilation

allows to fix es6 generation from separate compilation in a way that's
easier to reason about

* Wait until `[@@@bs.config { flags = [| ... |] }]` is processed before
dereferencing package specs

* wip (#388)

* wip

* fix / comment

* update flake lock

* bundle output_info instead of passing suffix and module_system around

* refactor: clearer definition of query_package_infos

* fix: type error

* fix test

* refactor: less bindings in `Js_name_of_module_id.string_of_module_id`

* refactor: Js_name_of_module_id.get_runtime_module_path

* rename function

* refactor: improve readability of `Js_name_of_module_id.string_of_module_id`
  • Loading branch information
anmonteiro committed Oct 28, 2022
1 parent d9bcc98 commit 2293d9a
Show file tree
Hide file tree
Showing 24 changed files with 1,007 additions and 676 deletions.
724 changes: 362 additions & 362 deletions dune.mel

Large diffs are not rendered by default.

14 changes: 7 additions & 7 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})))
28 changes: 16 additions & 12 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_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 ~output_info
~output_dir x,
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_info ~output_dir 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 ~output_info,
is_default x.kind )))
in
let () = P.at_least_two_lines f in
Expand All @@ -99,9 +101,8 @@ let es6_program ~output_dir fmt f (x : J.deps_program) =
]}
*)

let pp_deps_program ~(output_prefix : string)
(kind : Js_packages_info.module_system) (program : J.deps_program)
(f : Ext_pp.t) =
let pp_deps_program ~package_info ~(output_info : Js_packages_info.output_info)
~(output_prefix : string) (f : Ext_pp.t) (program : J.deps_program) =
if not !Js_config.no_version_header then (
P.string f Bs_version.header;
P.newline f);
Expand All @@ -114,9 +115,10 @@ let pp_deps_program ~(output_prefix : string)
P.newline f);
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);
(match output_info.module_system with
| Js_packages_info.Es6 | Es6_global ->
es6_program ~package_info ~output_dir ~output_info f program
| NodeJS -> node_program ~package_info ~output_info ~output_dir f program);
P.newline f;
P.string f
(match program.side_effect with
Expand All @@ -125,5 +127,7 @@ 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_info ~output_prefix x
(oc : out_channel) =
pp_deps_program ~package_info ~output_info ~output_prefix (P.from_channel oc)
x
14 changes: 4 additions & 10 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,24 +17,18 @@
* 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. *)

val dump_program : J.program -> out_channel -> unit
(** only used for debugging purpose *)

val pp_deps_program :
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_info:Js_packages_info.output_info ->
output_prefix:string ->
Js_packages_info.module_system ->
J.deps_program ->
out_channel ->
unit
14 changes: 9 additions & 5 deletions jscomp/core/js_implementation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
|> Lam_compile_main.compile outputprefix
in
if not !Js_config.cmj_only then
Lam_compile_main.lambda_as_module js_program outputprefix);
(* XXX(anmonteiro): important that we get package_info after
processing, as `[@@@config {flags = [| ... |]}]` could have added to
package specs. *)
let package_info = Js_packages_state.get_packages_info () in
Lam_compile_main.lambda_as_module ~package_info js_program outputprefix);
process_with_gentype (outputprefix ^ ".cmt")

let implementation ~parser ~lang ppf fname =
Expand Down Expand Up @@ -213,7 +217,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 All @@ -223,9 +228,8 @@ let make_structure_item ~ns cunit : Parsetree.structure_item =
(Mb.mk { txt = Some cunit; loc }
(Mod.ident { txt = Lident (Ext_namespace_encode.make ~ns cunit); loc }))

(** decoding [.mlmap]
keep in sync {!Bsb_namespace_map_gen.output}
*)
(* decoding [.mlmap]
keep in sync {!Bsb_namespace_map_gen.output} *)
let implementation_map ppf sourcefile =
let () = Js_config.cmj_only := true in
let ichan = open_in_bin sourcefile in
Expand Down

0 comments on commit 2293d9a

Please sign in to comment.