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-js-syntax

v1.0.7

Published

For use powerful JavaScript syntax for compile CSS instead legacy preprocessors: SASS, LESS, Stylus, etc.

Downloads

25

Readme

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Rationale

It turned out that no one CSS preprocessor is able to provide modularity of styles at the development level.

The convenience of preprocessors in the details is completely leveled when the application grows, because the preprocessors cannot provide adequate modularity.

When I have such a powerful tool at hand as JavaScript - it would not be wise not to use it.

Styles written in JavaScript are not so convenient, but when extending even a small project, the power of the JavaScript language fully compensated for this drawback.

Demo

Converter between Postcss-Js-Syntax and CSS

Description

You can use your JS modules for compile CSS files.

CSS styles in JavaScript format represent an simple object:

my-style.js

module.exports = [
    "@at-rule-wiwhout-params", // starts with @
    "@at-rule with params",
    "@at-rule (with params)",
    "// comment", // starts with "//" or "/*"
    {
        ".selector1": {
            color: "#0f0"
        },
        ".selector2": {
            color: "#0f0"
        }
    },
    "// another comment",
    {
        "@at-rule (with params)": {
            "and-content": "value"
        },
        ".selector3": {
            color: "#0f0",
            
            ".sub-selector": { // you can use postcss-nested plugin here
                content: '"quotes is required for this CSS property"'
            }
        }
    }
]

One possible use case:

import postcss from 'postcss'
import jsSyntax from 'postcss-js-syntax'

const css = postcss([...your plugins])
    .process(
        '', // this parameter will be ignored by js parser
        {
            from: require.resolve('./myJsStyle.js'), // required absolute path to real file
            parser: jsSyntax.parser,
            requireFromString: function(code, filename) {
            	// you can provide your own requireFromString function
            	
            	// default:
            	return require(filename)
            }
        }

console.log(css)

I recommend to use require-from-memory npm module in requireFromString option, because it allows you to use ES6 modules with babel like this:

import myModule './my-module.js'
import myNodeModule 'my-node-module'

export default [
    ...myModule.baseStyles({fontSize: "5px"}), // mixin equivalent
    {
    	...myNodeModule?.myCssClasses, // see @babel/plugin-proposal-optional-chaining
    	.override-selector {
    		color: myNodeModule?.colors?.primary, // if color == null it will not be added to CSS
    		content: myNodeModule.myContent?.surroundWithQuotes()
    	}
    }
]

License

CC0-1.0