Skip to content

Commit 166ee94

Browse files
committedAug 13, 2017
clean logic in js_program_loader
1 parent 2f566b9 commit 166ee94

7 files changed

+58
-59
lines changed
 

‎jscomp/all.depend

+5-5
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ core/lam_pass_alpha_conversion.cmi : core/lam_stats.cmi core/lam.cmi
334334
core/lam_pass_collect.cmi : core/lam_stats.cmi core/lam.cmi \
335335
ext/ident_set.cmi
336336
core/js_program_loader.cmi : core/lam_module_ident.cmi \
337-
core/js_packages_info.cmi core/j.cmx
337+
core/js_packages_info.cmi
338338
core/js_dump.cmi : core/js_packages_info.cmi core/j.cmx ext/ext_pp.cmi
339339
core/js_pass_debug.cmi : core/j.cmx
340340
core/js_of_lam_option.cmi : core/j.cmx
@@ -513,9 +513,9 @@ core/lam_pass_collect.cmx : core/lam_util.cmx core/lam_stats_util.cmx \
513513
ext/ident_hashtbl.cmx core/lam_pass_collect.cmi
514514
core/js_program_loader.cmx : core/lam_module_ident.cmx \
515515
core/lam_compile_env.cmx core/js_stmt_make.cmx core/js_packages_state.cmx \
516-
core/js_packages_info.cmx core/js_exp_make.cmx core/j.cmx \
517-
ext/ident_set.cmx ext/ext_sys.cmx ext/ext_string.cmx ext/ext_filename.cmx \
518-
core/config_util.cmx depends/bs_exception.cmx core/js_program_loader.cmi
516+
core/js_packages_info.cmx core/js_exp_make.cmx ext/ext_sys.cmx \
517+
ext/ext_string.cmx ext/ext_filename.cmx core/config_util.cmx \
518+
depends/bs_exception.cmx core/js_program_loader.cmi
519519
core/js_dump.cmx : ext/literals.cmx core/lam_module_ident.cmx \
520520
core/js_stmt_make.cmx core/js_runtime_modules.cmx \
521521
core/js_program_loader.cmx core/js_packages_state.cmx \
@@ -612,7 +612,7 @@ core/lam_compile_group.cmx : ext/literals.cmx core/lam_util.cmx \
612612
core/lam_module_ident.cmx core/lam_group.cmx core/lam_compile_env.cmx \
613613
core/lam_compile_defs.cmx core/lam_compile.cmx core/lam_coercion.cmx \
614614
core/lam_analysis.cmx core/lam.cmx core/js_stmt_make.cmx \
615-
core/js_shake.cmx core/js_runtime_modules.cmx core/js_program_loader.cmx \
615+
core/js_shake.cmx core/js_runtime_modules.cmx \
616616
core/js_pass_tailcall_inline.cmx core/js_pass_scope.cmx \
617617
core/js_pass_flatten_and_mark_dead.cmx core/js_pass_flatten.cmx \
618618
core/js_pass_debug.cmx core/js_packages_state.cmx core/js_output.cmx \

‎jscomp/core/js_dump.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@ let goog_program ~output_prefix f goog_package (x : J.deps_program) =
17931793
(fun x ->
17941794
Lam_module_ident.id x,
17951795
Js_program_loader.string_of_module_id
1796-
~output_prefix Goog x)
1796+
~hint_output_dir:(Filename.dirname output_prefix) Goog x)
17971797
x.modules)
17981798
in
17991799
program f cxt x.program
@@ -1808,7 +1808,7 @@ let node_program ~output_prefix f ( x : J.deps_program) =
18081808
(fun x ->
18091809
Lam_module_ident.id x,
18101810
Js_program_loader.string_of_module_id
1811-
~output_prefix
1811+
~hint_output_dir:(Filename.dirname output_prefix)
18121812
NodeJS x)
18131813
x.modules)
18141814
in
@@ -1824,7 +1824,9 @@ let amd_program ~output_prefix kind f ( x : J.deps_program) =
18241824
P.string f (Printf.sprintf "%S" L.exports);
18251825

