Git Product home page Git Product logo

class-names-loader's Introduction

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 @ecomfe/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: '@ecomfe/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('@ecomfe/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.

class-names-loader's People

Contributors

otakustay avatar thornwu avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

thornwu janlaywss

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.