22

I've come into an argument with my co-workers for which we can't seem to find an answer from any official source (MDN, webpack documentation, ...). My research hasn't yielded much either. There seems to be doubt even when it comes to importing as well.

Our setup is Webpack, Babel, and a typical React / Redux app. Take this example:

export * from './actions';
export * from './selectors';
export * from './reducer';

export { default } from './reducer';

This allows me to separate a Redux module into logical sections, making the code easier to read and maintain.

However, it is believed by some of my co-workers that export * from may, in fact, harm webpack's tree-shaking capabilities, by tricking it into believing an export is used when it is in fact just being re-exported.

So my question is, are there any facts proving or disproving this?

pbondoer
  • 538
  • 7
  • 15
  • 2
    Why don't you just set up a dummy project and try it? I realize that's not as authoritative as actual documentation from the rollup team, but it ain't nuthin. – Jared Smith Feb 20 '19 at 19:11
  • https://github.com/webpack/webpack/issues ... Seriously, with open source stuff sometimes the best place to ask is at the repo... – Heretic Monkey Feb 20 '19 at 22:16
  • @Bergi All of those relate to importing. I'm specifically asking about re-exporting using `export * from './module'` which is a different thing. – pbondoer Feb 21 '19 at 08:19
  • @pbondoer Ooops, you're right, I totally confused the syntax with a namespace import where the tree-shaking optimisation is less trivial. – Bergi Feb 21 '19 at 09:41
  • @HereticMonkey A link to an index page (that is guaranteed to change over time) was the last thing I needed to read. – Romain Vincent Nov 13 '21 at 19:36
  • @RomainVincent What are you on about? Read the remainder of the comment: "the best place to ***ask*** is at the repo" not the best place to read. Also, this question was already answered quite well by Bergi; do you just click all URLs on a web page or something? – Heretic Monkey Nov 14 '21 at 14:27

1 Answers1

19

It is believed that export * from may harm webpack's tree-shaking capabilities, by tricking it into believing an export is used when it is in fact just being re-exported.

No, import/export declarations don't do anything but set up an alias to the exported variable, they do not count as a "use". Given their semantics, they are tracked specially by any bundler and will not adversely affect tree-shaking.

Not even a properly done renamed alias will break that:

export { X as Y } from '…';

import { X as Y } from '…';
export { Y }

import { X } from '…';
export { X as Y }

but usage in a statement would count as usage and break (non-sophisticated) optimisations:

import { X } from '…';
export const Y = X; // avoid!

So my question is, are there any facts proving or disproving this?

You can just try it out and see it work.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375