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

extract-css-media

v0.1.2

Published

npm module to separate media queries from a css string

Downloads

753

Readme

extract-css-media

Provides a function, which parses a given css string and extracts the media query definitions into separate css strings (or ASTs, if configured).

It uses the css module, to parse and compile the input css string.

Installation

npm i extract-css-media

API

The module exports a function which accepts to parameters: the input css string and an options object.

Options

minRules: Number - default: 20

Specifies a minimum number of rules for a media query to be required to split that media query definitions into an extra string.

The background for that is, that there are sometimes media queries which contain only a little amount of definitions. In that case it often makes sense to keep them in the overall css.

compress: Boolean - default: true

The parameter gets passed to css.parse. When set to true, the output css gets minified.

asAst: Boolean - default: false

This option can be used, if the splitted stylesheets should get processed further.

source: String - default: undefined

The source parameter gets passed to css.parse and is used to show the filename, in which a potential parsing errors occurred.

Return: Object

If the execution was successful, the following object gets returned:

Example

const extractMedia = require('extract-css-media')

const extracted = extractMedia(`
    body { 
        color: red 
    } 
    
    @media screen and (min-width: 300px) {
        body {
            color: green
         }
    }`, {
        minRules: 1
    }
)

console.log(JSON.stringify(extracted, null, 4));

prints

{
    "screen and (min-width: 300px)": "body{color:green;}",
    "all": "body{color:red;}"
}

Note

The compress option doesn't produce a fully minimized css. Like you can already see in the example output, the splitting semicolon for css definitions is also added to the last statement of each definition. If you really want to have the smallest css, you should use a real minifier after the media query separation.