aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-01-29 23:43:10 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-01-29 23:43:10 -0800
commit73449bf43373ee3a23f080e3b9ce144bc93db27c (patch)
tree2e86c7ae15d5e3819dc0adf5b19419b0b5fe4b9a
parent4aabccfc79ea076c6d868910bd4df375a9749c4c (diff)
downloadbun-73449bf43373ee3a23f080e3b9ce144bc93db27c.tar.gz
bun-73449bf43373ee3a23f080e3b9ce144bc93db27c.tar.zst
bun-73449bf43373ee3a23f080e3b9ce144bc93db27c.zip
[JS Printer] Fix CJS <> ESM interop edgecase
For code like this: ```js module.exports = require("foo") ``` If `"foo"` is bundled, we should access it as the namespace reference from the bundle. Previously, we assumed it would be a property access.
-rw-r--r--src/js_printer.zig7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/js_printer.zig b/src/js_printer.zig
index dd7ec98e5..7271d97c2 100644
--- a/src/js_printer.zig
+++ b/src/js_printer.zig
@@ -2018,6 +2018,13 @@ pub fn NewPrinter(
p.printSymbol(namespace.namespace_ref);
}
+ // In the case of code like this:
+ // module.exports = require("foo")
+ // if "foo" is bundled
+ // then we access it as the namespace symbol itself
+ // that means the namespace alias is empty
+ if (namespace.alias.len == 0) return;
+
if (p.canPrintIdentifier(namespace.alias)) {
p.print(".");
p.printIdentifier(namespace.alias);