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

Remove all uses of top-level lodash import #1882

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

robknight
Copy link
Member

@robknight robknight commented Sep 14, 2024

Closes https://linear.app/0xparc-pcd/issue/0XP-1200/replace-lodash-imports-with-specific-function-imports

Importing lodash like this: import _ from "lodash"; imports the entire lodash library, which is often not what you want if you just want to call, say, _.isEqual(). Instead, you can do import isEqual from "lodash/isEqual", which will import only the isEqual function and nothing else. Eliminating the unused code reduces the bundle size. Modern ESM libraries can be "tree-shaken" to eliminate dead code automatically, but lodash is not one of these.

In this PR, we avoid importing around 500KiB of uncompressed JavaScript by replacing all of the instances of import _ from "lodash" with imports of the specific functions to be used. The impact on final bundle size is somewhat smaller - maybe a 60KiB reduction in the size of the passport-client bundle - because lodash compresses very well. However, this will be a meaningful reduction in runtime parsing of the resulting JavaScript.

In addition to changing the existing imports, I've added an ESLint rule to ensure that no new top-level imports are added.

This was prompted by noticing that, for the reference PARCNET client build, the single largest dependency was lodash, as a result of it being imported by @pcd/gpc:

image

@robknight robknight force-pushed the robknight/0xp-1200-replace-lodash-imports-with-specific-function-imports branch from 0605758 to 5b14489 Compare September 15, 2024 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant