Skip to content

Commit 2353066

Browse files
Refactor
1 parent 467fcf5 commit 2353066

2 files changed

Lines changed: 87 additions & 126 deletions

File tree

compiler/core/js_pass_nested_component_exports.ml

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,6 @@ module StringSet = Set.Make (String)
2828

2929
type candidate = {module_ident: Ident.t}
3030

31-
let dynamic_import_module_root (expr : J.expression) =
32-
match expr.expression_desc with
33-
| Await
34-
{
35-
expression_desc =
36-
Call ({expression_desc = Var (Id import_ident); _}, [arg], _);
37-
_;
38-
}
39-
when String.equal import_ident.name "import" -> (
40-
match arg.expression_desc with
41-
| Str {txt; _} ->
42-
let basename = Filename.basename txt in
43-
let suffixes = [".res.mjs"; ".res.js"; ".mjs"; ".js"] in
44-
let rec strip_suffix = function
45-
| [] -> basename
46-
| suffix :: rest ->
47-
if Filename.check_suffix basename suffix then
48-
Filename.chop_suffix basename suffix
49-
else strip_suffix rest
50-
in
51-
Some (strip_suffix suffixes)
52-
| _ -> None)
53-
| _ -> None
54-
5531
let hidden_component_suffix (module_ident : Ident.t) =
5632
"$" ^ Ident.name module_ident
5733

@@ -117,23 +93,48 @@ let hidden_export_names_to_remove candidates =
11793
Ext_list.fold_left candidates StringSet.empty (fun acc candidate ->
11894
StringSet.add (Ident.name candidate.module_ident) acc)
11995

120-
let candidate_by_module_ident candidates module_ident =
121-
List.find_map
122-
(fun candidate ->
123-
if Ident.same candidate.module_ident module_ident then Some candidate
124-
else None)
125-
candidates
96+
let dynamic_import_module_root (expr : J.expression) =
97+
match expr.expression_desc with
98+
| Await
99+
{
100+
expression_desc =
101+
Call ({expression_desc = Var (Id import_ident); _}, [arg], _);
102+
_;
103+
}
104+
when String.equal import_ident.name "import" -> (
105+
match arg.expression_desc with
106+
| Str {txt; _} ->
107+
let basename = Filename.basename txt in
108+
let suffixes = [".res.mjs"; ".res.js"; ".mjs"; ".js"] in
109+
let rec strip_suffix = function
110+
| [] -> basename
111+
| suffix :: rest -> (
112+
match Ext_string.ends_with_then_chop basename suffix with
113+
| Some basename -> basename
114+
| None -> strip_suffix rest)
115+
in
116+
Some (strip_suffix suffixes)
117+
| _ -> None)
118+
| _ -> None
126119

127-
let rewrite_block block candidates =
128-
List.concat_map
129-
(fun (st : J.statement) ->
130-
match st.statement_desc with
131-
| Variable {ident; _} -> (
132-
match candidate_by_module_ident candidates ident with
133-
| Some _ -> [st]
134-
| None -> [st])
135-
| _ -> [st])
136-
block
120+
let known_hidden_component_exports block =
121+
let hidden_exports = ref StringSet.empty in
122+
let mapper =
123+
{
124+
Js_record_map.super with
125+
expression =
126+
(fun self expr ->
127+
(match expr.expression_desc with
128+
| Var (Qualified (_, Some name)) ->
129+
hidden_exports := StringSet.add name !hidden_exports
130+
| Static_index (_, name, _) when String.contains name '$' ->
131+
hidden_exports := StringSet.add name !hidden_exports
132+
| _ -> ());
133+
Js_record_map.super.expression self expr);
134+
}
135+
in
136+
ignore (mapper.block mapper block);
137+
!hidden_exports
137138

138139
let dynamic_import_aliases block =
139140
List.fold_left
@@ -146,7 +147,8 @@ let dynamic_import_aliases block =
146147
| _ -> aliases)
147148
Map_ident.empty block
148149

