Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-jsx-auto-classnames

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-jsx-auto-classnames

If the className property uses a jsx object, the classnames dependency is automatically introduced

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-90%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-jsx-auto-classnames

import xxx from 'classnames'

This plugin is used to automatically add and supplement xxx() to the className property of jsx

NPM version

English | 简体中文

📦 Install

npm i babel-plugin-jsx-auto-classnames --save-dev

or

pnpm add babel-plugin-jsx-auto-classnames -D

🖥 config

vite.config.js

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// import JsxAutoClassnames from 'babel-plugin-jsx-auto-classnames'

export default defineConfig({
  plugins: [react({
    babel: {
      // plugins: [JsxAutoClassnames],
      plugins: [
        ['jsx-auto-classnames', {
          extensions: ['.jsx', '.tsx'],
          excludes: ['src/excludes']
        }]
      ]
    }
  })]
})

.babelrc

{
  "plugins": [
    "jsx-auto-classnames"
  ]
}

🔨 Usage

normal

<div
  className={{
    selected: true
  }}>
</div>

⬇️

import _classNames from 'classnames'
<div
  className={_classNames({
    selected: true
  })}>
</div>

Reuse the alias of the imported package

import xxx from 'classnames'
function App () {
  return (
    <>
      <div
        className={xxx({
          disabled: true
        })}>
      </div>
      <div
        className={{
          selected: true
        }}>
      </div>
    </>
  )
}

⬇️

import xxx from 'classnames'
function App () {
  return (
    <>
      <div
        className={xxx({
          disabled: true
        })}>
      </div>
      <div
        className={xxx({
          selected: true
        })}>
      </div>
    </>
  )
}

Options

extensions

  • Type: string[]
  • Default: ['.jsx', '.tsx']

excludes

  • Type: string[]
  • Default: ['node_modules']

TODO

  • Avoid multiple imports of classnames' dependency packages
  • Find a compatible solution for TS
declare namespace React {
  interface HTMLAttributes<any> extends AriaAttributes, DOMAttributes<any>  {
    className?: Record<string, boolean> | (string | Record<string, boolean>)[]
  }
}
  • Add excludes attributes
  • Add extensions attributes
  • Unit Testing

Keywords

FAQs

Package last updated on 24 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc