Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimize lodash dependency #7747

Open
51 of 54 tasks
exoego opened this issue May 19, 2020 · 33 comments
Open
51 of 54 tasks

Minimize lodash dependency #7747

exoego opened this issue May 19, 2020 · 33 comments

Comments

@exoego
Copy link
Contributor

exoego commented May 19, 2020

Use case description

#7680 (comment)

lodash offers lots of utility, but nowadays we only need the small parts of lodash, since recent Node.js supports native convenient counterpart (e.g. Array#forEach(f) over _.forEach(array, f)).

Minimizing lodash dependency will achieve

  • Faster runtime speed by native implementation
  • Smaller bundle size of serverless framework
  • Friendly for developers who are more familiar with standard JS, not lodash.

Proposed solution

  • Use convenient native counterpart as much as possible
  • Depend only some part of lodash, e.g. lodash-memoize and so on.

Note: Ideally if we handle all below cases, and each is addressed with separate PR

@medikoo
Copy link
Contributor

medikoo commented May 20, 2020

@exoego great thanks for putting that into issue.

I've updated description with list of all functions which I believe we can replace. I think best if each is handled with separate PR's, so we process it in small steps (change touches sensitive parts, so we need to be careful)

I've also added good first issue and help wanted labels as it appears there's a lot to cover

@chrisVillanueva
Copy link

Some news about lodash. There's a security issue noted in this issue:

https://github.com/lodash/lodash/issues/4738

The current version fails 3rd party security scans.

@chrisVillanueva
Copy link

@medikoo
Is there a current file or sub-directory we can place utility functions? Where can we add a function to execute deep and/or strict equality Object checks? I looked at /utils and /lib/classes/Utils.js. I didn't see a place to introduce a general, shareable utility function. Utils.js defines a class, but I only need a simple function to use across files. Any suggestions?

@medikoo
Copy link
Contributor

medikoo commented Jun 3, 2020

Is there a current file or sub-directory we can place utility functions?

@chrisVillanueva if you mean a language level utilities, then I think we should not introduce any here, but instead rely of utility library of choice. Currently we rely on lodash, and problem is that we overuse it for cases which should rather be handled with native API, and this issue is purely about that.

We may at some point consider replacing lodash completely, but I think then it should replaced with other utility library. We definitely should not attempt to build and maintain lang utilities in scope of this project.

@chrisVillanueva
Copy link

Thanks for clarification. This simplifies the issue.

@G-Rath
Copy link
Contributor

G-Rath commented Jan 1, 2021

Looks like _.reduce and _.forOwn are done, but not checked off :)

Also if you've got a build tool like TS or Babel, you could use optional chaining to replace most of the _.get usages (or something like ts-optchain to replace all the usages).

@medikoo
Copy link
Contributor

medikoo commented Jan 2, 2021

Looks like _.reduce and _.forOwn are done, but not checked off :)

@G-Rath thanks for pointing. Updated description

Also if you've got a build tool like TS or Babel, you could use optional chaining to replace most of the _.get usages (or something like ts-optchain to replace all the usages).

Once support for Node.js v12 is dropped we will simply move to native syntax. We do not plan to introduce any transpilation step into this package, as it comes with significant maintenance cost

@pjmattingly
Copy link
Contributor

I'd like to work on:

Replace _.merge with Object.assign. Note they're not equivalent (_.merge does deep merge), so it should be done with care in all places where it's safe to rely on Object.assign instead (majority of cases)

If that would be okay. It looks like it's still up for grabs.

@pgrzesik
Copy link
Contributor

pgrzesik commented Jul 6, 2022

Sure thing, that would be awesome @pjmattingly 👍

@medikoo
Copy link
Contributor

medikoo commented Jul 6, 2022

I've added also _.flat and _.flatMap to the list, as those can be replaced now since we dropped support for Node.js v10

@pjmattingly
Copy link
Contributor

pjmattingly commented Jul 24, 2022

I'd like to take Replace _.flat with array.flat; I'm guessing this should be _.flatten?

Submitted PR: #11271

@pjmattingly
Copy link
Contributor

pjmattingly commented Jul 24, 2022

Next, on to: Replace _.flatMap with array.flatMap

PR: #11272

@pjmattingly
Copy link
Contributor

pjmattingly commented Jul 24, 2022

Next, Replace _.set with normal obj.prop = value assignments, but only in top level property checks or where given property path is ensured to exist

Edit: did my best! #11273

@pjmattingly
Copy link
Contributor

Then, the last one: Replace _.get with normal obj.prop checks, but only in top level property checks or where given property path is ensured to exist

@sleepwithcoffee
Copy link
Contributor

hi @medikoo what about the case with _.isObject(obj)? I see there is a mix between lodash and using const isObject = require('type/object/is')

@medikoo
Copy link
Contributor

medikoo commented Mar 10, 2023

hi @medikoo what about the case with _.isObject(obj)? I see there is a mix between lodash and using const isObject = require('type/object/is')

@duytrandev I'd welcome PR that switches to type/object/is, still it's that's not really a scope here. Point of this issue is get rid of lodash in cases where native counterpart can be used, and we don't have plans to get rid of it completely (at least not at this point)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests