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

klass-loader

v1.1.4

Published

![](https://i.imgur.com/S0v8XBG.png)

Downloads

6

Readme

Travis GitHub code size in bytes

klass-loader is an improvement to CSS workflow and encapsulation in React. It enforces an opinionated, yet simple and non-obtrusive structure of presentation components and corresponding stylesheets. klass-loader utilises css-modules to scope style rules and gives warnings when attempting to use a non-existent rule.

Usage

Once the loader is correctly installed, there is no need to import any libraries or reference CSS files. klass attributes are evaluated as vanilla CSS selector strings, similar to the class tag in HTML. This attribute can be treated as a language feature in the same way class was pre modern Javascript. Usage is as follows:

Component.jsx

import React from 'react';

let variableName = 'child';

const Component = (props) => {
  return (
    <div klass='component'>
      <div klass={variableName}>
        Hello!
      </div>
    </div>
  );
}

export default Component;

styles.css

.component {
  padding: 10px;
}

.child {
  background-color: red; 
}

This minimal setup will apply classes in a corresponding file called styles.css in the same directory as the component. (LESS might work too, but it's untested so far). An example hierarchy is shown here:

src/
└── App.js
    ├── NoStyleButton/
    │   └── NoStyleButton.js
    ├── Progress/
    │   ├── Progress.js
    │   └── styles.css
    └── Question/
        ├── Question.js
        ├── Answer.js
        └── styles.css

A component requires a corresponding stylesheet in the same directory only if the component declares a klass attribute as shown above. If there are multiple components in the same folder, they will all be based on the same stylesheet.

Using global styles

In the styles.css file, the webpack implementation of css-modules allows us compose selectors, import selectors from other stylesheets, and more! Further info on their README. Alternatively, className can still be used alongside klass to reference global CSS files.

Install & Configuration

Install

npm i --save klass-loader

Configuration

Set up and install style-loader and css-loader for your CSS files (and less-loader too if you use less):

require.resolve('style-loader'),
{
  loader: require.resolve('css-loader'),
  options: {
    importLoaders: 1,
    modules: true,
    localIdentName: "[name]__[local]___[hash:base64:5]",  
  },
}, // TODO: Update example to use newer webpack syntax

Make sure to set the localIdentName to something like the above to enable encapsulation. More details on how to install can be found on their README.

Next, set up klass-loader itself. To do this, place the loader just before babel-loader in the webpack config:

{
  test: /\.(js|jsx|mjs)$/,
  include: paths.appSrc,
  use: [
    // other loaders
    {
      loader: require.resolve('klass-loader'),
    },
    {
      loader: require.resolve('babel-loader'),
      options: {
        // babel options
      },
    },
  ]
},

Example repo

An example of a project using klass-loader can be found here.