purgecss-whitelister
Advanced tools
Comparing version 2.0.1 to 2.1.0
30
index.js
const { parse } = require('scss-parser') // https://github.com/salesforce-ux/scss-parser | ||
const parse2 = require('gonzales-pe').parse | ||
const { readFileSync } = require('fs') | ||
@@ -7,2 +8,3 @@ const globAll = require('glob-all') | ||
const shouldKeep = ['id', 'class', 'attribute'] | ||
const shouldKeep2 = ['id', 'class', 'attribute', 'typeSelector'] | ||
const exts = ['css', 'sass', 'scss', 'less'] | ||
@@ -20,6 +22,28 @@ | ||
// File contents. | ||
const fileContents = readFileSync(filename, 'utf-8') | ||
const parsedData = parse(fileContents).value | ||
const selectors = parseStyleAST(parsedData) | ||
return acc.concat(selectors) | ||
// 1st try `scss-parser`. | ||
try { | ||
// throw 'nope' | ||
const parsedData = parse(fileContents).value | ||
const selectors = parseStyleAST(parsedData) | ||
return acc.concat(selectors) | ||
// 2nd try `gonzales-pe`. | ||
} catch(e) { | ||
const parsed = parse2(fileContents, { syntax: ext }) | ||
const nodes = [] | ||
// Built-in traversal method, no need to recursively | ||
// traverse the tree, cherry pick, and flatten the results! | ||
parsed.traverse(node => { | ||
if (shouldKeep2.includes(node.type)) { | ||
const thing = node.content.find(({ type }) => type === 'ident') | ||
if (thing) nodes.push(thing.content) | ||
} | ||
}) | ||
return [...acc, ...new Set(nodes)] | ||
} | ||
}, []) | ||
@@ -26,0 +50,0 @@ |
{ | ||
"name": "purgecss-whitelister", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "A utility for creating whitelists of CSS selectors for use with Purgecss.", | ||
@@ -17,4 +17,5 @@ "main": "index.js", | ||
"glob-all": "^3.1.0", | ||
"gonzales-pe": "^4.2.3", | ||
"scss-parser": "^1.0.0" | ||
} | ||
} |
7603
91
3
+ Addedgonzales-pe@^4.2.3
+ Addedgonzales-pe@4.3.0(transitive)
+ Addedminimist@1.2.8(transitive)