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

rehype-postcss

v0.2.1

Published

Run PostCSS through style nodes in rehype

Downloads

9

Readme

rehype-postcss

rehype plugin to process <style> nodes and elements containing a style attribute with PostCSS.

Contents

What is this?

This package is a unified (rehype) plugin to process <style> nodes and elements with a style attribute using PostCSS.

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that runs PostCSS on <style> elements and other elements that have a style attribute.

When should I use this?

In most cases running PostCSS with postcss-html syntax through CLI or with your build tool should be enough.

When it's not, this plugin helps you run PostCSS through nodes contextually, run on fragments etc.

Specifically, the plugin was written to apply CSS Modules in context of the parent element what would not be possible to do in another way.

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:

npm install rehype-postcss

In Deno with esm.sh:

import rehypePostCSS from 'https://esm.sh/[email protected]'

In browsers with esm.sh:

<script type="module">
  import rehypePostCSS from 'https://esm.sh/rehype-postcss@6?bundle'
</script>

Use

Say we have the following file example.html:

<style>
  a {
    display: flex;
  }
</style>

And our module example.js looks as follows:

import { read } from 'to-vfile'
import { rehype } from 'rehype'
import rehypePostCSS from 'rehype-postcss'

const file = await rehype()
  .data('settings', { fragment: true })
  .use(rehypePostCSS, {
    plugins: [
      autoprefixer({ overrideBrowserslist: ['ie >= 10'] }),
    ],
  })
  .process(await read('example.html'))

console.log(String(file))

Now, running node example.js yields:

<style>
  a {
    display: -ms-flexbox;
    display: flex;
  }
</style>

API

This package exports no identifiers. The default export is rehypePostCSS.

unified().use(rehypePostCSS[, options])

Runs PostCSS.

options

Configuration (optional).

options.plugins

A list of plugins to run with the PostCSS processor. This list is passed directly; the whole process can be imagined simply like in the example.

👉 Note: If options.plugins is not passed or is an empty array, the plugin would look for a PostCSS config in the current working directory.

If such a config could not be found, an error is thrown.

options.options

Processor options. The object would complement defaults and passed to the process call.

The initial default options are:

{
  from: 'path of the source file',
  map: false,
}

👉 Note: Source maps (map option) are turned off by default.

If you are going to use source maps, turn it on as well as pass to option to let the processor rewrite URLs properly. When the from option can be discovered from the file, there is no way to know where the result is going to be saved.

options.test

Test to define which elements should be processed. The test must be a function; if it's anything else, it is ignored. The default (no test) is to process all <style> elements and any element with a style attribute. If the option is passed, it will be filtering out from the list of selected nodes by the default test.

Types

This package is not typed with TypeScript. It can be though if you send a PR or when I have some extra time.

Compatibility

The project should be compatible with Node.js 12.20+, 14.14+, and 16.0+ but the compatibility is not tested.

Related

This plugin was inspired by posthtml-postcss.

License

MIT © Viktor Yakubiv