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

@ra-lib/babel-plugin-attribute-wrapper

v0.0.21

Published

jsx 属性包装babel插件

Downloads

152

Readme

babel-plugin-attribute-wrapper

jsx 属性包装babel插件

API

| 属性 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | importName | 非必须,引入名称,importName存在 import { [importName] as method} from packageName; 不存在:import method from packageName | string | - | | importName | 必须,引入的报名 | string | - | | attributeName | 必须,要处理的属性名称,比如 r-code、className等 | string | - | | methodName | 非必须,处理函数名称 | string | method | | conditional | 非必须,是否使用三元操作 | boolean | false | | replaceAttributeName | 非必须,需要替换的属性名称 | string | conditional===true ? null : wrapperAttributedName 丨丨 attributeName; | | wrapperAttributedName | 非必须,值需要包裹的属性名称,[attributeName]={xxx} onClick={handleClick} => onClick={method(xxx, handleClick)} | string | - | | negation | 非必须,取反 disabled={!_method(xxx)} | boolean | false |

示例

className={xxx} 转 className={_method(xxx)}

babel配置:

{
    plugins: [
        [
            'babel-plugin-attribute-wrapper',
            {
                packageName: 'classnames',
                attributeName: 'className',
            },
            'className',
        ]
    ]
}

jsx源码:

<div>
    <div>包裹classnames</div>
    <div className={[styles.title, {[styles.disabled]: disabled}]}/>
    <div className="title"/>
</div>

转换为:

import _method from "classnames";

<div>
    <div>包裹classnames</div>
    <div className={_method([styles.title, {[styles.disabled]: disabled}])}/>
    <div className={_method("title")}/>
</div>

三元运算_method(xxx) ? : null

babel配置:

{
    plugins: [
        [
            'babel-plugin-attribute-wrapper',
            {
                importName: 'hasPermission',
                packageName: 'src/commons',
                attributeName: 'r-code',
                conditional: true,
            },
            'permission-remove',
        ]
    ]
}

jsx源码:

<div>
    <div>三元操作</div>
    <div r-code/>
    <div r-code="ADD_USER"/>
    <div r-code={a.code}/>
</div>

转换为:

import {hasPermission as _method} from "src/commons";

<div>
    <div>三元操作</div>
    {_method(true) ? <div/> : null}
    {_method("ADD_USER") ? <div/> : null}
    {_method(a.code) ? <div/> : null}
</div>

添加disabled={!_method(xxx)}属性

babel配置:

{
    plugins: [
        [
            'babel-plugin-attribute-wrapper',
            {
                importName: 'hasPermission',
                packageName: 'src/commons',
                attributeName: 'd-code',
                replaceAttributeName: 'disabled',
                negation: true,
            },
            'permission-disabled',
        ]
    ]
}

jsx源码:

<div>
    <div>替换为disabled属性</div>
    <div d-code="ADD"/>
    <div d-code>只写r-code属性,缺省值,默认true</div>
</div>

转换为:

import {hasPermission as _method} from "src/commons";

<div>
    <div>替换为disabled属性</div>
    <div disabled={!_method("ADD")}/>
    <div disabled={!_method(true)}>只写r-code属性,缺省值,默认true</div>
</div>

r-report="用户保存" onClick={this.handleClick} => onClick={_method("用户保存", this.handleClick)}

babel配置:

{
    plugins: [
        [
            'babel-plugin-attribute-wrapper',
            {
                importName: 'report',
                packageName: 'src/commons',
                attributeName: 'r-report',
                wrapperAttributedName: 'onClick',
            },
            'wrapper',
        ]
    ]
}

jsx源码:

<div>
    <div>劫持onClick</div>
    <div r-report={reportToServer}></div>
    <div r-report="用户保存" onClick={this.handleClick}></div>
    <div r-report={a && b} onClick={handleClick}></div>
    <div r-report={true} onClick={(e) => handleCLick(e, 12)}></div>
</div>

转换为:

import {report as _method} from "src/commons";

<div>劫持onClick</div>
<div onClick={_method(reportToServer)}></div>
<div onClick={_method("用户保存", this.handleClick)}></div>
<div onClick={_method(a && b, handleClick)}></div>
<div onClick={_method(true, e => handleCLick(e, 12))}></div>