# @ecomfe/class-names-loader

> Webpack loader to transform style into classNames bindings

Latest version **2.0.0** (published 2021-04-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install @ecomfe/class-names-loader
pnpm add @ecomfe/class-names-loader
yarn add @ecomfe/class-names-loader
bun add @ecomfe/class-names-loader
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2021-04-30 |
| First published | 2020-11-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Maintainers | jinzhan, leeight, otakustay, ecomfe-admin, ecomfe-core, errorrik, gdjinbo, justineo, luyuan, ksky521, lang |

## Links

- npm: https://www.npmjs.com/package/@ecomfe/class-names-loader
- Repository: https://github.com/ecomfe/class-names-loader
- Homepage: https://github.com/ecomfe/class-names-loader#readme
- Issues: https://github.com/ecomfe/class-names-loader/issues
- npm.io page: https://npm.io/package/@ecomfe/class-names-loader

## Dependencies (1)

- [loader-utils](https://npm.io/package/loader-utils.md) ^2.0.0

## Recent versions

- 2.0.0 (latest) — 2021-04-30
- 1.0.0 — 2020-11-15

## README

# class-names-loader

![GitHub Workflow Status](https://img.shields.io/github/workflow/status/ecomfe/class-names-loader/Node.js%20CI?style=flat-square)
![Coveralls github](https://img.shields.io/coveralls/github/ecomfe/class-names-loader?style=flat-square)
![npm](https://img.shields.io/npm/v/class-names-loader?style=flat-square)

Webpack loader to transform style into classNames bindings.

Inspired by [itsmepetrov/classnames-loader](https://github.com/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

```shell
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:

```js
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:

```js
{
    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

```ts
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:

```js
// 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.

---
_Source: https://npm.io/package/@ecomfe/class-names-loader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
