Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analysis/examples/larger-project/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"package-specs": {
"module": "es6",
"module": "esmodule",
"in-source": true
}
}
2 changes: 1 addition & 1 deletion analysis/examples/workspace-project/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "workspace-project",
"sources": [],
"package-specs": {
"module": "es6",
"module": "esmodule",
"in-source": true
},
"suffix": ".mjs",
Expand Down
4 changes: 2 additions & 2 deletions compiler/core/js_dump_import_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ let exports cxt f (idents : Ident.t list) =
(fun _ -> P.newline f);
outer_cxt

(** Print module in ES6 format, it is ES6, trailing comma is valid ES6 code *)
let es6_export cxt f (idents : Ident.t list) =
(** Print module in ESModule format, it is ESModule, trailing comma is valid ES code *)
let esmodule_export cxt f (idents : Ident.t list) =
P.at_least_two_lines f;
match idents with
| [] -> cxt
Expand Down
3 changes: 2 additions & 1 deletion compiler/core/js_dump_import_export.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ val default_export : string

val exports : Ext_pp_scope.t -> Ext_pp.t -> Ident.t list -> Ext_pp_scope.t

val es6_export : Ext_pp_scope.t -> Ext_pp.t -> Ident.t list -> Ext_pp_scope.t
val esmodule_export :
Ext_pp_scope.t -> Ext_pp.t -> Ident.t list -> Ext_pp_scope.t

val requires :
string ->
Expand Down
10 changes: 5 additions & 5 deletions compiler/core/js_dump_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ let[@inline] is_default (x : Js_op.kind) =
| External {default} -> default
| _ -> false

let node_program ~output_dir f (x : J.deps_program) =
let commonjs_program ~output_dir f (x : J.deps_program) =
P.string f L.strict_directive;
P.newline f;
let cxt =
Expand All @@ -87,7 +87,7 @@ let node_program ~output_dir f (x : J.deps_program) =
in
program f cxt x.program

let es6_program ~output_dir fmt f (x : J.deps_program) =
let esmodule_program ~output_dir fmt f (x : J.deps_program) =
let cxt =
Js_dump_import_export.imports Ext_pp_scope.empty f
(* Not be emitted in import statements *)
Expand All @@ -105,7 +105,7 @@ let es6_program ~output_dir fmt f (x : J.deps_program) =
in
let () = P.at_least_two_lines f in
let cxt = Js_dump.statements true cxt f x.program.block in
Js_dump_import_export.es6_export cxt f x.program.exports
Js_dump_import_export.esmodule_export cxt f x.program.exports

