8000 dynamic import by anmonteiro · Pull Request #1164 · melange-re/melange · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dynamic import #1164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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.

3 changes: 2 additions & 1 deletion jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ and ident = Ident.t
currently we always use quote
*)

and module_id = { id : ident; kind : Js_op.kind }
and module_id = { id : ident; kind : Js_op.kind; dynamic_import : bool }

and required_modules = module_id list
and vident = Id of ident | Qualified of module_id * string option
Expand Down Expand Up @@ -180,6 +180,7 @@ and expression_desc =
| Object of property_map
| Undefined
| Null
| Module of module_id

and for_ident_expression = expression
(* pure*)
Expand Down
10 changes: 7 additions & 3 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ let free_variables (stats : idents_stats) =
| Fun (_, _, _, env, _)
(* a optimization to avoid walking into function again
if it's already comuted
*)
->
*) ->
stats.used_idents <-
Ident.Set.union (Js_fun_env.get_unbounded env) stats.used_idents
| _ -> super.expression self exp);
Expand All @@ -84,7 +83,7 @@ let free_variables_of_expression st =

let rec no_side_effect_expression_desc (x : J.expression_desc) =
match x with
| Undefined | Null | Bool _ | Var _ | Unicode _ -> true
| Undefined | Null | Bool _ | Var _ | Unicode _ | Module _ -> true
| Fun _ -> true
| Number _ -> true (* Can be refined later *)
| Static_index (obj, (_name : string), (_pos : int32 option)) ->
Expand Down Expand Up @@ -188,6 +187,11 @@ let rec eq_expression ({ expression_desc = x0; _ } : J.expression)
| Str (a0, b0) -> (
match y0 with Str (a1, b1) -> a0 = a1 && b0 = b1 | _ -> false)
| Unicode s0 -> ( match y0 with Unicode s1 -> s0 = s1 | _ -> false)
| Module { id = id0; dynamic_import = d0; _ } -> (
match y0 with
| Module { id = id1; dynamic_import = d1; _ } ->
Ident.same id0 id1 && d0 = d1
| _ -> false)
| Static_index (e0, p0, off0) -> (
match y0 with
| Static_index (e1, p1, off1) ->
Expand Down
76 changes: 48 additions & 28 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,20 @@ module L = Js_dump_lit
(our call Js_fun_env.get_unbounded env) is not precise
*)

type cxt = { scope : Js_pp.Scope.t; pp : Js_pp.t }
type cxt = {
scope : Js_pp.Scope.t;
pp : Js_pp.t;
output_dir : string;
package_info : Js_packages_info.t;
output_info : Js_packages_info.output_info;
}

let from_pp ~output_dir ~package_info ~output_info pp =
{ scope = Js_pp.Scope.empty; pp; output_dir; package_info; output_info }

let from_buffer ~output_dir ~package_info ~output_info buf =
from_pp ~output_dir ~package_info ~output_info (Js_pp.from_buffer buf)

let from_pp pp = { scope = Js_pp.Scope.empty; pp }
let from_buffer buf = from_pp (Js_pp.from_buffer buf)
let update_scope cxt scope = { cxt with scope }
let ident cxt id = update_scope cxt (Js_pp.Scope.ident cxt.scope cxt.pp id)
let string cxt s = Js_pp.string cxt.pp s
Expand Down Expand Up @@ -203,8 +213,8 @@ let exp_need_paren (e : J.expression) =
| Length _ | Call _ | Caml_block_tag _ | Seq _ | Static_index _ | Cond _
| Bin _ | Is_null_or_undefined _ | String_index _ | Array_index _
| String_append _ | Char_of_int _ | Char_to_int _ | Var _ | Undefined | Null
| Str _ | Unicode _ | Array _ | Optional_block _ | Caml_block _ | FlatCall _
| Typeof _ | Number _ | Js_not _ | Bool _ | New _ ->
| Str _ | Unicode _ | Module _ | Array _ | Optional_block _ | Caml_block _
| FlatCall _ | Typeof _ | Number _ | Js_not _ | Bool _ | New _ ->
false

