forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_name_of_module_id.ml
195 lines (178 loc) · 7.66 KB
/
js_name_of_module_id.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
(* Copyright (C) 2017 Authors of BuckleScript
*
* 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
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* 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. *)
(*
let (=) (x : int) (y:float) = assert false
*)
let (//) = Filename.concat
let fix_path_for_windows : string -> string =
if Ext_sys.is_windows_or_cygwin then Ext_string.replace_backward_slash
else fun s -> s
let get_runtime_module_path
(dep_module_id : Lam_module_ident.t)
(current_package_info : Js_packages_info.t)
(module_system : Js_packages_info.module_system) =
let current_info_query =
Js_packages_info.query_package_infos current_package_info
module_system in
let js_file =
Ext_namespace.js_name_of_modulename dep_module_id.id.name
Little Js in (* Js may be subject to the module system *)
match current_info_query with
| Package_not_found -> assert false
| Package_script ->
Js_packages_info.runtime_package_path module_system js_file
| Package_found pkg ->
let dep_path =
"lib" // Js_packages_info.runtime_dir_of_module_system module_system in
if Js_packages_info.is_runtime_package current_package_info then
Ext_path.node_rebase_file
~from:pkg.rel_path
~to_:dep_path
js_file
(** TODO: we assume that both [x] and [path] could only be relative path
which is guaranteed by [-bs-package-output]
*)
else
match module_system with
| NodeJS | Es6 ->
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 bs-platform, it is used to
calculate relative js path
*)
((Filename.dirname (Filename.dirname Sys.executable_name)) // 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)
~(output_dir : string )
(module_system : Js_packages_info.module_system)
: string =
let current_package_info = Js_packages_state.get_packages_info () in
fix_path_for_windows (
match dep_module_id.kind with
| External {name} -> name (* the literal string for external package *)
(** This may not be enough,
1. For cross packages, we may need settle
down a single js package
2. We may need es6 path for dead code elimination
But frankly, very few JS packages have no dependency,
so having plugin may sound not that bad
*)
| Runtime ->
get_runtime_module_path dep_module_id current_package_info module_system
| Ml ->
let current_info_query =
Js_packages_info.query_package_infos
current_package_info
module_system
in
match Lam_compile_env.get_package_path_from_cmj dep_module_id with
| (package_path, dep_package_info, case) ->
let dep_info_query =
Js_packages_info.query_package_infos dep_package_info module_system
in
match dep_info_query, current_info_query with
| Package_not_found , _ ->
Bs_exception.error (Missing_ml_dependency dep_module_id.id.name)
| Package_script , Package_found _ ->
Bs_exception.error (Dependency_script_module_dependent_not dep_module_id.id.name)
| (Package_script | Package_found _ ), Package_not_found -> assert false
| Package_found ({suffix} as pkg), Package_script
->
let js_file =
Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in
pkg.pkg_rel_path // js_file
| Package_found ({suffix } as dep_pkg),
Package_found cur_pkg ->
let js_file =
Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in
if Js_packages_info.same_package_by_name current_package_info dep_package_info then
Ext_path.node_rebase_file
~from:cur_pkg.rel_path
~to_:dep_pkg.rel_path
js_file
(** TODO: we assume that both [x] and [path] could only be relative path
which is guaranteed by [-bs-package-output]
*)
else
begin match module_system with
| NodeJS | Es6 ->
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
| Package_script, Package_script
->
let js_file =
Ext_namespace.js_name_of_modulename dep_module_id.id.name case Js in
match Config_util.find_opt js_file with
| Some file ->
let basename = Filename.basename file in
let dirname = Filename.dirname file in
Ext_path.node_rebase_file
~from:(
Ext_path.absolute_cwd_path
output_dir)
~to_:(
Ext_path.absolute_cwd_path
dirname)
basename
| None ->
Bs_exception.error (Js_not_found js_file))
(* Override it in browser *)
#if BS_BROWSER then
let string_of_module_id_in_browser (x : Lam_module_ident.t) =
match x.kind with
| External {name} -> name
| Runtime | Ml ->
"./stdlib/" ^ Ext_string.uncapitalize_ascii x.id.name ^ ".js"
let string_of_module_id
(id : Lam_module_ident.t)
~output_dir:(_:string)
(_module_system : Js_packages_info.module_system)
= string_of_module_id_in_browser id
#end