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

问一下组件导出为啥这样写? #132

Closed
Go7hic opened this issue Dec 30, 2021 · 3 comments
Closed

问一下组件导出为啥这样写? #132

Go7hic opened this issue Dec 30, 2021 · 3 comments

Comments

@Go7hic
Copy link

Go7hic commented Dec 30, 2021

没有仔细研究,看了一下 button 组件,为啥要支持两种导出方式?是有其他目的么

import _Button from './Button';

import './style/index.js';

export type { ButtonProps } from './Button';
export * from './type';

export const Button = _Button;
export default Button;

即使要支持两种导出方式,这样写不是更简单么?

import Button from './Button';
...
export { Button };
export default Button;
@honkinglin
Copy link
Collaborator

honkinglin commented Dec 30, 2021

早期就是下面这样的写法,当时处理按需加载的时候发现由于导出的 Button 变量都指向了同一个引用也就是 Button.tsx 从而跳过 button/index.ts 的导入直接进入 button/Button.tsx 的编译,丢失了 style 的样式文件,中间重新赋值给了 _Button 变量就可以避免指向同一个引用保留住了 style 样式文件

@Go7hic
Copy link
Author

Go7hic commented Dec 30, 2021

早期就是下面这样的写法,当时处理按需加载的时候发现由于导出的 Button 变量都指向了同一个引用也就是 Button.tsx 从而跳过 button/index.ts 的导入直接进入 button/Button.tsx 的编译,丢失了 style 的样式文件,中间重新赋值给了 _Button 变量就可以避免指向同一个引用保留住了 style 样式文件

原来如此,没注意你们在这个组件入口的地方引入样式文件,一般样式在组件本身(Button.tsx)里引入,可能和你们采用的 CSS 方案有关吧。

@honkinglin
Copy link
Collaborator

honkinglin commented Dec 30, 2021

早期就是下面这样的写法,当时处理按需加载的时候发现由于导出的 Button 变量都指向了同一个引用也就是 Button.tsx 从而跳过 button/index.ts 的导入直接进入 button/Button.tsx 的编译,丢失了 style 的样式文件,中间重新赋值给了 _Button 变量就可以避免指向同一个引用保留住了 style 样式文件

原来如此,没注意你们在这个组件入口的地方引入样式文件,一般样式在组件本身(Button.tsx)里引入,可能和你们采用的 CSS 方案有关吧。

你说的在组件本身(Button.tsx) 里引入也是一个方案,那样的话确实也能保留住样式文件,不过感觉管理起来可能会比较混乱,不同组件有不同的叫法(Button.tsx、Input.tsx...),有的组件还需要嵌套其他子组件就需要引入对应文件夹下的 tsx 文件,文件层级跟代码约束比较多,统一在 index.ts 文件里处理看起来也比较规范一些

@Go7hic Go7hic closed this as completed Dec 30, 2021
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

No branches or pull requests

2 participants