bun icon indicating copy to clipboard operation
bun copied to clipboard

Bundler Duplicate Exports

Open jacob-ebey opened this issue 1 year ago • 9 comments

What version of Bun is running?

1.0.1

What platform is your computer?

Darwin 22.5.0 arm64 arm

What steps can reproduce the bug?

Double exports seems to arise when one entrypoint is re-exporting from another entrypoint.

A reproduction can be found here: https://github.com/jacob-ebey/bun-bug-reproductions/tree/main/bugs/bundler-double-exports

What is the expected behavior?

Entrypoint output is a valid ES module.

What do you see instead?

Duplicate exports in the dist/entry-b.js that ends up looking something like:

function b() {
}
export {
  b
};

export { b };

Additional information

I'm going to be using https://github.com/jacob-ebey/bun-bug-reproductions/ as a general bug-reproduction location for anything else I run across.

jacob-ebey avatar Sep 14 '23 06:09 jacob-ebey

oh i think i know why this happens

Jarred-Sumner avatar Sep 14 '23 06:09 Jarred-Sumner

I can reproduce this as well! Been having lots of issues in my repo because of it and having to do a lot of weird workarounds.

hex2f avatar Sep 14 '23 14:09 hex2f

Doesn't seem to happen when you enable minification

hex2f avatar Sep 14 '23 17:09 hex2f

Enabling minification seems to cause issues with JSX runtime imports not being added. Once this is fixed I'll explore getting a reproduction of the JSX issue well.

jacob-ebey avatar Sep 14 '23 19:09 jacob-ebey

Doesn't seem to happen when you enable minification

That's a workaround

raulfdm avatar Oct 07 '23 16:10 raulfdm

I have a hunch as to why minifying it solves the issue. I got the following output when compiling my own test case, with the files ./dependency.ts and ./main.ts, with the --minify-identifiers flag set:

function o() {
  console.log("foobar");
}
export {
  o as foobar
};

export { o as a };

To contrast, when I compile the dependency.ts file standalone, lines 7,8 are removed.

function o() {
  console.log("foobar");
}
export {
  o as foobar
};

When compiling a file and its dependency under the --minify-identifiers flag, it seems the bundler exports each symbol with its regular name and a minified one. Presumably, this is done to alllow the end user to import each symbols under either the original or shortened name.

anabelle2001 avatar Nov 02 '23 20:11 anabelle2001

I just hit this too. bun build --splitting client/ajax.ts --outdir static works fine, but if I do bun build --splitting client/*.ts --outdir static, that same file ends with

export {
  postJson,
  post,
  okFetch,
  baseFetch
};

export { ContentTypes, REDIRECT_CODE, postJson };

Note how postJson is exported twice now.


I see, because one of the other files also wants to import postJson, so it exports one with an unmangled name so the browser can import it, and then a mangled name so that the other file can import it. You can see this when --minify is enabled:

export {c as postJson, Y as post, K as okFetch, U as baseFetch};
export {v as a, Z as b, c};

The first set of exports is for the browser, the 2nd is for the other scripts.

mnpenner avatar Nov 18 '23 08:11 mnpenner

Any update on this?

rdev06 avatar Apr 08 '24 23:04 rdev06

Is there a plan to solve this problem?

renzp94 avatar Apr 28 '24 07:04 renzp94