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-clsx

v0.3.5

Published

Add `clsx()` automatically to `className` in `React` and support `Typescript`.

Downloads

19

Readme

babel-plugin-clsx

CI GitHub

Automatically add clsx for className in React and have the same fun without importing and writing it.

It is important to note that this library supports the use of Typescript projects. No other library of its kind has been found to do this.

Before doing so, make sure that clsx is installed or another available className handler exists for your project.

Install

npm

npm i --save-dev babel-plugin-clsx

yarn

yarn add --save-dev babel-plugin-clsx

pnpm

pnpm add --save-dev babel-plugin-clsx

Usage

Add the babel configuration

{
  "plugins": ["clsx"]
}

Your code

<div className={['c1', 'c2']} />;
<div className={{ c1: true, c2: true }} />;

After compilation

import _clsx from 'clsx';
<div className={_clsx('c1', 'c2')} />;
<div className={_clsx({ c1: true, c2: true })} />;

Options

options.static

| Type | Default | | --------- | ------- | | boolean | true |

By default, static mode is enabled, in which only array and object are converted, effectively avoiding duplicate processing of className. Of course, although it is not recommended to do so, you can still turn off this option, and after that, it will be up to you to handle or ignore unnecessary transformations.

Add the babel configuration

{
  "plugins": [
    [
      "clsx",
      {
        "static": false
      }
    ]
  ]
}

Your code

const className = ['c1', 'c2'];
<div className={className} />;

After compilation

import _clsx from 'clsx';
const className = ['c1', 'c2'];
<div className={_clsx(className)} />;

In an existing project, there may be a lot of code like this, and if you turn off static mode, there will be a lot of duplication.

Your code

import classNames from 'clsx';

// 👎 This will repeat the process
const className = classNames('c1', 'c2');
<div className={className} />;

// 👍 This does not repeat the process
<div className={classNames('c1', 'c2')} />;

After compilation

import _clsx from 'clsx';
import classNames from 'clsx';

// 👎 This will repeat the process
const className = classNames('c1', 'c2');
<div className={_clsx(className)} />;

// 👍 This does not repeat the process
<div className={classNames('c1', 'c2')} />;

options.strict

| Type | Default | | --------- | ------- | | boolean | true |

Strict mode is turned on by default, and you can turn it off if you want to add clsx to any attribute suffixed by className.

Add the babel configuration

{
  "plugins": [
    [
      "clsx",
      {
        "strict": false
      }
    ]
  ]
}

Your code

<Component
  className={['c1', 'c2']}
  headerClassName={['c1', 'c2']}
  footerClassName={['c1', 'c2']}
/>

After compilation

import _clsx from 'clsx';
<Component
  className={_clsx('c1', 'c2')}
  headerClassName={_clsx('c1', 'c2')}
  footerClassName={_clsx('c1', 'c2')}
/>;

options.importSource

| Type | Default | | -------- | -------- | | string | 'clsx' |

clsx is the supported library by default, and if you have your choice, you can replace it with importSource.

Add the babel configuration

{
  "plugins": [
    [
      "clsx",
      {
        "importSource": "classnames"
      }
    ]
  ]
}

Your code

<div className={['c1', 'c2']} />

After compilation

import _clsx from 'classnames';
<div className={_clsx('c1', 'c2')} />;

options.importName

| Type | Default | | -------- | ----------- | | string | 'default' |

If your custom import source does not have a default export available, you can specify the import name with importName.

Add the babel configuration

{
  "plugins": [
    [
      "clsx",
      {
        "importSource": "@/utils",
        "importName": "classNames"
      }
    ]
  ]
}

Your code

<div className={['c1', 'c2']} />

After compilation

import { classNames as _clsx } from '@/utils';
<div className={_clsx('c1', 'c2')} />;

Ignore

If you feel that there is an unnecessary transformation, you can add a comment so that it is ignored during the transformation.

Local ignore

You can ignore the conversion of this line by adding a comment above.

Your code

<div className={['c1', 'c2']} />;
<div
  // @clsx-ignore
  className={['c1', 'c2']}
/>;

After compilation

import _clsx from 'clsx';
<div className={_clsx('c1', 'c2')} />;
<div className={['c1', 'c2']} />;

Global ignore

You can omit the conversion of the entire file by adding a comment at the top of the file.

Your code

// @clsx-ignore-global
<div className={['c1', 'c2']} />;
<div className={['c1', 'c2']} />;

After compilation

<div className={['c1', 'c2']} />;
<div className={['c1', 'c2']} />;

Typescript

Support Typescript with jsxImportSource.

You only need to make minor changes to tsconfig.json to support the use of the plug-in in Typescript projects.

Only react17+ and Typescript4.7+ are supported due to the use of advanced syntax.

preserve

{
  "compilerOptions": {
    "jsx": "preserve",
    "jsxImportSource": "babel-plugin-clsx/jsx",
    "isolatedModules": true
  }
}

react-jsx

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "babel-plugin-clsx/jsx"
  }
}

react-jsxdev

{
  "compilerOptions": {
    "jsx": "react-jsxdev",
    "jsxImportSource": "babel-plugin-clsx/jsx"
  }
}

react-native

{
  "compilerOptions": {
    "jsx": "react-native",
    "jsxImportSource": "babel-plugin-clsx/jsx",
    "isolatedModules": true
  }
}

One thing to note is that "babel-plugin-clsx/jsx" only supports type inference, which prevents Typescript from throwing errors.

Examples