18261826
List.iter (fun x ->
1827-
let s = Js_program_loader.string_of_module_id ~output_prefix kind x in
1827+
let s =
1828+
Js_program_loader.string_of_module_id
1829+
~hint_output_dir:(Filename.dirname output_prefix) kind x in
18281830
P.string f L.comma ;
18291831
P.space f;
18301832
pp_string f s;
@@ -1862,7 +1864,7 @@ let es6_program ~output_prefix fmt f ( x : J.deps_program) =
18621864
(fun x ->
18631865
Lam_module_ident.id x,
18641866
Js_program_loader.string_of_module_id
1865-
~output_prefix
1867+
~hint_output_dir:(Filename.dirname output_prefix)
18661868
fmt x)
18671869
x.modules)
18681870
in

‎jscomp/core/js_packages_info.ml

+8-4
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,19 @@ let query_package_infos
126126
(* for a single pass compilation, [output_dir]
127127
can be cached
128128
*)
129-
let get_output_dir ~pkg_dir module_system output_prefix
129+
let get_output_dir ~pkg_dir module_system
130+
~hint_output_dir
131+
(* output_prefix *)
130132
packages_info =
131133
match packages_info with
132134
| Empty | NonBrowser (_, [])->
133-
if Filename.is_relative output_prefix then
135+
if Filename.is_relative hint_output_dir then
134136
Filename.concat (Lazy.force Ext_filename.cwd )
135-
(Filename.dirname output_prefix)
137+
hint_output_dir
138+
(* (Filename.dirname output_prefix) *)
136139
else
137-
Filename.dirname output_prefix
140+
hint_output_dir
141+
(* Filename.dirname output_prefix *)
138142
| NonBrowser (_, modules) ->
139143
begin match List.find (fun (k,_) ->
140144
compatible k module_system) modules with

‎jscomp/core/js_packages_info.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ val query_package_infos :
6868
val get_output_dir:
6969
pkg_dir:string ->
7070
module_system ->
71-
string ->
71+
hint_output_dir:string ->
7272
t ->
7373
string
7474

‎jscomp/core/js_program_loader.ml

+22-30
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,31 @@ module S = Js_stmt_make
6868

