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-classname-for-css-module

v0.0.1

Published

A babel plugin lets you use css modules while writing string classnames directly

Downloads

4

Readme

babel-plugin-classname-for-css-module

English | 简体中文

✨ Features

This plugin allows you to write string classnames directly in jsx, and then the classnames will automatically adapt to the css module.

As follow:

import './style1.module.css';
import './style2.module.css';
import './style3.module.css' /* test1 */;
import './style4.module.css' /* test2 */;

function Test() {
  return;
  <>
    <div className="test"></div>
    <div className="test1 test2"></div>
    <div className="test1.a test2.b"></div>
    <div className={`${Math.random() > 0.5 ? 'a' : 'b'}`}></div>
    <div className={(function (a, b) {})('a', 'b')}></div>
    <div className={((a, b) => {})('a', 'b')}></div>
    <div className={fn('a', 'b')}></div>
    <div className={[].map(() => {})}></div>
    <div className={Math.random() > 0.5 ? 'a' : 'b'}></div>
    <div className={['a', 'b']}></div>
    <div className={(a = 'a')}></div>
    <div className={'a' + 'b'}></div>
    <div className={'a'}></div>
    <div className={function () {}}></div>
    <div className={() => {}}></div>
    <div className={class {}}></div>
    <div className={a}></div>
    <div className={this}></div>
  </>;
}

will be transform to:

import _gcn from 'babel-plugin-classname-for-css-module/runtime';
import _style from './style1.module.css';
import _style2 from './style2.module.css';
import _test from './style3.module.css';
import _test2 from './style4.module.css';
const _sym = {
  style1: '_style',
  _style,
  style2: '_style2',
  _style2,
  test1: '_test',
  _test,
  test2: '_test2',
  _test2,
};
function Test() {
  return;
  <>
    <div className={_gcn('test', _sym)}></div>
    <div className={_gcn('test1 test2', _sym)}></div>
    <div className={_gcn('test1.a test2.b', _sym)}></div>
    <div className={_gcn(`${Math.random() > 0.5 ? 'a' : 'b'}`, _sym)}></div>
    <div className={_gcn((function (a, b) {})('a', 'b'), _sym)}></div>
    <div className={_gcn(((a, b) => {})('a', 'b'), _sym)}></div>
    <div className={_gcn(fn('a', 'b'), _sym)}></div>
    <div
      className={_gcn(
        [].map(() => {}),
        _sym,
      )}
    ></div>
    <div className={_gcn(Math.random() > 0.5 ? 'a' : 'b', _sym)}></div>
    <div className={_gcn(['a', 'b'], _sym)}></div>
    <div className={_gcn((a = 'a'), _sym)}></div>
    <div className={_gcn('a' + 'b', _sym)}></div>
    <div className={_gcn('a', _sym)}></div>
    <div className={_gcn(function () {}, _sym)}></div>
    <div className={_gcn(() => {}, _sym)}></div>
    <div className={_gcn(class {}, _sym)}></div>
    <div className={_gcn(a, _sym)}></div>
    <div className={_gcn(this, _sym)}></div>
  </>;
}

_gcn is a runtime method, so you don't need to explicitly import it. It will help you deal with class names that are only available at runtime, and if multiple stylesheets have been imported, it will also correctly help you deal with correspondence between class names.

like this:

<div className="App test"></div>

will be transform to:

// This is just an example; the actual class names will be determined by your css
<div className="_App_ahvyq_8 _test_ahvyq_46"></div>

📦 Install

npm i babel-plugin-classname-for-css-module -D
yarn add babel-plugin-classname-for-css-module -D
pnpm i babel-plugin-classname-for-css-module -D

🔨 Usage

  • first you need to apply the plugin.
// babel.config.js
{
  "plugins": ["babel-plugin-classname-for-css-module"]
}
  • then you just need to include the css module in the js file where you want to use it.
import './style.module.css';
  • you can do this if you have multiple stylesheets to import. (note: the introduction of the CSS file specification must be XXX.module.(css|less|sass|scss))
import './style3.module.css';
import './style4.module.css';

function App() {
  return (
    <div className="style3.xxx style4.xxx"></div>;
  );
}

export default App;
  • you can also use comments to decide which class names to import.(without comments, the imported class is named xxx.module => xxx)
import './style3.module.less' /* test1 */;
import './style4.module.sass' /* test2 */;

function App() {
  return (
    <div className="test1.xxx test2.xxx"></div>;
  );
}

export default App;
  • If you are using less or sass syntax (imported styles will be automatically smoothed if there are nested rules)

df9bfe66400aba21ec64a29689fc54a.png

// style3.module.less
.a {
  display: flex;

  &-child {
    display: flex;
  }
}
// style4.module.sass
.b
  display: flex
  &-child
    display: flex
    
// App.jsx
import './style3.module.less' /* test1 */;
import './style4.module.sass' /* test2 */;

function App() {
  return (
    <>
      <div className="test1.a">
        <div className="test1.a-child"></div>
      </div>
      <div className="test2.b">
        <div className="test2.b-child"></div>
      </div>
    </>
  );
}

export default App;