
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
extract-css-media
Advanced tools
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.
npm i extract-css-media
The module exports a function which accepts to parameters: the input css string and an options object.
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.
The parameter gets passed to css.parse. When set to true, the output css gets minified.
This option can be used, if the splitted stylesheets should get processed further.
The source parameter gets passed to css.parse and is used to show the filename, in which a potential parsing errors occurred.
If the execution was successful, the following object gets returned:
{ mediaQuery1: CSS-string or AST mediaQuery2: CSS-string or AST, all: CSS-string or AST of the rules without media queries }
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;}"
}
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.
FAQs
npm module to separate media queries from a css string
We found that extract-css-media demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.