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

premodules-loader

v0.0.6

Published

Share css-modules compiled class names over pre-processers

Downloads

18

Readme

premodules-loader

npm Travis

Share css-modules compiled class names over pre-processers!

Install

npm i premodules-loader --save-dev

What's the problem

css-modules is a great workaround to generate scoped css. However since scoped css are tightly bound with js files that import them, when you are trying to write a reusable component, you're possibly getting a headache - how can other developers override the default styles in my components?

So,

What if we can import class name maps into sass/less/stylus code?

How it works

As is explained in this article, this loader executed twice in the webpack workflow by the following order:

  1. Create directives:

  2. Use some directives to interpolate class names into source code - surely it should be converted to sass/less/stylus hashes.

  3. Use variable interpolation syntax to define class name, e.g. ${map-get($comp, 'main')} in sass.

  4. Process css-modules output:

  5. Restore twice-transformed class names to the previous one (both in stylesheet and class map).

  6. Save the class map for interpolation.

workflow

Usage

Webpack config (execute twice):

// webpack.config.js
module.exports = {
  ...
  module: {
    loaders: [{
      test: /\.scss/,
      loaders: [
        'style',
        'premodules?restore', // restore here!
        'css?modules&importLoaders=1&localIdentName=[path]_[name]_[local]_[hash:base64:5]',
        'postcss',
        'sass',
        'premodules?parse', // parse here!
      ]
    }]
  },
  premodules: {
    transformer(varName, hash) {
      // custom code transformer
    }
  }
};

Components define:

// button.js
import React from 'react';
import styles from './button.scss';

const Button = () => (
  <button className={style.button}>click me</button>
);

export default Button;
// button.scss
.button {
  border-radius: 3px;
}

Your react app:

// app.js
import React from 'react';
import './app.scss';

const App = () => (
  <Button />
);
// app.scss
@module './button.scss' => $comp;

#{map-get($comp, 'button')} {
  background: #123;
}

Directive Syntax

// import node_modules
@module 'module_name/dir/file' => $var;

// or use relative/absolute path
@module '/path/of/module' => $var;

Customize Transformer

Default transform is designed for scss code transformation, write your own transformer to support your pre-processor.

Transformer function receives two parameters:

  1. varName:string: variable name defined in directive
  2. hash:object: css-modules exported class name map

A typical transformer may like this:

// scss transformer
// `@module './comp.scss' => $comp` -> `$comp: ( key: value, ... )`

const transformer = (varName, hash) => {
  if (!hash) return '';

  const main = Object.keys(hash).map(key => `${key}: '.${hash[key]}'`).join(',');

  return `${varName}: (${main});`;
};

Limitations

  1. Must import all stylesheets you need into JavaScript code, otherwise it won't be passed through webpack.

  2. Importing files that declared with @module directives in your sass/less/stylus codes may cause breaks (pre-processors won't understand this directive).

License

MIT.