4 ms·
> 2. Lodash is tree-shakeable, so if you import the functions you use directly, only those will be included in your JS bundle. I don't use Lodash, but recently
by toastercat 4y ago
> 2. Lodash is tree-shakeable, so if you import the functions you use directly, only those will be included in your JS bundle.
I don't use Lodash, but recently began work on a project that does. I was surprised (along with the rest of the team) to find out that Lodash is not tree-shakeable by default. So if you do `import { debounce } from 'lodash';`, you're actually including the entirety of lodash in your bundle. More info here: https://lodash.com/per-method-packages https://lodash.com/per-method-packages
The recommended solution by the lodash team is to use `babel-plugin-lodash` (including a babel plugin to use less code? no thanks) or import the single modules directly like `import throttle from 'lodash/throttle';`. In the end, since we only used about 3 functions from lodash that were trivial to replicate, we just ditched lodash.
EDIT: There is also a `lodash-es` package with native ESM modules which may solve this issue for some. We avoided this also because it had implications with our toolchain.
- emptysea 4y agoYeah I switched a project over to lodash-es which fixed the tree shaking problem but it was painful to migrate everything
- lobo_tuerto 4y agoHow so? You only search and replace: "from 'lodash'" => "from 'lodash-es'" and you should be good. Unless you were importing individual functions.
- toastercat 4y agoCan't speak for them, but we had some CJS/ESM interop issues with our (large and overcomplicated) babel toolchain as well as with our test suite.
- emptysea 4y agoYeah exactly what happened to us, it’s solvable but annoying Also we did have a mix of things like import _ from ‘lodash’ and import foo from ‘lodash/foo’, etc
- korijn 4y agoImporting single modules is so trivial and makes it tree shakeable. Is that really unacceptable?
- afavour 4y ago[delete]
- jwolfe 4y agoYou're misreading that source. It's the separate npm packages that are deprecated, not the module imports.
- deleted 4y ago[deleted]
- toastercat 4y agoWhat's being deprecated is the single module packages. So currently, you can do `npm install lodash.merge` and then just use that. I believe the post you're replying to is referring to what I originally referred to (doing something like `const throttle = require('lodash/throttle')`, which you would install lodash normally for `npm install lodash`).
- deleted 4y ago[deleted]
- toastercat 4y agoIt's not, but for 3 functions, and reducing our dependencies, it was worth just cutting lodash out. We have been happy with the change. EDIT: And to be a bit pedantic, importing single modules is not really treeshaking. You're literally just importing and including the entirety of single modules.
- joshmanders 4y ago> EDIT: And to be a bit pedantic, importing single modules is not really treeshaking. You're literally just importing and including the entirety of single modules. I'd say that's basically a poor man's treeshaking because you're telling it you only want that module and its dependencies. Not it trying to figure it out for you.
- domh 4y agoFor a few years I was opting to use the per-method packages from npm (`npm install lodash.pick`) but they just stopped being updated... https://www.npmjs.com/package/lodash.pick https://www.npmjs.com/package/lodash.pick has not been updated in 7 years and is stuck at v4.4.0. I guess I missed the memo on that... and none of the npm packages have been deprecated to reflect the state of things. Now going onto the lodash package on npm, that's listed as v4.17.21 and hasn't been published in over 2 years: https://www.npmjs.com/package/lodash https://www.npmjs.com/package/lodash Now the lodash package on github, the latest release is listed as v4.0.0 from 2016: https://github.com/lodash/lodash/releases https://github.com/lodash/lodash/releases There have been no commits to the main branch on github in almost 2 years: https://github.com/lodash/lodash/commits/master https://github.com/lodash/lodash/commits/master I consider lodash to be deprecated at this point, and will always go for a lightweight function pulled from somewhere like SO or No Lodash.
- kipple 4y agoOh wow, I didn't realize how quiet lodash has been Activity on master really died down mid-2017 [1] And the most recent closed PR is jdalton this year (Jan 4 '23) saying he's not looking to expand lodash [2] [1] https://github.com/lodash/lodash/graphs/contributors https://github.com/lodash/lodash/graphs/contributors [2] https://github.com/lodash/lodash/pull/5575#issuecomment-1371605435 https://github.com/lodash/lodash/pull/5575#issuecomment-1371...
- toastercat 4y agoWhat's more surprising is that Underscore.js[1] has seen more activity recently than Lodash (which was touted as Underscore's successor for years). [1] https://github.com/jashkenas/underscore https://github.com/jashkenas/underscore
- kipple 4y agoInteresting! Looks like this jgonggrijp user has been keeping things moving for the last couple years: https://github.com/jashkenas/underscore/graphs/contributors https://github.com/jashkenas/underscore/graphs/contributors