|
1 | 1 | (* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript |
2 | | - * |
| 2 | + * |
3 | 3 | * This program is free software: you can redistribute it and/or modify |
4 | 4 | * it under the terms of the GNU Lesser General Public License as published by |
5 | 5 | * the Free Software Foundation, either version 3 of the License, or |
|
17 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | 19 | * GNU Lesser General Public License for more details. |
20 | | - * |
| 20 | + * |
21 | 21 | * You should have received a copy of the GNU Lesser General Public License |
22 | 22 | * along with this program; if not, write to the Free Software |
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
24 | 24 | (* |
25 | | -let (=) (x : int) (y:float) = assert false |
| 25 | +let (=) (x : int) (y:float) = assert false |
26 | 26 | *) |
27 | 27 |
|
28 | 28 | #ifdef BROWSER |
29 | 29 |
|
30 | | -let string_of_module_id_in_browser (x : Lam_module_ident.t) = |
| 30 | +let string_of_module_id_in_browser (x : Lam_module_ident.t) = |
31 | 31 | match x.kind with |
32 | 32 | | External {name} -> name |
33 | | - | Runtime | Ml -> |
| 33 | + | Runtime | Ml -> |
34 | 34 | "./stdlib/" ^ x.id.name ^ ".js" |
35 | 35 |
|
36 | | -let string_of_module_id |
| 36 | +let string_of_module_id |
37 | 37 | (id : Lam_module_ident.t) |
38 | 38 | ~output_dir:(_:string) |
39 | 39 | (_module_system : Js_packages_info.module_system) |
40 | 40 | = string_of_module_id_in_browser id |
41 | 41 |
|
42 | 42 | #else |
43 | | - |
44 | | -let (//) = Filename.concat |
| 43 | + |
| 44 | +let (//) = Filename.concat |
45 | 45 |
|
46 | 46 |
|
47 | | -let fix_path_for_windows : string -> string = |
| 47 | +let fix_path_for_windows : string -> string = |
48 | 48 | if Ext_sys.is_windows_or_cygwin then Ext_string.replace_backward_slash |
49 | | - else fun s -> s |
| 49 | + else fun s -> s |
50 | 50 |
|
51 | 51 |
|
52 | | -(* dependency is runtime module *) |
53 | | -let get_runtime_module_path |
54 | | - (dep_module_id : Lam_module_ident.t) |
| 52 | +(* dependency is runtime module *) |
| 53 | +let get_runtime_module_path |
| 54 | + (dep_module_id : Lam_module_ident.t) |
55 | 55 | (current_package_info : Js_packages_info.t) |
56 | | - (module_system : Js_packages_info.module_system) = |
57 | | - let current_info_query = |
| 56 | + (module_system : Js_packages_info.module_system) = |
| 57 | + let current_info_query = |
58 | 58 | Js_packages_info.query_package_infos current_package_info |
59 | 59 | module_system in |
60 | | - let js_file = |
61 | | - Ext_namespace.js_name_of_modulename dep_module_id.id.name |
62 | | - Upper Literals.suffix_js in |
63 | | - match current_info_query with |
| 60 | + let js_file = |
| 61 | + Ext_namespace.js_name_of_modulename dep_module_id.id.name |
| 62 | + Upper Literals.suffix_js in |
| 63 | + match current_info_query with |
64 | 64 | | Package_not_found -> assert false |
65 | | - | Package_script -> |
66 | | - Js_packages_info.runtime_package_path module_system js_file |
67 | | - | Package_found pkg -> |
68 | | - let dep_path = |
69 | | - "lib" // Js_packages_info.runtime_dir_of_module_system module_system in |
70 | | - if Js_packages_info.is_runtime_package current_package_info then |
| 65 | + | Package_script -> |
| 66 | + Js_packages_info.runtime_package_path module_system js_file |
| 67 | + | Package_found pkg -> |
| 68 | + let dep_path = |
| 69 | + "lib" // Js_packages_info.runtime_dir_of_module_system module_system in |
| 70 | + if Js_packages_info.is_runtime_package current_package_info then |
71 | 71 | Ext_path.node_rebase_file |
72 | 72 | ~from:pkg.rel_path |
73 | | - ~to_:dep_path |
| 73 | + ~to_:dep_path |
74 | 74 | js_file |
75 | 75 | (* TODO: we assume that both [x] and [path] could only be relative path |
76 | 76 | which is guaranteed by [-bs-package-output] |
77 | 77 | *) |
78 | | - else |
79 | | - match module_system with |
80 | | - | Commonjs | Esmodule -> |
81 | | - Js_packages_info.runtime_package_path module_system js_file |
| 78 | + else |
| 79 | + match module_system with |
| 80 | + | Commonjs | Esmodule -> |
| 81 | + Js_packages_info.runtime_package_path module_system js_file |
82 | 82 | (* Note we did a post-processing when working on Windows *) |
83 | | - | Es6_global |
84 | | - -> |
85 | | - (* lib/ocaml/xx.cmj -- |
| 83 | + | Es6_global |
| 84 | + -> |
| 85 | + (* lib/ocaml/xx.cmj -- |
86 | 86 | HACKING: FIXME |
87 | 87 | maybe we can caching relative package path calculation or employ package map *) |
88 | 88 | (* assert false *) |
89 | | - Ext_path.rel_normalized_absolute_path |
| 89 | + Ext_path.rel_normalized_absolute_path |
90 | 90 | ~from:( |
91 | | - Js_packages_info.get_output_dir |
| 91 | + Js_packages_info.get_output_dir |
92 | 92 | current_package_info |
93 | 93 | ~package_dir:(Lazy.force Ext_path.package_dir) |
94 | 94 | module_system ) |
95 | | - (*Invariant: the package path to rescript, it is used to |
| 95 | + (*Invariant: the package path to rescript, it is used to |
96 | 96 | calculate relative js path |
97 | 97 | *) |
98 | | - (Runtime_package.path // dep_path // js_file) |
| 98 | + (!Runtime_package.path // dep_path // js_file) |
99 | 99 |
|
100 | 100 | (* [output_dir] is decided by the command line argument *) |
101 | | -let string_of_module_id |
102 | | - (dep_module_id : Lam_module_ident.t) |
| 101 | +let string_of_module_id |
| 102 | + (dep_module_id : Lam_module_ident.t) |
103 | 103 | ~(output_dir : string ) |
104 | | - (module_system : Js_packages_info.module_system) |
| 104 | + (module_system : Js_packages_info.module_system) |
105 | 105 | : string = |
106 | | - let current_package_info = Js_packages_state.get_packages_info () in |
107 | | - fix_path_for_windows ( |
108 | | - match dep_module_id.kind with |
| 106 | + let current_package_info = Js_packages_state.get_packages_info () in |
| 107 | + fix_path_for_windows ( |
| 108 | + match dep_module_id.kind with |
109 | 109 | | External {name} -> name (* the literal string for external package *) |
110 | | - (* This may not be enough, |
111 | | - 1. For cross packages, we may need settle |
| 110 | + (* This may not be enough, |
| 111 | + 1. For cross packages, we may need settle |
112 | 112 | down a single js package |
113 | 113 | 2. We may need es6 path for dead code elimination |
114 | | - But frankly, very few JS packages have no dependency, |
115 | | - so having plugin may sound not that bad |
| 114 | + But frankly, very few JS packages have no dependency, |
| 115 | + so having plugin may sound not that bad |
116 | 116 | *) |
117 | | - | Runtime -> |
| 117 | + | Runtime -> |
118 | 118 | get_runtime_module_path dep_module_id current_package_info module_system |
119 | | - | Ml -> |
120 | | - let current_info_query = |
121 | | - Js_packages_info.query_package_infos |
| 119 | + | Ml -> |
| 120 | + let current_info_query = |
| 121 | + Js_packages_info.query_package_infos |
122 | 122 | current_package_info |
123 | | - module_system |
| 123 | + module_system |
124 | 124 | in |
125 | | - match Lam_compile_env.get_package_path_from_cmj dep_module_id with |
126 | | - | (package_path, dep_package_info, case) -> |
| 125 | + match Lam_compile_env.get_package_path_from_cmj dep_module_id with |
| 126 | + | (package_path, dep_package_info, case) -> |
127 | 127 |
|
128 | 128 |
|
129 | | - let dep_info_query = |
130 | | - Js_packages_info.query_package_infos dep_package_info module_system |
131 | | - in |
| 129 | + let dep_info_query = |
| 130 | + Js_packages_info.query_package_infos dep_package_info module_system |
| 131 | + in |
132 | 132 | match dep_info_query, current_info_query with |
133 | | - | Package_not_found , _ -> |
| 133 | + | Package_not_found , _ -> |
134 | 134 | Bs_exception.error (Missing_ml_dependency dep_module_id.id.name) |
135 | | - | Package_script , Package_found _ -> |
| 135 | + | Package_script , Package_found _ -> |
136 | 136 | Bs_exception.error (Dependency_script_module_dependent_not dep_module_id.id.name) |
137 | 137 | | (Package_script | Package_found _ ), Package_not_found -> assert false |
138 | 138 |
|
139 | | - | Package_found ({suffix} as pkg), Package_script |
140 | | - -> |
141 | | - let js_file = |
142 | | - Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in |
| 139 | + | Package_found ({suffix} as pkg), Package_script |
| 140 | + -> |
| 141 | + let js_file = |
| 142 | + Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in |
143 | 143 | pkg.pkg_rel_path // js_file |
144 | 144 | | Package_found ({suffix } as dep_pkg), |
145 | | - Package_found cur_pkg -> |
146 | | - let js_file = |
147 | | - Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in |
| 145 | + Package_found cur_pkg -> |
| 146 | + let js_file = |
| 147 | + Ext_namespace.js_name_of_modulename dep_module_id.id.name case suffix in |
148 | 148 |
|
149 | | - if Js_packages_info.same_package_by_name current_package_info dep_package_info then |
| 149 | + if Js_packages_info.same_package_by_name current_package_info dep_package_info then |
150 | 150 | Ext_path.node_rebase_file |
151 | 151 | ~from:cur_pkg.rel_path |
152 | | - ~to_:dep_pkg.rel_path |
| 152 | + ~to_:dep_pkg.rel_path |
153 | 153 | js_file |
154 | 154 | (* TODO: we assume that both [x] and [path] could only be relative path |
155 | 155 | which is guaranteed by [-bs-package-output] |
156 | 156 | *) |
157 | | - else |
158 | | - if Js_packages_info.is_runtime_package dep_package_info then |
| 157 | + else |
| 158 | + if Js_packages_info.is_runtime_package dep_package_info then |
159 | 159 | get_runtime_module_path dep_module_id current_package_info module_system |
160 | | - else |
161 | | - begin match module_system with |
162 | | - | Commonjs | Esmodule -> |
| 160 | + else |
| 161 | + begin match module_system with |
| 162 | + | Commonjs | Esmodule -> |
163 | 163 | dep_pkg.pkg_rel_path // js_file |
164 | 164 | (* Note we did a post-processing when working on Windows *) |
165 | | - | Es6_global |
166 | | - -> |
167 | | - begin |
168 | | - Ext_path.rel_normalized_absolute_path |
| 165 | + | Es6_global |
| 166 | + -> |
| 167 | + begin |
| 168 | + Ext_path.rel_normalized_absolute_path |
169 | 169 | ~from:( |
170 | | - Js_packages_info.get_output_dir |
| 170 | + Js_packages_info.get_output_dir |
171 | 171 | current_package_info |
172 | 172 | ~package_dir:(Lazy.force Ext_path.package_dir) |
173 | | - module_system |
| 173 | + module_system |
174 | 174 | ) |
175 | | - (package_path // dep_pkg.rel_path // js_file) |
| 175 | + (package_path // dep_pkg.rel_path // js_file) |
176 | 176 | end |
177 | 177 | end |
178 | | - | Package_script, Package_script |
179 | | - -> |
180 | | - let js_file = |
181 | | - Ext_namespace.js_name_of_modulename dep_module_id.id.name case Literals.suffix_js in |
182 | | - match Config_util.find_opt js_file with |
183 | | - | Some file -> |
184 | | - let basename = Filename.basename file in |
185 | | - let dirname = Filename.dirname file in |
| 178 | + | Package_script, Package_script |
| 179 | + -> |
| 180 | + let js_file = |
| 181 | + Ext_namespace.js_name_of_modulename dep_module_id.id.name case Literals.suffix_js in |
| 182 | + match Config_util.find_opt js_file with |
| 183 | + | Some file -> |
| 184 | + let basename = Filename.basename file in |
| 185 | + let dirname = Filename.dirname file in |
186 | 186 | Ext_path.node_rebase_file |
187 | 187 | ~from:( |
188 | | - Ext_path.absolute_cwd_path |
| 188 | + Ext_path.absolute_cwd_path |
189 | 189 | output_dir) |
190 | 190 | ~to_:( |
191 | | - Ext_path.absolute_cwd_path |
| 191 | + Ext_path.absolute_cwd_path |
192 | 192 |
|
193 | 193 | dirname) |
194 | | - basename |
195 | | - | None -> |
| 194 | + basename |
| 195 | + | None -> |
196 | 196 | Bs_exception.error (Js_not_found js_file)) |
197 | 197 |
|
198 | 198 | #endif |
0 commit comments