0

I am working on node@6.11.0, webpack@3.4.1

file.js

export function foo () {
  return 'foo'
}

export function bar() {
  return 'bar'
}

main.js

import { foo } from './file'
import { log } from 'mathjs'
foo()
log(10000,10)

And bundle.js include all methods from math.js, so i wonder if tree-shaking works. and the function bar has signed with /* unused harmony export bar */ when i use tree-shaking, the bundlejs should be only include foo and lod methods, shouldn't it? git clone https://github.com/z2014/All-test-demo,and test

2014
  • 103
  • 2
  • 9

1 Answers1

1

Unused code is removed by a minifier (such as UglifyJS). Tree-shaking just makes it possible for minifier to remove it.

So, in your case, enabling UglifyJS will most likely solve the problem.

You can read more about this behavior in https://webpack.js.org/guides/tree-shaking, https://github.com/webpack/webpack/tree/master/examples/harmony-unused and https://github.com/webpack/webpack/issues/2866

Quoc-Anh Nguyen
  • 4,798
  • 1
  • 22
  • 34
  • but when i use UglifyJS plugin ,i got 1571 KIB, and when i don't use tree-shaking, it also got 1573 KIB, so i wonder if tree-shaking works – 2014 Sep 24 '17 at 10:32
  • I have no idea about your problem because I have no code here, but I think you can try to find whether your unused code in your build. PS: Remember to run webpack with flag `-p` for production build. – Quoc-Anh Nguyen Sep 24 '17 at 10:41
  • One more question: What function do you want to remove from build? Tree shaking only works with es module not normal commonjs package (what you install by npm). I see you use `babel-preset-es2015`, it will transform all es module to normal commonjs so tree-shaking won't work – Quoc-Anh Nguyen Sep 24 '17 at 10:47
  • i use it to compare with `presets: [['es2015', {modules:false}], 'stage-3', 'react']` – 2014 Sep 24 '17 at 10:53
  • preset es2015 doesn't have option `modules: false`. You should my new MR – Quoc-Anh Nguyen Sep 24 '17 at 11:07
  • but when i set `modules: false`, it does tree-shaking – 2014 Sep 25 '17 at 01:28