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

@ecomfe/class-names-loader

v2.0.0

Published

Webpack loader to transform style into classNames bindings

Downloads

129

Readme

class-names-loader

GitHub Workflow Status Coveralls github npm

Webpack loader to transform style into classNames bindings.

Inspired by itsmepetrov/classnames-loader and have a total written to introduce features and breaking changes like:

  1. Written in TypeScript.
  2. Support style-loader's esModule format only.
  3. Compatible both default export and named export.
  4. Drop support for IE < 9 to minimize output code.
  5. Produce ES module code to enable optimizations like module concatenation.

We will keep active maintain.

Install

npm install -D class-names-loader

Auto binding

This loader is to bind a CSS modules enabled style-loader output into a classnames compatible function, received class names are mapped to CSS modules transformed ones:

import c from './index.css';

// May renders as `<div class="title-0f2bd">
<div className={c('title')} />

This function is also a mapping object from raw class names to transformed ones, so c.title is identical to c('title').

Also it behaves like classnames and accept more complex arguments like c('title', {emphasis: props.isHeading}, props.className). Any class names not in CSS file are rended as is, c('some-class') returns "some-class".

Usage

A traditional webpack configuration looks like:

{
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    {
                        loader: 'class-names-loader',
                        options: {
                            // options
                        },
                    },
                    {
                        loader: 'style-loader',
                        modules: true,
                    },
                    {
                        loader: 'css-loader',
                        modules: true,
                    },
                ],
            },
        ],
    },
}

style-loader >= 2 enables esModule option by default, and it must be enabled when combine with class-names-loader.

Options

interface Options {
    // Custom classnames module path, by default it imports `classnames`
    classNamesModule?: string;
    // Use named import from style-loader
    namedImport?: boolean;
}

Custom classnames

By default we require classnames module to be installed and import it, you can also install a custom module and pass it's module path to classNameModule option.

This is especially useful when you create a custom build tool and want to encapsulate classnames inside this tool:

// Build tool with class-names-loader and classnames installed locally
{
    loader: require.resolve('class-names-loader'),
    options: {
        classNamesModule: require.resolve('classnames'),
    },
}

Named import

If you have namedExport option enabled in style-loader, you should also enable namedImport option to make it compatible.