oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.36k stars 2.78k forks source link

Bundler Duplicate Exports #5344

Open jacob-ebey opened 1 year ago

jacob-ebey commented 1 year ago

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.

Jarred-Sumner commented 1 year ago

oh i think i know why this happens

hex2f commented 1 year ago

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 commented 1 year ago

Doesn't seem to happen when you enable minification

jacob-ebey commented 1 year ago

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.

raulfdm commented 1 year ago

Doesn't seem to happen when you enable minification

That's a workaround

anabelle2001 commented 1 year ago

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.

mnpenner commented 1 year ago

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.

rdev06 commented 7 months ago

Any update on this?

eokic commented 4 months ago

Bump for visibility

vinny-silveira commented 4 months ago

Any updates? Also occurring in recent version, related issue #10631

dgpt commented 4 months ago

I just ran into this issue as well, confirm still happening as of bun 1.1.21

ceymard commented 2 days ago

Still there in 1.1.37 and blocking for bun adoption in our company's projects