(* Print as underscore for unused vars, may not be
Expand Down Expand Up @@ -491,7 +501,7 @@ and pp_one_case_clause : 'a. _ -> (_ -> 'a -> unit) -> 'a * J.case_clause -> _ =
| [] -> cxt
| _ ->
newline cxt;
statements false cxt switch_body
statements ~top:false cxt switch_body
in
if should_break then (
newline cxt;
Expand All @@ -512,16 +522,16 @@ and vident cxt (v : J.vident) =
match v with
| Id v
| Qualified ({ id = v; _ }, None)
| Qualified ({ id = v; kind = External { default = true; _ } }, _) ->
| Qualified ({ id = v; kind = External { default = true; _ }; _ }, _) ->
ident cxt v
| Qualified ({ id; kind = Ml | Runtime }, Some name) ->
| Qualified ({ id; kind = Ml | Runtime; _ }, Some name) ->
let cxt = ident cxt id in
string cxt L.dot;
string cxt
(if name = Js_dump_import_export.default_export then name
else Ident.convert name);
cxt
| Qualified ({ id; kind = External _ }, Some name) ->
| Qualified ({ id; kind = External _; _ }, Some name) ->
let cxt = ident cxt id in
Js_dump_property.property_access cxt.pp name;
cxt
Expand Down Expand Up @@ -630,6 +640,13 @@ and expression_desc cxt ~(level : int) x : cxt =
*)
Js_dump_string.pp_string cxt.pp s;
cxt
| Module module_id ->
let path =
Js_name_of_module_id.string_of_module_id ~package_info:cxt.package_info
~output_info:cxt.output_info ~output_dir:cxt.output_dir module_id
in
Js_dump_string.pp_string cxt.pp path;
cxt
| Raw_js_code { code = s; code_info = info } -> (
match info with
| Exp exp_info ->
Expand Down Expand Up @@ -953,7 +970,8 @@ and pp_comment_option cxt comment =
match comment with None -> () | Some x -> pp_comment cxt x

(* and pp_loc_option f loc = *)
and statement top cxt ({ statement_desc = s; comment; _ } : J.statement) : cxt =
and statement ~top cxt ({ statement_desc = s; comment; _ } : J.statement) : cxt
=
pp_comment_option cxt comment;
statement_desc top cxt s

Expand Down Expand Up @@ -989,7 +1007,7 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
| Block b ->
(* No braces needed here *)
ipp_comment cxt L.start_block;
let cxt = statements top cxt b in
let cxt = statements ~top cxt b in
ipp_comment cxt L.end_block;
cxt
| Variable l -> variable_declaration top cxt l
Expand Down Expand Up @@ -1017,7 +1035,7 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
space cxt;
string cxt L.else_;
space cxt;
statement false cxt nest
statement ~top:false cxt nest
| _ :: _ as s2 ->
space cxt;
string cxt L.else_;
Expand Down Expand Up @@ -1161,7 +1179,7 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
string cxt L.default;
string cxt L.colon;
newline cxt;
statements false cxt def))
statements ~top:false cxt def))
| String_switch (e, cc, def) ->
string cxt L.switch;
space cxt;
Expand All @@ -1180,7 +1198,7 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
string cxt L.default;
string cxt L.colon;
newline cxt;
statements false cxt def))
statements ~top:false cxt def))
| Throw e ->
let e =
match e.expression_desc with
Expand Down Expand Up @@ -1235,42 +1253,44 @@ and function_body (cxt : cxt) ~return_unit (b : J.block) : unit =
{ statement_desc = Return { expression_desc = Undefined; _ }; _ };
] ) ->
ignore
(statement false cxt
(statement ~top:false cxt
{ s with statement_desc = If (bool, then_, []) }
: cxt)
| Return { expression_desc = Undefined; _ } -> ()
| Return exp when return_unit ->
ignore (statement false cxt (S.exp exp) : cxt)
| _ -> ignore (statement false cxt s : cxt))
ignore (statement ~top:false cxt (S.exp exp) : cxt)
| _ -> ignore (statement ~top:false cxt s : cxt))
| [ s; { statement_desc = Return { expression_desc = Undefined; _ }; _ } ] ->
ignore (statement false cxt s : cxt)
ignore (statement ~top:false cxt s : cxt)
| s :: r ->
let cxt = statement false cxt s in
let cxt = statement ~top:false cxt s in
newline cxt;
function_body cxt r ~return_unit

and brace_block cxt b =
(* This one is for '{' *)
brace_vgroup cxt 1 (fun _ -> statements false cxt b)
brace_vgroup cxt 1 (fun _ -> statements ~top:false cxt b)

(* main entry point *)
and statements top cxt b =
and statements ~top cxt b =
iter_lst cxt b
(fun cxt s -> statement top cxt s)
(fun cxt s -> statement ~top cxt s)
(if top then at_least_two_lines else newline)

let string_of_block (block : J.block) =
let string_of_block ~output_dir ~package_info ~output_info (block : J.block) =
let buffer = Buffer.create 50 in
let cxt = from_buffer buffer in
let (_ : cxt) = statements true cxt block in
let cxt = from_buffer ~output_dir ~package_info ~output_info buffer in
let (_ : cxt) = statements ~top:true cxt block in
flush cxt ();
Buffer.contents buffer

