Skip to content

treeshaking generate different result from webpack #2049

@hardfist

Description

@hardfist

webpack has a nice feature,when set sideEffects to false, if a import x from b, and x comes from b's reexport from c, then bundler could shake whole side effect in b

  • index.js
import { internal } from './lib'
console.log('outer',internal);
  • lib.js
export const answer = 42;
export const secret = 10;
console.log('lib'); // because internal comes from reexport other than lib.js, so this side effect could be shaked
export { internal } from './reexport';
  • reexport.js
console.log('internal');
export const internal = 100;

different bundler result are shown, and you can see webpack tree shake the lib.js side effect, but esbuild and rollup does not, https://github.com/hardfist/treeshaking-demo

  • rollup
    image

  • esbuild
    image

  • webpack
    image

This feature is documented here https://webpack.js.org/guides/tree-shaking/#clarifying-tree-shaking-and-sideeffects
image

and vue change sideEffects to true to avoid shaking sideEffects vuejs/core#1263, so I'm wondering whether esbuild could align with webpack or webpack's behavior is not right.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions