Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
future-proof the api, return { css } instead of just the css string
  • Loading branch information
leeoniya committed Mar 23, 2019
1 parent 2ac7aec commit de31f05
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -49,12 +49,12 @@ let css = `
}
`;

const whitelist = /\b(?:#foo|\.bar)\b/;
const whitelist = /#foo|\.bar/;

let dropped = new Set();

// returns a string
let cleanCSS = dropcss({
// returns { css }
let cleaned = dropcss({
html,
css,
shouldDrop: (sel) => {
Expand All @@ -67,7 +67,7 @@ let cleanCSS = dropcss({
},
});

console.log(cleanCSS);
console.log(cleaned.css);

console.log(dropped);
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "dropcss",
"version": "0.2.1",
"version": "0.3.0",
"description": "Unused CSS Cleaner",
"main": "./src/dropcss.js",
"scripts": {
Expand Down
16 changes: 9 additions & 7 deletions src/dropcss.js
@@ -1,5 +1,5 @@
const { parse } = require('node-html-parser');
const csstree = require('css-tree');
const CSSTree = require('css-tree');
const CSSselect = require("css-select");
const adapter = require("./adapter");

Expand All @@ -11,15 +11,15 @@ const doDrop = sel => true;
function dropcss(opts) {
const htmlAst = parse(opts.html);

const cssAst = csstree.parse(opts.css, {
const cssAst = CSSTree.parse(opts.css, {
parseRulePrelude: false,
parseValue: false,
parseAtrulePrelude: false
});

const shouldDrop = opts.shouldDrop || doDrop;

csstree.walk(cssAst, {
CSSTree.walk(cssAst, {
visit: 'Rule',
enter: (node, item, list) => {
var pre = [];
Expand Down Expand Up @@ -47,11 +47,13 @@ function dropcss(opts) {
}
});

return (
csstree.generate(cssAst)
let cleaned = CSSTree.generate(cssAst)
// hack to remove leftover/empty @media queries until i can figure out how to prune them from cssAst
.replace(/@media\s*\([^\)]+\)\s*\{\}/gm, '')
);
.replace(/@media\s*\([^\)]+\)\s*\{\}/gm, '');

return {
css: cleaned
};
}

module.exports = dropcss;

0 comments on commit de31f05

Please sign in to comment.