6969
let (//) = Filename.concat
7070

71-
let string_of_module_id ~output_prefix
72-
(module_system : Js_packages_info.module_system)
73-
(x : Lam_module_ident.t) : string =
74-
#if BS_COMPILER_IN_BROWSER then
71+
let string_of_module_id_in_browser (x : Lam_module_ident.t) =
7572
match x.kind with
73+
| External name -> name
7674
| Runtime | Ml ->
7775
"stdlib" // String.uncapitalize x.id.name
78-
| External name -> name
79-
#else
76+
77+
8078

79+
let string_of_module_id
80+
~hint_output_dir
81+
(module_system : Js_packages_info.module_system)
82+
(x : Lam_module_ident.t) : string =
83+
#if BS_COMPILER_IN_BROWSER then
84+
string_of_module_id_in_browser x
85+
#else
8186
let result =
8287
match x.kind with
88+
| External name -> name (* the literal string for external package *)
89+
(** This may not be enough,
90+
1. For cross packages, we may need settle
91+
down a single js package
92+
2. We may need es6 path for dead code elimination
93+
But frankly, very few JS packages have no dependency,
94+
so having plugin may sound not that bad
95+
*)
8396
| Runtime
8497
| Ml ->
8598
let id = x.id in
@@ -89,7 +102,7 @@ let string_of_module_id ~output_prefix
89102
let current_unit_dir =
90103
`Dir (Js_packages_info.get_output_dir
91104
~pkg_dir:package_dir module_system
92-
output_prefix
105+
~hint_output_dir
93106
current_package_info
94107
) in
95108
Ext_filename.node_relative_path different_package current_unit_dir dep
@@ -142,7 +155,7 @@ let string_of_module_id ~output_prefix
142155
(Js_packages_info.get_output_dir
143156
~pkg_dir:(Lazy.force Ext_filename.package_dir)
144157
module_system
145-
output_prefix
158+
~hint_output_dir
146159
(Js_packages_state.get_packages_info())
147160
)
148161
((Filename.dirname
@@ -177,14 +190,7 @@ let string_of_module_id ~output_prefix
177190
end
178191

179192
end
180-
| External name -> name (* the literal string for external package *)
181-
(** This may not be enough,
182-
1. For cross packages, we may need settle
183-
down a single js package
184-
2. We may need es6 path for dead code elimination
185-
But frankly, very few JS packages have no dependency,
186-
so having plugin may sound not that bad
187-
*)
193+
188194
in
189195
if Ext_sys.is_windows_or_cygwin then Ext_string.replace_backward_slash result
190196
else result
@@ -203,17 +209,3 @@ let string_of_module_id ~output_prefix
203209
FIXME: the module order matters?
204210
*)
205211

206-
let make_program name export_idents block : J.program =
207-
208-
{
209-
name;
210-
211-
exports = export_idents ;
212-
export_set = Ident_set.of_list export_idents;
213-
block = block;
214-
215-
}
216-
let decorate_deps modules side_effect program : J.deps_program =
217-
218-
{ program ; modules ; side_effect }
219-

‎jscomp/core/js_program_loader.mli

+8-9
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@
3737
this makes it easier to read code
3838
*)
3939

40-
val make_program :
41-
string ->
42-
Ident.t list -> J.block -> J.program
43-
44-
val decorate_deps :
45-
J.required_modules ->
46-
string option ->
47-
J.program -> J.deps_program
4840

41+
(**
42+
generate the mdoule path so that it can be spliced here:
43+
{[
44+
var Xx = require("package/path/to/xx.js")
45+
]}
46+
Note that it has to be consistent to how it is generated
47+
*)
4948
val string_of_module_id :
50-
output_prefix:string ->
49+
hint_output_dir:string ->
5150
Js_packages_info.module_system ->
5251
Lam_module_ident.t -> string

‎jscomp/core/lam_compile_group.ml

+8-6
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,11 @@ let compile ~filename output_prefix env _sigs
315315
#end
316316
(* The file is not big at all compared with [cmo] *)
317317
(* Ext_marshal.to_file (Ext_filename.chop_extension filename ^ ".mj") js; *)
318-
let js =
319-
Js_program_loader.make_program filename meta.exports
320-
body
318+
let js : J.program =
319+
{ J.name = filename ;
320+
exports = meta.exports ;
321+
export_set = Ident_set.of_list meta.exports;
322+
block = body}
321323
in
322324
js
323325
|> _j "initial"
@@ -332,11 +334,11 @@ let compile ~filename output_prefix env _sigs
332334
|> (fun js -> ignore @@ Js_pass_scope.program js ; js )
333335
|> Js_shake.shake_program
334336
|> _j "shake"
335-
|> ( fun (js: J.program) ->
337+
|> ( fun (program: J.program) ->
336338
let external_module_ids =
337339
Lam_compile_env.get_required_modules
338340
may_required_modules
339-
(Js_fold_basic.calculate_hard_dependencies js.block)
341+
(Js_fold_basic.calculate_hard_dependencies program.block)
340342
|>
341343
(fun x ->
342344
if !Js_config.sort_imports then
@@ -359,7 +361,7 @@ let compile ~filename output_prefix env _sigs
359361
(if not @@ !Clflags.dont_write_files then
360362
Js_cmj_format.to_file
361363
(output_prefix ^ Literals.suffix_cmj) v);
362-
Js_program_loader.decorate_deps external_module_ids v.effect js
364+
{J.program = program ; side_effect = v.effect ; modules = external_module_ids }
363365
)
364366
;;
365367

0 commit comments

Comments
 (0)