let string_of_expression (e : J.expression) =
let string_of_expression ~output_dir ~package_info ~output_info
(e : J.expression) =
let buffer = Buffer.create 50 in
let cxt = from_buffer buffer in
let cxt = from_buffer ~output_dir ~package_info ~output_info buffer in
let (_ : cxt) = expression ~level:0 cxt e in
flush cxt ();
Buffer.contents buffer

let statements top scope pp b = (statements top { scope; pp } b).scope
let statements ~top ~scope ~output_dir ~package_info ~output_info pp b =
(statements ~top { scope; pp; output_dir; package_info; output_info } b).scope
24 changes: 21 additions & 3 deletions jscomp/core/js_dump.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,29 @@

open Import

val statements : bool -> Js_pp.Scope.t -> Js_pp.t -> J.block -> Js_pp.Scope.t
val statements :
top:bool ->
scope:Js_pp.Scope.t ->
output_dir:string ->
package_info:Js_packages_info.t ->
output_info:Js_packages_info.output_info ->
Js_pp.t ->
J.block ->
Js_pp.Scope.t
(** Print JS IR to vanilla Javascript code
Called by module {!Js_dump_program} *)

val string_of_block : J.block -> string
val string_of_block :
output_dir:string ->
package_info:Js_packages_info.t ->
output_info:Js_packages_info.output_info ->
J.block ->
string
(** 2 functions Only used for debugging *)

val string_of_expression : J.expression -> string
val string_of_expression :
output_dir:string ->
package_info:Js_packages_info.t ->
output_info:Js_packages_info.output_info ->
J.expression ->
string
46 changes: 30 additions & 16 deletions jscomp/core/js_dump_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,55 @@ let extract_file_comments (x : J.deps_program) =
let comments, new_block = extract_block_comments [] x.program.block in
(comments, { x with program = { x.program with block = new_block } })

let program f cxt (x : J.program) =
let program ~output_dir ~package_info ~output_info f cxt (x : J.program) =
P.at_least_two_lines f;
let cxt = Js_dump.statements true cxt f x.block in
let cxt =
Js_dump.statements ~top:true ~scope:cxt ~output_dir ~package_info
~output_info f x.block
in
Js_dump_import_export.exports cxt f x.exports

let dump_program (x : J.program) oc =
ignore (program (P.from_channel oc) Js_pp.Scope.empty x)
let dump_program ~output_dir ~package_info ~output_info (x : J.program) oc =
ignore
(program ~output_dir ~package_info ~output_info (P.from_channel oc)
Js_pp.Scope.empty x)

let modules ~output_dir ~package_info ~output_info (x : J.deps_program) =
List.map x.modules ~f:(fun (x : Lam_module_ident.t) ->
{
Js_dump_import_export.id = x.id;
path =
Js_name_of_module_id.string_of_module_id ~package_info ~output_info
~output_dir x;
default =
(match x.kind with External { default; _ } -> default | _ -> false);
})
List.filter_map x.modules ~f:(fun (x : Lam_module_ident.t) ->
match x.dynamic_import with
| true -> None
| false ->
Some
{
Js_dump_import_export.id = x.id;
path =
Js_name_of_module_id.string_of_module_id ~package_info
~output_info ~output_dir x;
default =
(match x.kind with
| External { default; _ } -> default
| _ -> false);
})

let node_program ~package_info ~output_info ~output_dir f (x : J.deps_program) =
let node_program ~output_dir ~package_info ~output_info f (x : J.deps_program) =
P.string f L.strict_directive;
P.newline f;
let cxt =
Js_dump_import_export.requires Js_pp.Scope.empty f
(modules ~package_info ~output_info ~output_dir x)
in
program f cxt x.program
program ~output_dir ~package_info ~output_info f cxt x.program

let es6_program ~package_info ~output_info ~output_dir f (x : J.deps_program) =
let cxt =
Js_dump_import_export.imports Js_pp.Scope.empty f
(modules ~package_info ~output_info ~output_dir x)
in
let () = P.at_least_two_lines f in
let cxt = Js_dump.statements true cxt f x.program.block in
let cxt =
Js_dump.statements ~top:true ~output_dir ~output_info ~package_info
~scope:cxt f x.program.block
in
Js_dump_import_export.es6_export cxt f x.program.exports

let pp_deps_program =
A8AA Expand Down
8 changes: 7 additions & 1 deletion jscomp/core/js_dump_program.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@

open Import

val dump_program : J.program -> out_channel -> unit
val dump_program :
output_dir:string ->
package_info:Js_packages_info.t ->
output_info:Js_packages_info.output_info ->
J.program ->
out_channel ->
unit
(** only used for debugging purpose *)

val dump_deps_program :
Expand Down
Loading
0