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-module-call-import

v1.0.1

Published

根据库命名空间的调用加载模块

Downloads

4

Readme

根据库命名空间的调用导入模块

根据一个库的命名空间的调用进行按需导入。

添加依赖:

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

它能将下面代码:

import utils from 'utils@'
utils.downloadFile('path/to/file')

转换为:

import _downloadFile from 'utils@/downloadFile'
_downloadFile('path/to/file')

或者将下面代码:

import * as Antd from 'antd';
ReactDOM.render(<Antd.Button>xxxx</Antd.Button>);

转换为:

import Button from 'antd/lib/button';
import('antd/lib/button');
ReactDOM.render(<Button>xxxx</Button>);

还支持将

import El from 'element-ui';

export default {
  name: 'TestComponent',
  components: {
    [El.ButtonGroup.name]: El.ButtonGroup,
    [El.Button.name]: El.Button,
  },
};

转换为

import ButtonGroup from 'element-ui/lib/button-group';
import Button from 'element-ui/lib/button';

export default {
  name: 'TestComponent',
  components: {
    [ButtonGroup.name]: ButtonGroup,
    [Button.name]: Button,
  },
};

选项

components

类型为:{ [componentId: string]: (libraryName: string, ...modules: string[]) => {source: string, extras: string[]} }

库按需导入配置。

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

import * as Ad from 'antd';
import Comp from '@component';

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

library对象的值为一个回调配置函数,函数传递的第一个参数为libraryName库名,其他参数都作为modules模块列表, 返回一个新导入的信息。

  • componentName为导入的组件名,如import { Button } from 'antd';libraryName会为antd;
  • modules为库引用时的引用路径上的模块列表,如import * as Ad from 'antd'; Ad.a.b.c.d,那么modules会为['a', 'b', 'c', 'd'], 对模块列表生成动态的导入路径,可以控制那些是作为模块导入路径,那些是模块上的属性;
  • 返回的一个新导入信息对象,对象的格式为:{source: string,extras: string[]}
    • source为新导入语句的导入地址,如设置/path/to/file.js将会生成语句import moduleId from '/path/to/file.js'
    • extras为额外导入地址,通常可用于UI组件库的其他资源导入,如样式资源。不配置或者配置为空不生成额外导入;