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-component-import

v0.1.2

Published

Modular babel plugin for ui library import like `comby-lib`, `antd` etc.

Downloads

7

Readme

babel-plugin-component-import

Modular babel plugin for ui library import like comby-lib, antd etc.

[TOC]


What to do ?

  • Transform from import whole package to import individual components, so to reduce code size.
  • Automatic import individual component style

Usage

npm i babel-plugin-component-import --save-dev

Via babel.config.js, .babelrc or babel-loader.

plugins: [
  ['babel-plugin-component-import', {
    libraryName: 'comby-lib-mobile',
    libraryDirectory: 'lib',
    style: true,
    styleLibraryName: 'comby-lib-mobile_default',
    styleDirectory: 'dist',
    styleLibraryPath: '',
    camel2DashComponentName: true,
    camel2UnderlineComponentName: false,
    fileName: '',
    customName: null,
  }]
]

Options

libraryName

libraryName: String

The library name you are using, like comby-lib, antd etc.

libraryDirectory

libraryDirectory: String - default is 'lib'

The components directory in libraryName.

fileName

fileName: String - default is ''

The component file name.

camel2DashComponentName

camel2DashComponentName: Boolean - default is true

Transform component name from camel style to dash style if true

camel2UnderlineComponentName

camel2UnderlineComponentName: Boolean - default is false

Transform component name from camel style to underline style if true. Priority level is higher than camel2DashComponentName

styleLibraryName

styleLibraryName: String

The style library you are using.

styleDirectory

styleDirectory: String - default is 'dist'

The style direactory in styleLibraryName.

styleLibraryPath

styleLibraryPath: String

The style library path, the priority level is lower than styleLibraryName.

NOTICE: The styleLibraryName, styleDirectory and styleLibraryPath determine styleImportPath, where to import style file.

import { join } from 'path'

const importPath = customName
                    ? customName(componentName)
                    : join(libraryName, libraryDirectory, componentName, fileName)

let styleImportPath = importPath

if (styleLibraryName) {
  styleImportPath = join(styleLibraryName, styleDirectory, componentName)
} else if (styleLibraryPath) {
  styleImportPath = join(styleLibraryPath, componentName)
}

style

css: Boolean|String:Function - default is false
  • { style: false }: do not import style file.
  • { style: true }: import js and css modularly which path is ${styleImportPath}/style
  • { style: string }: import js and css modularly which path is ${styleImportPath}/style/${style}
  • { style: (name) => `${name}/style/2x` }: import js and css modularly & css file path is ${name}/style/2x

customName

Customize import file path.

For example, the default behavior:

import { TimePicker } from 'comby-lib'
↓ ↓ ↓ ↓ ↓ ↓
var _button = require('comby-lib/lib/time-picker');

Use customName:

[
  'babel-plugin-component-import',
    {
      libraryName: 'comby-lib',
      customName: (name: string) => {
        if (name === 'time-picker'){
          return 'comby-lib/lib/custom-time-picker'
        }
        return `comby-lib/lib/${name}`
      }
    }
]

Result is:

import { TimePicker } from 'comby-lib'
↓ ↓ ↓ ↓ ↓ ↓
var _button = require('comby-lib/lib/custom-time-picker')

Example

{ "libraryName": "comby-lib-mobile" }

import { Button } from 'comby-lib-mobile'
ReactDOM.render(<Button>xxxx</Button>)

      ↓ ↓ ↓ ↓ ↓ ↓

var _button = require('comby-lib-mobile/lib/button')
ReactDOM.render(<_button>xxxx</_button>)

{ "libraryName": "comby-lib-mobile", style: "css" }

import { Button } from 'comby-lib-mobile'
ReactDOM.render(<Button>xxxx</Button>)

      ↓ ↓ ↓ ↓ ↓ ↓

var _button = require('comby-lib-mobile/lib/button')
require('comby-lib-mobile/lib/button/style/css')
ReactDOM.render(<_button>xxxx</_button>)

{ "libraryName": "comby-lib-mobile", style: true }

import { Button } from 'comby-lib-mobile'
ReactDOM.render(<Button>xxxx</Button>)

      ↓ ↓ ↓ ↓ ↓ ↓

var _button = require('comby-lib-mobile/lib/button')
require('comby-lib-mobile/lib/button/style')
ReactDOM.render(<_button>xxxx</_button>)