npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-transform-import-module

v1.0.1

Published

按需导入第三方组件库

Downloads

5

Readme

[TOC]

按需导入库模块

添加依赖:

npm i -D babel-plugin-transform-import-module

它可以转化下面这何种导入:

import { Row, Grid as MyGrid } from 'react-bootstrap';

将其转换为:

import Row from 'react-bootstrap/lib/Row';
import MyGrid from 'react-bootstrap/lib/Grid';

只需要在babel配置中添加插件并配置:

{
  plugins: [
    ['transform-import-module', {
      library: {
          'react-bootstrap': (componentName, alias) => [{
            moduleId: alias || componentName,
            source: `react-bootstrap/lib/${_.kebabCase(componentName)}`,
          }],
      }
    }]
  ]
}

对于antd这种需要额外导入样式文件的也支持:

import { Button } from 'antd';

将其转换为:

import Button from 'antd/lib/button';
import 'antd/lib/button/style/index.js',

只需要在babel配置中添加插件并配置:

{
  plugins: [
    ['transform-import-module', {
      library: {
          'antd': (componentName, alias) => [{
            moduleId: alias || componentName,
            source: `antd/lib/${_.kebabCase(componentName)}`,
          }, {
            source: `antd/lib/${_.kebabCase(componentName)}/style/index.js`,
          }],
      }
    }]
  ]
}

利用好配置,可以支持对项目中所有的库模块的按需导入,并且支持高度定制化的导入方式,如支持antd的主题切换(切换导入组件的样式文件地址)。

选项

library

类型为:{ [libraryId: string]: (componentName: string, alias: string) => {moduleId: string, source: string}[] }

库按需导入配置。

值为一个对象,每个键为库的id, 如antd, @component等,等源码中导入语句中的地址匹配库id时,就会开启按需导入。如:

import { Button } from 'antd';
import { MyButton } from '@component';

如果配置了antd, @component两个库的化,就会触发。

library对象的值为一个回调配置函数,函数会传入两个参数componentNamealias, 返回一个新导入的信息列表。

  • componentName为导入的组件名,如import { Button } from 'antd';componentName会为Button;
  • alias为导入的具名别名,如import { Button as AntdButton } from 'antd';,那么alias会为AntdButton;
  • 返回的列表中每一个信息为一个对象,对象的格式为:{moduleId: string, source: string}
    • moduleId为新导入语句的模块id, 指导新生成的导入语句中的具名,如设置ModuleId会生成import ModuleId from '...',不设置时,会生成命名控件导入,如import '....'
    • source为新导入语句的导入地址,如设置/path/to/file.js将会生成语句import moduleId from '/path/to/file.js'

preventFullImport

是否允许在源码中全导入了前配置了按需导入的库。

如果设置为true, 且设置了:

{
  plugins: [
    ['transform-import-module', {
      library: {
          'react-bootstrap': (componentName, alias) => [{
            moduleId: alias || componentName,
            source: `react-bootstrap/lib/${_.kebabCase(componentName)}`,
          }],
      }
    }]
  ]
}

那么在源码中如果存在:

import * as ReactBootstrap from 'react-bootstrap';

那么就会报错。