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

tree-shaking: 只使用 Button, rc-field-form 即使没有使用也会包括在最终 bundle 中 #43019

Closed
magicdawn opened this issue Jun 14, 2023 · 4 comments · Fixed by #43023

Comments

@magicdawn
Copy link
Contributor

Reproduction link

https://github.com/magicdawn/antd-tree-shaking-demo

Steps to reproduce

只包括 Button

repo main.tsx 为

// import { Button } from 'antd'
import Button from 'antd/es/button'
console.log(Button)

只使用 Button, pnpm build 后, dist 里有来自 rc-field-form 中的代码, 搜索 (useForm, Form 等

导入链为

  • antd/es/button
  • antd/es/config-provider/index
  • antd/es/form/context
  • rc-field-form

好像 rc-field-form 里 RecForm 被 vite/rollup 识别为 side effects

var RefForm = InternalForm;
RefForm.FormProvider = FormProvider;
RefForm.Field = Field;
RefForm.List = List;
RefForm.useForm = useForm;
RefForm.useWatch = useWatch;

手动修改 node_modules/.pnpm/antd@5.6.1_react-dom@18.2.0_react@18.2.0/node_modules/rc-field-form/es/index.js

var RefForm = /*#__PURE__*/ (function () {
	var f = InternalForm;
	f.FormProvider = FormProvider;
	f.Field = Field;
	f.List = List;
	f.useForm = useForm;
	f.useWatch = useWatch;
	return f
})()

后再次打包(pnpm build), 可以看到 useForm 没了.
大小也小了, 562 -> 454, 未 minify 状态下少了 100 kB

$ pb 

> antd-demo@0.0.0 build /Users/magicdawn/projects/001_learn/antd/antd-demo
> tsc && vite build

vite v4.3.9 building for production...
✓ 225 modules transformed.
dist/index.html                  0.40 kB │ gzip:   0.28 kB
dist/assets/index-a724464a.js  562.84 kB │ gzip: 130.06 kB
✓ built in 2.16s

antd-demo on git main via node v18.14.2 took 4s 
$ pb

> antd-demo@0.0.0 build /Users/magicdawn/projects/001_learn/antd/antd-demo
> tsc && vite build

vite v4.3.9 building for production...
✓ 225 modules transformed.
dist/index.html                  0.40 kB │ gzip:   0.28 kB
dist/assets/index-05c373d8.js  454.76 kB │ gzip: 108.63 kB
✓ built in 2.30s

What is expected?

期望优化 tree-shaking

What is actually happening?

包含了未使用的代码(async-validator, form 等)

Environment Info
antd 5.6.1
React 18.2.0
System macos 12.6.1
Browser chrome 114.0.5735.106
@BoyYangzai
Copy link
Contributor

BoyYangzai commented Jun 14, 2023

ref #42499 #42604

@magicdawn
Copy link
Contributor Author

nice !
坐等新版~

@afc163
Copy link
Member

afc163 commented Jun 14, 2023

@magicdawn 还没有解法。。

@zombieJ
Copy link
Member

zombieJ commented Jun 15, 2023

试试 5.6.2-alpha.0,应该好了:

截屏2023-06-15 10 44 16

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

Successfully merging a pull request may close this issue.

4 participants