149-
let rewrite_dynamic_import_component_access aliases (expr : J.expression) =
150+
let rewrite_dynamic_import_component_access aliases known_hidden_exports
151+
(expr : J.expression) =
150152
let rec collect_segments segments (expr : J.expression) =
151153
match expr.expression_desc with
152154
| Static_index (inner, field, _) ->
@@ -168,21 +170,25 @@ let rewrite_dynamic_import_component_access aliases (expr : J.expression) =
168170
String.concat "$" segments
169171
| _ -> String.concat "$" (module_root :: segments)
170172
in
171-
{expr with expression_desc = Static_index (E.var id, hidden_name, None)}
173+
if StringSet.mem hidden_name known_hidden_exports then
174+
{expr with expression_desc = Static_index (E.var id, hidden_name, None)}
175+
else expr
172176
| None -> expr)
173177
| _ -> expr
174178

175179
let rewrite_dynamic_import_block block =
176180
let aliases = dynamic_import_aliases block in
177181
if Map_ident.is_empty aliases then block
178182
else
183+
let known_hidden_exports = known_hidden_component_exports block in
179184
let mapper =
180185
{
181186
Js_record_map.super with
182187
expression =
183188
(fun self expr ->
184189
let expr = Js_record_map.super.expression self expr in
185-
rewrite_dynamic_import_component_access aliases expr);
190+
rewrite_dynamic_import_component_access aliases known_hidden_exports
191+
expr);
186192
}
187193
in
188194
mapper.block mapper block
@@ -195,7 +201,5 @@ let program (js : J.program) : J.program =
195201
not (StringSet.mem (Ident.name ident) removed_export_names))
196202
in
197203
let export_set = Set_ident.of_list exports in
198-
let block =
199-
rewrite_dynamic_import_block (rewrite_block js.block candidates)
200-
in
204+
let block = rewrite_dynamic_import_block js.block in
201205
{J.block; exports; export_set}

compiler/core/lam_compile.ml

