Skip to content

Commit aca7f9c

Browse files
committedMar 23, 2025·
Fix symbol collection with multiple exports of same function
Fixes bundling date-fns. Fixes #9676. Fixes #10098.
1 parent 3a566fd commit aca7f9c

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed
 

‎packages/core/integration-tests/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"command-exists": "^1.2.6",
2828
"core-js": "^3.2.1",
2929
"cross-env": "^7.0.0",
30+
"date-fns": "^4.1.0",
3031
"elm": "^0.19.1-5",
3132
"elm-hot": "^1.1.5",
3233
"esbuild-register": "^3.5.0",

‎packages/core/integration-tests/test/javascript.js

+19
Original file line numberDiff line numberDiff line change
@@ -6143,6 +6143,25 @@ describe('javascript', function () {
61436143
await res.output;
61446144
assert.equal(res.result, 2);
61456145
});
6146+
6147+
it(`can bundle date-fns ${
6148+
shouldScopeHoist ? 'with' : 'without'
6149+
} scope-hoisting`, async () => {
6150+
await fsFixture(overlayFS, __dirname)`
6151+
date-fns
6152+
a.ts:
6153+
import {format} from 'date-fns';
6154+
output = format(new Date(2025, 1, 3), "yyyy-MM-dd");`;
6155+
6156+
let b = await bundle(path.join(__dirname, 'date-fns/a.ts'), {
6157+
...options,
6158+
mode: 'development',
6159+
inputFS: overlayFS,
6160+
});
6161+
let res = await run(b, null, {require: false});
6162+
let result = await res.output;
6163+
assert.equal(result, '2025-02-03');
6164+
});
61466165
}
61476166

61486167
for (let defaultTargetOptions of [

‎packages/transformers/js/core/src/collect.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,7 @@ impl Visit for Collect {
457457
);
458458
self
459459
.exports_locals
460-
.entry(id!(class.ident))
461-
.or_insert_with(|| class.ident.sym.clone());
460+
.insert(id!(class.ident), class.ident.sym.clone());
462461
}
463462
Decl::Fn(func) => {
464463
self.exports.insert(
@@ -472,8 +471,7 @@ impl Visit for Collect {
472471
);
473472
self
474473
.exports_locals
475-
.entry(id!(func.ident))
476-
.or_insert_with(|| func.ident.sym.clone());
474+
.insert(id!(func.ident), func.ident.sym.clone());
477475
}
478476
Decl::Var(var) => {
479477
for decl in &var.decls {
@@ -503,10 +501,7 @@ impl Visit for Collect {
503501
is_esm: true,
504502
},
505503
);
506-
self
507-
.exports_locals
508-
.entry(id!(ident))
509-
.or_insert_with(|| js_word!("default"));
504+
self.exports_locals.insert(id!(ident), js_word!("default"));
510505
} else {
511506
self.exports.insert(
512507
js_word!("default"),
@@ -530,10 +525,7 @@ impl Visit for Collect {
530525
is_esm: true,
531526
},
532527
);
533-
self
534-
.exports_locals
535-
.entry(id!(ident))
536-
.or_insert_with(|| js_word!("default"));
528+
self.exports_locals.insert(id!(ident), js_word!("default"));
537529
} else {
538530
self.exports.insert(
539531
js_word!("default"),
@@ -597,8 +589,7 @@ impl Visit for Collect {
597589
);
598590
self
599591
.exports_locals
600-
.entry(id!(node.id))
601-
.or_insert_with(|| node.id.sym.clone());
592+
.insert(id!(node.id), node.id.sym.clone());
602593
}
603594

604595
if self.in_assign && node.id.ctxt.has_mark(self.global_mark) {
@@ -623,8 +614,7 @@ impl Visit for Collect {
623614
);
624615
self
625616
.exports_locals
626-
.entry(id!(node.key))
627-
.or_insert_with(|| node.key.sym.clone());
617+
.insert(id!(node.key), node.key.sym.clone());
628618
}
629619

630620
if self.in_assign && node.key.ctxt.has_mark(self.global_mark) {
@@ -700,7 +690,8 @@ impl Visit for Collect {
700690
.entry(id!(ident))
701691
.or_default()
702692
.push(node.span);
703-
} else if self.imports.contains_key(&id!(ident)) {
693+
}
694+
if self.imports.contains_key(&id!(ident)) {
704695
self.used_imports.insert(id!(ident));
705696
}
706697
return;

‎packages/transformers/js/core/src/hoist.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,17 @@ mod tests {
15931593
w!("y") => (w!("other"), w!("*"), false)
15941594
}
15951595
);
1596+
1597+
let (collect, _code, _hoist) = parse(
1598+
r#"
1599+
import { formatters } from "other";
1600+
1601+
export function format() {
1602+
return formatters[something]();
1603+
}
1604+
"#,
1605+
);
1606+
assert_eq_set!(collect.used_imports, set! { w!("formatters") });
15961607
}
15971608

15981609
#[test]
@@ -2870,6 +2881,23 @@ mod tests {
28702881
w!("siteSettings") => (w!("./settings"), w!("settings"))
28712882
}
28722883
);
2884+
2885+
let (_collect, _code, hoist) = parse(
2886+
r#"
2887+
export { format as formatDate };
2888+
export function format() {}
2889+
export default format;
2890+
"#,
2891+
);
2892+
2893+
assert_eq_exported_symbols!(
2894+
hoist.exported_symbols,
2895+
map! {
2896+
w!("formatDate") => (w!("$abc$export$d9468344d3651243"), true),
2897+
w!("format") => (w!("$abc$export$d9468344d3651243"), true),
2898+
w!("default") => (w!("$abc$export$2e2bcd8739ae039"), true)
2899+
}
2900+
);
28732901
}
28742902

28752903
#[test]

‎yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -5079,6 +5079,11 @@ data-view-byte-offset@^1.0.0:
50795079
es-errors "^1.3.0"
50805080
is-data-view "^1.0.1"
50815081

5082+
date-fns@^4.1.0:
5083+
version "4.1.0"
5084+
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-4.1.0.tgz#64b3d83fff5aa80438f5b1a633c2e83b8a1c2d14"
5085+
integrity sha512-Ukq0owbQXxa/U3EGtsdVBkR1w7KOQ5gIBqdH2hkvknzZPYvBxb/aa6E8L7tmjFtkwZBu3UXBbjIgPo/Ez4xaNg==
5086+
50825087
dateformat@^3.0.0:
50835088
version "3.0.3"
50845089
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"

0 commit comments

Comments
 (0)
Please sign in to comment.