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

postcss-node

v2.0.2

Published

Package which allows import of css/scss/lass/.. files under the node.js eg `require('styles.css')`

Downloads

25,316

Readme

postcss-node

Package which allows import of css files under the node.js eg require('styles.css') This package should be used together with postcss-es-modules

Overview

By using require.extensions this package register the handlers which on the fly, on runtime, for each .css file require is transpiling the css content by using postcss. To be able to load the .css file as javascript module, you should use postcss-es-modules post css plugin.

Installation

npm i postcss-node postcss-es-modules

Usage

Firstly we need to provide the proper postcss configuration, you can add it to your package.json, or keep it in separate file eg:

/* postcss.config.js */
module.exports = {
    "plugins": {
        // this plugin will transform css into the js module
        "postcss-es-modules": {
            "inject": {
                // we will use the common js modules
                "moduleType": "cjs"
            }
        }
    }
}

Usage on the cli

You can register the .css/.sass files handle just by the -r node.js option.

node -r postcss-node/register test.js
/* test.js */
const { styles, css } = require('./test.css');
console.log(css);
console.log(styles.a);
/* test.css */
.a {
    color: blue;
} 

Usage within the code

Or you can register the handle manually within the code.

node test.js
/* test.js */
const { register } = require('postcss-node');
register();
const { styles, css } = require('./test.css');
console.log(css);
console.log(styles.a);
/* test.css */
.a {
    color: blue;
} 

Custom files extensions

The register function is able to take the optional parameter, which will be the array of the file extensions which should be handled by that package. By default, register is attaching handlers for the '.css', '.scss', '.sass', '.less', '.stylus'.

node test.js
/* test.js */
const { register } = require('postcss-node');
register(['.acss', '.bcss', '.css']);
const { styles, css } = require('./test.acss');
console.log(css);
console.log(styles.a);
/* test.acss */
.a {
    color: blue;
} 

You can also provide the custom extensions by the POSTCSS_NODE_EXT environment variable like: cross-env POSTCSS_NODE_EXT=.acss,.bcss,.css

Other options

The register function is able to take the two more optional parameter

  • timeout (POSTCSS_NODE_TIMEOUT) - the timeout of css processing, default 60000 ms
  • bufferSize (POSTCSS_NODE_BUFFER_SIZE) - the size of buffer used to transfer compiled code from the worker, in case of RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: "length" is outside of buffer bounds error please increase this parameter, default 1024 kB

Need a help ?

If you have any problems, issues, ect. please use github discussions.