let pp_deps_program ~(output_prefix : string)
(kind : Js_packages_info.module_system) (program : J.deps_program)
Expand All @@ -128,8 +128,8 @@ let pp_deps_program ~(output_prefix : string)
let output_dir = Filename.dirname output_prefix in
ignore
(match kind with
| Esmodule | Es6_global -> es6_program ~output_dir kind f program
| Commonjs -> node_program ~output_dir f program);
| Esmodule -> esmodule_program ~output_dir kind f program
| Commonjs -> commonjs_program ~output_dir f program);
P.newline f;
P.string f
(match program.side_effect with
Expand Down
40 changes: 2 additions & 38 deletions compiler/core/js_name_of_module_id.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,7 @@ let get_runtime_module_path
which is guaranteed by [-bs-package-output]
*)
else
match module_system with
| Commonjs | Esmodule ->
Js_packages_info.runtime_package_path module_system js_file
(* Note we did a post-processing when working on Windows *)
| Es6_global
->
(* lib/ocaml/xx.cmj --
HACKING: FIXME
maybe we can caching relative package path calculation or employ package map *)
(* assert false *)
Ext_path.rel_normalized_absolute_path
~from:(
Js_packages_info.get_output_dir
current_package_info
~package_dir:(Lazy.force Ext_path.package_dir)
module_system )
(*Invariant: the package path to rescript, it is used to
calculate relative js path
*)
(!Runtime_package.path // dep_path // js_file)
Js_packages_info.runtime_package_path module_system js_file

(* [output_dir] is decided by the command line argument *)
let string_of_module_id
Expand Down Expand Up @@ -157,24 +138,7 @@ let string_of_module_id
else
if Js_packages_info.is_runtime_package dep_package_info then
get_runtime_module_path dep_module_id current_package_info module_system
else
begin match module_system with
| Commonjs | Esmodule ->
dep_pkg.pkg_rel_path // js_file
(* Note we did a post-processing when working on Windows *)
| Es6_global
->
begin
Ext_path.rel_normalized_absolute_path
~from:(
Js_packages_info.get_output_dir
current_package_info
~package_dir:(Lazy.force Ext_path.package_dir)
module_system
)
(package_path // dep_pkg.rel_path // js_file)
end
end
else dep_pkg.pkg_rel_path // js_file
| Package_script, Package_script
->
let js_file =
Expand Down
9 changes: 3 additions & 6 deletions compiler/core/js_packages_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ let compatible (dep : module_system) (query : module_system) =
match query with
| Commonjs -> dep = Commonjs
| Esmodule -> dep = Esmodule
| Es6_global -> dep = Es6_global || dep = Esmodule
(* As a dependency Leaf Node, it is the same either [global] or [not] *)

type package_info = {module_system: module_system; path: string; suffix: string}
Expand All @@ -41,11 +40,11 @@ type package_name = Pkg_empty | Pkg_runtime | Pkg_normal of string

let ( // ) = Filename.concat

(* in runtime lib, [es6] and [es6] are treated the same wway *)
(* We keep the dir names "js" and "es6" (instead of "commonjs" and "esmodule") for compatibility. *)
let runtime_dir_of_module_system (ms : module_system) =
match ms with
| Commonjs -> "js"
| Esmodule | Es6_global -> "es6"
| Esmodule -> "es6"

let runtime_package_path (ms : module_system) js_file =
Runtime_package.name // "lib" // runtime_dir_of_module_system ms // js_file
Expand Down Expand Up @@ -101,13 +100,11 @@ let string_of_module_system (ms : module_system) =
match ms with
| Commonjs -> "CommonJS"
| Esmodule -> "ESModule"
| Es6_global -> "Es6_global"

let module_system_of_string package_name : module_system option =
match package_name with
| "commonjs" -> Some Commonjs
| "esmodule" | "es6" -> Some Esmodule
| "es6-global" -> Some Es6_global
| "esmodule" -> Some Esmodule
| _ -> None

let dump_package_info (fmt : Format.formatter)
Expand Down
2 changes: 1 addition & 1 deletion compiler/ext/ext_module_system.ml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
type t = Commonjs | Esmodule | Es6_global
type t = Commonjs | Esmodule
143 changes: 0 additions & 143 deletions compiler/ext/ext_path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ type t =
| Dir of string
[@@unboxed]

let simple_convert_node_path_to_os_path =
if Sys.unix then fun x -> x
else if Sys.win32 || Sys.cygwin then Ext_string.replace_slash_backward
else failwith ("Unknown OS : " ^ Sys.os_type)

let cwd = lazy (Sys.getcwd ())

let split_by_sep_per_os : string -> string list =
Expand Down Expand Up @@ -80,149 +75,18 @@ let node_rebase_file ~from ~to_ file =
else node_relative_path ~from:(Dir from) (Dir to_))
file

(***
{[
Filename.concat "." "";;
"./"
]}
*)
let combine path1 path2 =
if Filename.is_relative path2 then
if Ext_string.is_empty path2 then path1
else if path1 = Filename.current_dir_name then path2
else if path2 = Filename.current_dir_name then path1
else Filename.concat path1 path2
else path2

let ( // ) x y =
if x = Filename.current_dir_name then y
else if y = Filename.current_dir_name then x
else Filename.concat x y

(**
{[
split_aux "//ghosg//ghsogh/";;
- : string * string list = ("/", ["ghosg"; "ghsogh"])
]}
Note that
{[
Filename.dirname "/a/" = "/"
Filename.dirname "/a/b/" = Filename.dirname "/a/b" = "/a"
]}
Special case:
{[
basename "//" = "/"
basename "///" = "/"
]}
{[
basename "" = "."
basename "" = "."
dirname "" = "."
dirname "" = "."
]}
*)
let split_aux p =
let rec go p acc =
let dir = Filename.dirname p in
if dir = p then (dir, acc)
else
let new_path = Filename.basename p in
if Ext_string.equal new_path Filename.dir_sep then go dir acc
(* We could do more path simplification here
leave to [rel_normalized_absolute_path]
*)
else go dir (new_path :: acc)
in

go p []

(**
TODO: optimization
if [from] and [to] resolve to the same path, a zero-length string is returned

This function is useed in [es6-global] and
[amdjs-global] format and tailored for `rollup`
*)
let rel_normalized_absolute_path ~from to_ =
let root1, paths1 = split_aux from in
let root2, paths2 = split_aux to_ in
if root1 <> root2 then root2
else
let rec go xss yss =
match (xss, yss) with
| x :: xs, y :: ys ->
if Ext_string.equal x y then go xs ys
else if x = Filename.current_dir_name then go xs yss
else if y = Filename.current_dir_name then go xss ys
else
let start =
Ext_list.fold_left xs Ext_string.parent_dir_lit (fun acc _ ->
acc // Ext_string.parent_dir_lit)
in
Ext_list.fold_left yss start (fun acc v -> acc // v)
| [], [] -> Ext_string.empty
| [], y :: ys -> Ext_list.fold_left ys y (fun acc x -> acc // x)
| _ :: xs, [] ->
Ext_list.fold_left xs Ext_string.parent_dir_lit (fun acc _ ->
acc // Ext_string.parent_dir_lit)
in
let v = go paths1 paths2 in

if Ext_string.is_empty v then Literals.node_current
else if
v = "." || v = ".."
|| Ext_string.starts_with v "./"
|| Ext_string.starts_with v "../"
then v
else "./" ^ v

(*TODO: could be hgighly optimized later
{[
normalize_absolute_path "/gsho/./..";;

normalize_absolute_path "/a/b/../c../d/e/f";;

normalize_absolute_path "/gsho/./..";;

normalize_absolute_path "/gsho/./../..";;

normalize_absolute_path "/a/b/c/d";;

normalize_absolute_path "/a/b/c/d/";;

normalize_absolute_path "/a/";;

normalize_absolute_path "/a";;
]}
*)

(** See tests in {!Ounit_path_tests} *)
let normalize_absolute_path x =
let drop_if_exist xs =
match xs with
| [] -> []
| _ :: xs -> xs
in
let rec normalize_list acc paths =
match paths with
| [] -> acc
| x :: xs ->
if Ext_string.equal x Ext_string.current_dir_lit then
normalize_list acc xs
else if Ext_string.equal x Ext_string.parent_dir_lit then
normalize_list (drop_if_exist acc) xs
else normalize_list (x :: acc) xs
in
let root, paths = split_aux x in
let rev_paths = normalize_list [] paths in
let rec go acc rev_paths =
match rev_paths with
| [] -> Filename.concat root acc
| last :: rest -> go (Filename.concat last acc) rest
in
match rev_paths with
| [] -> root
| last :: rest -> go last rest

let absolute_path cwd s =
let process s =
Expand All @@ -246,13 +110,6 @@ let absolute_cwd_path s = absolute_path cwd s
| File x -> File (absolute_path cwd x )
| Dir x -> Dir (absolute_path cwd x) *)

let concat dirname filename =
if filename = Filename.current_dir_name then dirname
else if dirname = Filename.current_dir_name then filename
else Filename.concat dirname filename

let check_suffix_case = Ext_string.ends_with

(* Input must be absolute directory *)
let rec find_root_filename ~cwd filenames =
let file_exists =
Expand Down
Loading
Loading