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?
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?
Share Improve this question asked Feb 20, 2019 at 18:55 pbondoerpbondoer 5687 silver badges15 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 20It 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.
export * from './module'
which is a different thing. – pbondoer Commented Feb 21, 2019 at 8:19