Socket
Socket
Sign inDemoInstall

postcss-js

Package Overview
Dependencies
4
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    postcss-js

PostCSS for React Inline Styles, Radium, Free Style and other CSS-in-JS


Version published
Maintainers
1
Install size
1.46 MB
Created

Package description

What is postcss-js?

The postcss-js package allows you to use PostCSS, a tool for transforming CSS with JavaScript, in a JavaScript environment. This enables the processing of CSS styles with JavaScript objects, facilitating dynamic style generation and manipulation within JavaScript projects.

What are postcss-js's main functionalities?

Parsing CSS to JS Objects

This feature allows the conversion of CSS styles into JavaScript objects, making it easier to manipulate CSS properties and values programmatically.

const postcssJs = require('postcss-js');
const autoprefixer = require('autoprefixer');
const root = postcssJs.parse({ color: 'red' });
console.log(root);

Applying PostCSS Plugins

Enables the use of PostCSS plugins, such as autoprefixer, directly on JavaScript objects representing CSS styles, allowing for advanced CSS processing and manipulation.

const postcssJs = require('postcss-js');
const autoprefixer = require('autoprefixer');
const prefixer = postcssJs.sync([ autoprefixer ]); 
const prefixed = prefixer({ display: 'flex' });
console.log(prefixed);

Other packages similar to postcss-js

Changelog

Source

2.0.0

  • Remove Node.js 9 and Node.js 4 support.
  • Use PostCSS 7.0.

Readme

Source

PostCSS JS Build Status

PostCSS for React Inline Styles, Radium, JSS and other CSS-in-JS.

For example, to use Stylelint, RTLCSS or postcss-write-svg plugins in your workflow.

Usage

Installation

npm i postcss-js

Processing

const postcssJs = require('postcss-js')
const autoprefixer = require('autoprefixer')

const prefixer = postcssJs.sync([ autoprefixer ]);

const style = prefixer({
    display: 'flex'
});

style //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }

Compile CSS-in-JS to CSS

const postcss = require('postcss')
const postcssJs = require('postcss-js')

const style = {
    top: 10,
    '&:hover': {
        top: 5
    }
};

postcss().process(style, { parser: postcssJs }).then( (result) => {
    result.css //=> top: 10px;
               //   &:hover { top: 5px; }
})

Compile CSS to CSS-in-JS

const postcss = require('postcss')
const postcssJs = require('postcss-js')

const css  = '@media screen { z-index: 1 }'
const root = postcss.parse(css);

postcssJs.objectify(root) //=> { '@media screen': { zIndex: '1' } }

API

sync(plugins): function

Create PostCSS processor with simple API, but with only sync PostCSS plugins support.

Processor is just a function, which takes one style object and return other.

async(plugins): function

Same as sync, but also support async plugins.

Returned processor will return Promise.

parse(obj): Root

Parse CSS-in-JS style object to PostCSS Root instance.

It converts numbers to pixels and parses [Free Style] like selectors and at-rules:

{
    '@media screen': {
        '&:hover': {
            top: 10
        }
    }
}

This methods use Custom Syntax name convention, so you can use it like this:

postcss().process(obj, { parser: postcssJs })

objectify(root): object

Convert PostCSS Root instance to CSS-in-JS style object.

Troubleshoot

Webpack may need some extra config for some PostCSS plugins.

Module parse failed

Autoprefixer and some other plugins need a json-loader to import data.

So, please install this loader and add to webpack config:

loaders: [
    {
        test: /\.json$/,
        loader: "json-loader"
    }
]

Keywords

FAQs

Last updated on 16 Jul 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc