NervJS / taro-ui

一款基于 Taro 框架开发的多端 UI 组件库
https://taro-ui.taro.zone
MIT License
4.52k stars 756 forks source link

能否增加css按需要加载对类babel-plugin-import插件的支持? #1305

Open erniu opened 3 years ago

erniu commented 3 years ago

这个功能解决了什么问题?

加上该功能的支持之后就再也不用手动引用对应的样式了,由插件自动去加载组件对应的样式,对开发人员很友好啊。

Ps: 主流的UI库基本都支持呢

你期望的功能是怎样的?

import { AtButton } from 'taro-ui'

      ↓ ↓ ↓ ↓ ↓ ↓

var button = require('taro-ui/lib/components/button');
require('taro-ui/dist/style/components/button.scss');
require('taro-ui/dist/style/components/loading.scss');

补充信息

目前使用babel-plugin-import基本上能做到单组件的按需加载,但是对一些复合组件来说,就显得无能为力了。

module.exports = {
  presets: [
    ['taro', {
      framework: 'react',
      ts: false
    }]
  ],
  plugins: [
    [
      "import", {
        libraryName: "taro-ui",
        libraryDirectory: `lib/components`,
        customName: function (name) {
          let component = name[0].toLowerCase() + name.substr(1);
          component = component.replace(/at-/, '').replace(/([A-Z])/g, $1 => `-${$1.toLowerCase()}`);
          return `taro-ui/lib/components/${component}`
        },
        customStyleName: function (name) {
          const component = name.replace(/at-/, '')
          return `taro-ui/dist/style/components/${component}.scss`
        }
      },
      "taro-ui"
    ]
  ]
}
// 单组件
import { AtAvatar } from 'taro-ui'

      ↓ ↓ ↓ ↓ ↓ ↓

var button = require('taro-ui/lib/components/avatar');
require('taro-ui/dist/style/components/avatar.scss');
// 复合组件

import { AtButton } from 'taro-ui'

      ↓ ↓ ↓ ↓ ↓ ↓

var button = require('taro-ui/lib/components/button');
require('taro-ui/dist/style/components/button.scss');

/*  AtButton组件依赖的loading组件就无法自动处理,这个就需要手动补充。要是能在button.scss中处理对loading的依赖就完美了
require('taro-ui/dist/style/components/loading.scss');
*/
taro-ui-bot[bot] commented 3 years ago

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~