Lines changed: 37 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,34 @@ let compile output_prefix =
241241
| Some index -> String.sub id.name 0 index
242242
| None -> id.name)
243243
in
244-
let rec extract_nested_external_component_segments segments
245-
((lam : Lam.t), (make_dynamic_import : bool option ref)) :
244+
let nested_component_path id dynamic_import segments =
245+
let root_name = root_module_name id in
246+
let denamespace_segment segment =
247+
let namespaced_prefix = root_name ^ "$" in
248+
if Ext_string.starts_with segment namespaced_prefix then
249+
match String.split_on_char '$' segment with
250+
| root :: _namespace :: rest when rest <> [] ->
251+
String.concat "$" (root :: rest)
252+
| _ -> segment
253+
else segment
254+
in
255+
let segments =
256+
match segments with
257+
| head :: rest
258+
when head = id.name || head = root_name
259+
|| Ext_string.starts_with head (root_name ^ "$") ->
260+
rest
261+
| _ -> segments
262+
in
263+
match segments with
264+
| [] -> None
265+
| head :: rest ->
266+
Some
267+
( id,
268+
dynamic_import,
269+
String.concat "$" (root_name :: denamespace_segment head :: rest) )
270+
in
271+
let rec extract_nested_external_component_segments segments (lam : Lam.t) :
246272
(Ident.t * bool * string list) option =
247273
match lam with
248274
| Lprim
@@ -251,21 +277,15 @@ let compile output_prefix =
251277
args = [arg];
252278
_;
253279
} ->
254-
extract_nested_external_component_segments (name :: segments)
255-
(arg, make_dynamic_import)
280+
extract_nested_external_component_segments (name :: segments) arg
256281
| Lprim {primitive = Pawait; args = [arg]; _} ->
257-
extract_nested_external_component_segments segments
258-
(arg, make_dynamic_import)
282+
extract_nested_external_component_segments segments arg
259283
| Lvar id -> (
260284
match Map_ident.find_opt !local_module_aliases id with
261285
| Some alias_lam ->
262-
extract_nested_external_component_segments segments
263-
(alias_lam, make_dynamic_import)
264-
| None ->
265-
make_dynamic_import := Some false;
266-
Some (id, false, List.rev segments))
286+
extract_nested_external_component_segments segments alias_lam
287+
| None -> Some (id, false, List.rev segments))
267288
| Lglobal_module (id, dynamic_import) ->
268-
make_dynamic_import := Some dynamic_import;
269289
Some (id, dynamic_import, List.rev segments)
270290
| _ -> None
271291
in
@@ -294,42 +314,9 @@ let compile output_prefix =
294314
in
295315
let extract_nested_external_component_path (lam : Lam.t) :
296316
(Ident.t * bool * string) option =
297-
let dynamic_import = ref None in
298-
match
299-
extract_nested_external_component_segments [] (lam, dynamic_import)
300-
with
301-
| Some (id, dynamic_import, segments) -> (
302-
let denamespace_segment segment =
303-
let root_name = root_module_name id in
304-
let namespaced_prefix = root_name ^ "$" in
305-
if Ext_string.starts_with segment namespaced_prefix then
306-
match String.split_on_char '$' segment with
307-
| root :: _namespace :: rest when rest <> [] ->
308-
String.concat "$" (root :: rest)
309-
| _ -> segment
310-
else segment
311-
in
312-
let segments =
313-
match segments with
314-
| head :: rest
315-
when head = id.name
316-
|| head = root_module_name id
317-
|| Ext_string.starts_with head (root_module_name id ^ "$") ->
318-
rest
319-
| _ -> segments
320-
in
321-
let segments =
322-
match segments with
323-
| head :: rest -> denamespace_segment head :: rest
324-
| [] -> []
325-
in
326-
match segments with
327-
| [] -> None
328-
| _ ->
329-
Some
330-
( id,
331-
dynamic_import,
332-
String.concat "$" (root_module_name id :: segments) ))
317+
match extract_nested_external_component_segments [] lam with
318+
| Some (id, dynamic_import, segments) ->
319+
nested_component_path id dynamic_import segments
333320
| None -> None
334321
in
335322
let extract_nested_external_component_field (lam : Lam.t) :
@@ -347,38 +334,8 @@ let compile output_prefix =
347334
let extract_static_nested_external_component_path (lam : Lam.t) :
348335
(Ident.t * bool * string) option =
349336
match extract_static_nested_external_component_segments [] lam with
350-
| Some (id, dynamic_import, segments) -> (
351-
let denamespace_segment segment =
352-
let root_name = root_module_name id in
353-
let namespaced_prefix = root_name ^ "$" in
354-
if Ext_string.starts_with segment namespaced_prefix then
355-
match String.split_on_char '$' segment with
356-
| root :: _namespace :: rest when rest <> [] ->
357-
String.concat "$" (root :: rest)
358-
| _ -> segment
359-
else segment
360-
in
361-
let segments =
362-
match segments with
363-
| head :: rest
364-
when head = id.name
365-
|| head = root_module_name id
366-
|| Ext_string.starts_with head (root_module_name id ^ "$") ->
367-
rest
368-
| _ -> segments
369-
in
370-
let segments =
371-
match segments with
372-
| head :: rest -> denamespace_segment head :: rest
373-
| [] -> []
374-
in
375-
match segments with
376-
| [] -> None
377-
| _ ->
378-
Some
379-
( id,
380-
dynamic_import,
381-
String.concat "$" (root_module_name id :: segments) ))
337+
| Some (id, dynamic_import, segments) ->
338+
nested_component_path id dynamic_import segments
382339
| None -> None
383340
in
384341
let normalize_hidden_component_name (id : Ident.t) (hidden_name : string) =

0 commit comments

Comments
 (0)