Skip to content

Commit effd8e1

Browse files
committed
[js] Fix exports in ES modules declaring a variable
1 parent c7aa303 commit effd8e1

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

src/generators/genjs.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,28 +1041,28 @@ let path_to_brackets path =
10411041
"[\"" ^ (String.concat "\"][\"" parts) ^ "\"]"
10421042

10431043
let mangle_export_name_es6 ctx ident =
1044-
if ctx.es_version >= 2022 then
1044+
if not (ExtString.String.contains ident '.') then
1045+
ident
1046+
else if ctx.es_version >= 2022 then
10451047
(* Keyword: "arbitrary module namespace identifier names" *)
10461048
Printf.sprintf "\"%s\"" ident
10471049
else
10481050
let parts = ExtString.String.nsplit ident "." in
10491051
String.concat "_" parts
10501052

1051-
let tmp_var_counter = ref 0
1052-
let export_tmp_name() =
1053-
let name = Printf.sprintf "$hx_export_tmp_%d" !tmp_var_counter in
1054-
tmp_var_counter := !tmp_var_counter + 1;
1055-
name
1056-
1053+
let tmp_export_vars = Atomic.make 0
10571054
let generate_export_statement ctx expr ident =
10581055
if ctx.js_module_type == Es then
1059-
let ident_contains_dots = ExtString.String.contains ident '.' in
1060-
if (not ident_contains_dots) && expr <> ident then
1061-
print ctx "export const %s = %s;" ident expr
1056+
let ident = mangle_export_name_es6 ctx ident in
1057+
let expr_contains_dots = ExtString.String.contains expr '.' in
1058+
if not expr_contains_dots then
1059+
if expr = ident then
1060+
print ctx "export { %s };" ident
1061+
else
1062+
print ctx "export { %s as %s };" expr ident
10621063
else begin
1063-
let tmp_name = export_tmp_name() in
1064-
let exported_name = if ident_contains_dots then mangle_export_name_es6 ctx ident else ident in
1065-
print ctx "const %s = %s; export {%s as %s};" tmp_name expr tmp_name exported_name
1064+
let tmp_name = Printf.sprintf "$hx_export_tmp_%d" (Atomic.fetch_and_add tmp_export_vars 1) in
1065+
print ctx "const %s = %s; export { %s as %s };" tmp_name expr tmp_name ident
10661066
end
10671067
else
10681068
print ctx "$hx_exports%s = %s;" (path_to_brackets ident) expr;
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
package unit.issues;
22

33
class Issue8710 extends unit.Test {
4-
#if (js || lua)
4+
#if (js || lua)
55
@:expose('exposed')
66
static var field = 10 + Std.random(1);
77

8+
#if (js && js.module == "es")
9+
// ESM exports are not easily accessible as variables.
10+
// To circumvent this, dynamically import current module:
11+
function test(async:utest.Async) {
12+
js.Lib.dynamicImport(js.Syntax.code("import.meta.url")).then(module -> {
13+
var actual = module.exposed;
14+
eq(10, actual);
15+
async.done();
16+
}).catchError(e -> {
17+
assert(Std.string(e));
18+
async.done();
19+
});
20+
}
21+
#else
822
function test() {
923
var actual =
10-
#if (js && js.module == "es")
11-
js.Syntax.code("exposed");
12-
#elseif js
13-
js.Syntax.code("$hx_exports[\"exposed\"]");
24+
#if js
25+
js.Syntax.code("$hx_exports[\"exposed\"]");
1426
#elseif lua
15-
untyped __lua__("_hx_exports[\"exposed\"]");
27+
untyped __lua__("_hx_exports[\"exposed\"]");
1628
#end
1729
eq(10, actual);
1830
}
19-
#end
20-
}
31+
#end
32+
#end
33+
}

0 commit comments

Comments
 (0)