Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
micro-purify
Advanced tools
Readme
This is a zero-dependencies fork of purify-css.
Micro-purify is a function that takes content (HTML/JS/PHP/etc) and CSS, and returns only the used CSS.
Micro-Purify does not modify the original CSS files. You can write to a new file, like minification.
If your application is using a CSS framework, this is especially useful as many selectors are often unused.
Installation
npm i -D micro-purify
import purify from "micro-purify"
const purify = require("micro-purify")
let content = ""
let css = ""
let options = {
output: "filepath/output.css"
}
purify(content, css, options)
Statically analyzes your code to pick up which selectors are used.
But will it catch all of the cases?
button-active
<!-- html -->
<!-- class directly on element -->
<div class="button-active">click</div>
// javascript
// Anytime your class name is together in your files, it will find it.
$(button).addClass('button-active');
button-active
// Can detect if class is split.
var half = 'button-';
$(button).addClass(half + 'active');
// Can detect if class is joined.
var dynamicClass = ['button', 'active'].join('-');
$(button).addClass(dynamicClass);
// Can detect various more ways, including all Javascript frameworks.
// A React example.
var classes = classNames({
'button-active': this.state.buttonActive
});
return (
<button className={classes}>Submit</button>;
);
var content = '<button class="button-active"> Login </button>';
var css = '.button-active { color: green; } .unused-class { display: block; }';
console.log(purify(content, css));
logs out:
.button-active { color: green; }
var content = ['**/src/js/*.js', '**/src/html/*.html'];
var css = ['**/src/css/*.css'];
var options = {
// Will write purified CSS to this file.
output: './dist/purified.css'
};
purify(content, css, options);
var content = ['**/src/js/*.js', '**/src/html/*.html'];
var css = '.button-active { color: green; } .unused-class { display: block; }';
var options = {
output: './dist/purified.css'
};
purify(content, css, options);
// Three possible arguments.
purify(content, css, options);
content
argumentArray
or String
Array
of glob file patterns to the files to search through for used classes (HTML, JS, PHP, ERB, Templates, anything that uses CSS selectors).
String
of content to look at for used classes.
css
argumentArray
or String
Array
of glob file patterns to the CSS files you want to filter.
String
of CSS to purify.
options
argumentObject
whitelist
Array of selectors to always leave in. Ex. ['button-active', '*modal*']
this will leave any selector that includes modal
in it and selectors that match button-active
. (wrapping the string with *'s, leaves all selectors that include it)FAQs
Zero dependencies unused-CSS removal tool.
The npm package micro-purify receives a total of 1 weekly downloads. As such, micro-purify popularity was classified as not popular.
We found that micro-purify 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.