Skip to content

Conversation

Copy link

Copilot AI commented Nov 28, 2025

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

Addresses request to switch from lodash to lodash-es for better tree-shaking support.

💡 Background and solution

lodash uses CommonJS exports which limits tree-shaking effectiveness. lodash-es provides ES module exports enabling modern bundlers to eliminate unused code.

Changes:

  • lodashlodash-es in packages/hooks dependencies
  • @types/lodash@types/lodash-es in devDependencies
  • Updated all 7 import statements from lodash/* to lodash-es/*
  • Added explicit return type interfaces for useThrottleFn and useDebounceFn to fix TypeScript declaration portability

Bundle size (UMD):

  • Before: 96,656 bytes
  • After: 96,099 bytes (~0.6% reduction)

Note on tree-shaking benefits:
The minimal UMD bundle reduction is expected - the UMD bundle (dist/ahooks.js) includes all code and doesn't tree-shake since it's designed for direct browser usage.

The primary benefit is for consumers using ES module bundlers (webpack, rollup, Vite, etc.) who import from es/index.js via the "module" field. The package already has the required configurations:

  • "module": "./es/index.js" - ES module entry point
  • "sideEffects": false - Enables tree-shaking

With lodash-es, when a consumer imports only specific hooks like useDebounceFn, their bundler can now tree-shake unused lodash functions. With the previous lodash (CommonJS), bundlers couldn't eliminate unused code even from the ES build. No packaging config changes are needed - the tree-shaking benefit is realized on the consumer side.

📝 Changelog

Language Changelog
🇺🇸 English Replace lodash with lodash-es for improved tree-shaking. No API changes.
🇨🇳 Chinese 将 lodash 替换为 lodash-es 以改善 tree-shaking。无 API 变更。

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed
Original prompt

This section details on the original issue you should resolve

<issue_title>ahooks依赖loadash是否不好shaking?</issue_title>
<issue_description>换成lodash-es是否可以减少包体积?</issue_description>

<agent_instructions>Can switching to lodash es reduce the size of the package?

Replace it and compare the volume of the front and rear bags</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 28, 2025

npm i https://pkg.pr.new/ahooks@2882
npm i https://pkg.pr.new/@ahooksjs/use-url-state@2882

commit: 2c347f3

Copilot AI changed the title [WIP] Improve package size by switching to lodash-es refactor: replace lodash with lodash-es for better tree-shaking Nov 28, 2025
Copilot AI requested a review from meet-student November 28, 2025 01:54
Copilot finished work on behalf of meet-student November 28, 2025 01:54
@meet-student
Copy link
Collaborator

@copilot
lodash-es for better tree-shaking

It seems that the effect is not significant, and you did not adjust the packaging related configurations

Copy link
Author

Copilot AI commented Nov 28, 2025

@copilot
lodash-es for better tree-shaking

...

The ~0.6% reduction in UMD bundle is expected - the UMD bundle (dist/ahooks.js) includes all code and doesn't tree-shake since it's meant to work in any environment.

The key benefit is for consumers using ES module bundlers (webpack, rollup, Vite, etc.) who import from es/index.js via the "module" field. The package already has the required configurations:

  • "module": "./es/index.js" - ES module entry
  • "sideEffects": false - Enables tree-shaking

With lodash-es, when a consumer imports only useDebounceFn, their bundler can now tree-shake unused lodash functions. With the previous lodash (CommonJS), bundlers couldn't eliminate unused code even from the ES build.

The packaging configs don't need changes - the tree-shaking benefit is realized on the consumer side, not in ahooks' own builds.

Copilot finished work on behalf of meet-student November 28, 2025 02:13
@meet-student meet-student marked this pull request as ready for review December 3, 2025 03:32
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.

ahooks依赖loadash是否不好shaking?

3 participants