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

[v5] remove es dir for better import #36395

Closed
dancerphil opened this issue Jul 6, 2022 · 7 comments
Closed

[v5] remove es dir for better import #36395

dancerphil opened this issue Jul 6, 2022 · 7 comments

Comments

@dancerphil
Copy link
Contributor

dancerphil commented Jul 6, 2022

What problem does this feature solve?

Since we want to remove lib and focus on ESM only, it is better remove es dir.

Then we can:

import Button from 'antd/button';

instead of:

import Button from 'antd/es/button';

and have a better tree shaking experience.

What does the proposed API look like?

import Button from 'antd/button';
@afc163
Copy link
Member

afc163 commented Jul 6, 2022

Why not import { Button } from 'antd'; ?

@Dunqing
Copy link
Contributor

Dunqing commented Jul 6, 2022

I like import { Button } from 'antd';

@izhouteng
Copy link

import Button from 'antd/button'; 这种不觉得啰嗦吗?一大堆import。。 直接解构啊。。多简洁

@dancerphil
Copy link
Contributor Author

dancerphil commented Jul 12, 2022

Sorry for not reply in time.

I agree that import { Button } from 'antd'; should be the most recommend import pattern.

I'm talking about some edge cases which have to import from es modules. Those will seems more trivial to import from without-es dir.

The main concept is that since we don't need to tell from lib and es, it's better just erase the es dir.

Some cases that I can imagine: 1. tree shaking without babel plugin. 2. import ts type from inner dirs. 3. some advanced usage of inner utils.

@izhouteng
Copy link

izhouteng commented Jul 16, 2022

关于你说的这个问题,我深表理解。

题主之所以不喜欢 import { Button } from 'antd' 的语法,一个重要的原因,是因为这样 tree shaking 体验很差。

Vite 中,像上面这样简写的解构导入,会在打包时让 Rollup 去分析整个 antd 包,从而极大的拖慢打包速度。

若改成 import Button from 'antd/es/button',则打包速度可以飞跃提升。

但这种写法,实在是很啰嗦,好在,有一个插件 vite-plugin-imp,可以帮我们自动转换这种写法,并自动引入样式。

其实它就是 babel-plugin-importVite 版,给你参考一下我的配置,希望对你有帮助。

  plugins: [
    {
      ...styleImport({
        resolves: [
          AntdResolve()
        ]
      }),
      apply: 'serve'
    },
    {
      ...vitePluginImp({
        libList: [
          {
            libName: 'antd',
            style (name) {
              return `antd/es/${name}/style/index.js`
            }
          },
          { libName: 'lodash-es', libDirectory: '' },
          { libName: '@ant-design/icons', libDirectory: 'es/icons', camel2DashComponentName: false }
        ]
      }),
      apply: 'build'
    }
  ],

另外,这个问题其实并不关 antd 的事,删除 lib 目录只留 es 目录更是duck不必。

@dancerphil dancerphil changed the title [experimental] remove es dir for better import [v5] remove es dir for better import Aug 3, 2022
@dancerphil
Copy link
Contributor Author

另外,这个问题其实并不关 antd 的事,删除 lib 目录只留 es 目录更是duck不必。

For clarification, in antd@v5, we may drop lib and ESM only, #34087.

So I'm talking about the further change after we drop lib.

If we decide that we will support lib for another major version, of course this will not happen.

@dancerphil
Copy link
Contributor Author

由于我们仍需要保留 dist 等文件夹,关闭此 issue

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

4 participants