css-sniper
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -15,4 +15,4 @@ const path = require('path'); | ||
module.exports = { | ||
resolver: function(path) { | ||
return path; | ||
resolver: function(base) { | ||
return path.join(process.cwd(), base); | ||
} | ||
@@ -19,0 +19,0 @@ }; |
122
importer.js
@@ -43,3 +43,3 @@ const fs = require('fs'); | ||
* Syntax: @import "@seven/css/base/elements.css remove { body, | ||
* .thunder-details, .apple { color } }"; | ||
* .thunder-details, .apple { color; font-size } }"; | ||
* | ||
@@ -55,5 +55,4 @@ */ | ||
if (selectorMatches) { | ||
let definition = selectorMatches[1].match(/.+?}/g).map( | ||
getSelectorAndDeclarations | ||
); | ||
// Parse import string into ast. | ||
let definition = csstree.parse(selectorMatches[1]); | ||
@@ -67,21 +66,7 @@ return [ definition, fileBase, filePath ]; | ||
function getSelectorAndDeclarations(string) { | ||
let [selector, declarations] = string.split(/[{}]/); | ||
selector = csstree.translate(csstree.parse(selector, { context: 'selectorList' })); | ||
if (declarations) { | ||
declarations = declarations.split(',').map(val => val.trim()); | ||
} | ||
else { | ||
declarations = []; | ||
} | ||
return { selector: selector, declarations: declarations }; | ||
} | ||
/** | ||
* | ||
*/ | ||
function parseFile(file, definitions){ | ||
function parseFile(file, definition){ | ||
let contents = fs.readFileSync(file, 'utf-8'); | ||
@@ -98,4 +83,26 @@ | ||
// Use walkRulesRight: added rules are not parsed. | ||
csstree.walkRulesRight(ast, function(rule, item, list) { | ||
removeSelectors(ast, definition); | ||
return csstree.generate(ast); | ||
} | ||
function removeSelectors(ast, definition) { | ||
csstree.walk(ast, function (rule, item, list) { | ||
// Handle removal of @-rules. | ||
if (rule.type === 'Atrule') { | ||
let atRule = rule; | ||
csstree.walk(definition, function (defRule) { | ||
if ( defRule.type !== 'Atrule' && defRule.type !== 'Rule') { | ||
return; | ||
} | ||
// Remove the whole rule when there are no selectors. | ||
if (defRule.block.children.getSize() === 0) { | ||
if (checkAtruleIsSame(atRule, defRule)) { | ||
list.remove(item); | ||
} | ||
} | ||
}); | ||
} | ||
// Ignore all other types. | ||
@@ -107,17 +114,28 @@ if (rule.type !== 'Rule') { | ||
// Render selector from ast for comparison. | ||
let selector = csstree.translate(rule.prelude) | ||
let definition = definitions.find(function (def) { | ||
return def.selector === selector; | ||
}); | ||
let selector = csstree.generate(rule.prelude); | ||
// this.atrule corresponds to the stylesheet context. | ||
let atRule = this.atrule; | ||
if (definition) { | ||
if (definition.declarations.length) { | ||
removeDeclarations(rule, item, list, definition.declarations); | ||
// Context changes to the definition tree here. | ||
csstree.walk(definition, function (defRule) { | ||
if ( defRule.type !== 'Atrule' && defRule.type !== 'Rule') { | ||
return; | ||
} | ||
else { | ||
list.remove(item); | ||
let defSelector = csstree.generate(defRule.prelude); | ||
if (selector === defSelector) { | ||
if (checkAtruleIsSame(atRule, this.atrule)) { | ||
// Remove declarations or the whole rule. | ||
if (defRule.block.children.getSize()) { | ||
removeDeclarations(rule, item, list, defRule.block.children); | ||
} | ||
else { | ||
list.remove(item); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
// Remove rule if there is no declaration. | ||
// Remove rule if there are no declarations. | ||
if (rule.block.children.isEmpty()) { | ||
@@ -127,18 +145,46 @@ list.remove(item); | ||
}); | ||
return csstree.translate(ast); | ||
} | ||
function removeDeclarations(rule, item, list, declarations) { | ||
function removeDeclarations(rule, ruleitem, rulelist, declarations) { | ||
let warn = false; | ||
// Traverse csstree Lists | ||
rule.block.children.each(function(node, item, list) { | ||
if (node.type === 'Declaration') { | ||
if (declarations.includes(node.property)) { | ||
list.remove(item); | ||
} | ||
declarations.each(function(currentNode) { | ||
if (node.property === currentNode.value.replace(/;/g , '')) { | ||
list.remove(item); | ||
} | ||
else if (currentNode.type === 'Raw') { | ||
// Handle comma separated declarations. | ||
currentNode.value.split(',').forEach(function (value) { | ||
if (node.property === value.trim()) { | ||
list.remove(item); | ||
warn = true; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
if (warn) console.log('WARNING: Use of commas in remove definition is deprecated and will be removed, use semi-colon instead.'); | ||
} | ||
function checkAtruleIsSame(atRule, defAtRule) { | ||
if (!atRule && !defAtRule) { | ||
// no @-rule | ||
return true; | ||
// Check if css rule is also in @-rule and compare type. e.g. @media | ||
} else if (atRule && defAtRule && atRule.name === defAtRule.name) { | ||
let prelude = csstree.generate(atRule.prelude); | ||
let defPrelude = csstree.generate(defAtRule.prelude); | ||
// Compare selector, e.g. screen and width() | ||
if (prelude === defPrelude) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
module.exports = { | ||
sniperImporter, | ||
}; |
{ | ||
"name": "css-sniper", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A node module and sass compiler, which lets you remove styles and selectors, while importing external CSS files.", | ||
@@ -23,6 +23,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"commander": "^2.11.0", | ||
"css-tree": "^1.0.0-alpha.26", | ||
"node-sass": "^4.6.1" | ||
"commander": "^2.14.1", | ||
"css-tree": "^1.0.0-alpha.28", | ||
"node-sass": "^4.7.2" | ||
} | ||
} |
# css-sniper | ||
A node module and sass compiler, which lets you remove styles and selectors, while importing external CSS files. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
10826
12
342
4
0
Updatedcommander@^2.14.1
Updatedcss-tree@^1.0.0-alpha.28
Updatednode-sass@^4.7.2