jashkenas / underscore

JavaScript's utility _ belt
https://underscorejs.org
MIT License
27.3k stars 5.53k forks source link

add sideEffects false to modules/package.json #2939

Closed marcin-wosinek closed 3 years ago

marcin-wosinek commented 3 years ago

Thanks for all the work done on this project.

I've been trying to use tree-shacking with the module. Looks that the current convention among bundler is to assume all code has side effects (cannot be removed, even if not used directly), unless there is sideEffect: false set on package.json (https://github.com/evanw/esbuild/issues/50#issuecomment-634107700)

The solution I'm proposing here can be see in action here:

...

asset main.js 19.7 KiB [emitted] [minimized] (name: main) orphan modules 83.1 KiB [orphan] 161 modules runtime modules 670 bytes 3 modules ./src/index.js + 161 modules 83.2 KiB [built] [code generated] webpack 5.50.0 compiled successfully in 1252 ms


while https://github.com/marcin-wosinek/underscore-tree-shaking/commit/9a52666b8cdbe2dc9f8939ef9361fffdc11d05a3 build to **2.9 KiB**:
```sh
$ npm ci; npm run webpack

...

asset main.js 2.9 KiB [emitted] [minimized] (name: main)
orphan modules 83.1 KiB [orphan] 161 modules
./src/index.js + 26 modules 12.4 KiB [built] [code generated]
webpack 5.50.0 compiled successfully in 754 ms
jgonggrijp commented 3 years ago

Sorry but no. This will actually break things because of https://underscorejs.org/docs/modules/index-default.html (note "Add all of the Underscore functions to the wrapper object"). In other words, the fully composed Underscore cannot be tree-shaken because all functions are mixed into _.

You can have perfect "treeshaking" by composing your own Underscore. I extensively documented how to do that in https://juliangonggrijp.com/article/introducing-modular-underscore.html (which is also linked from the Underscore website). If you prefer a video introduction, refer to https://www.telerik.com/react-wednesdays/what-i-learned-modularizing-underscore-with-julian-gonggrijp or https://www.youtube.com/watch?v=d4ORLdr3vWE.