babel-plugin-cssta-stylename
Advanced tools
Comparing version 0.1.5 to 0.1.6
{ | ||
"name": "babel-plugin-cssta-stylename", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Transform css imports into cssta template components", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
55
utils.js
@@ -24,30 +24,39 @@ const fs = require('fs') | ||
const regex = new RegExp(`\\.${className}(?:[^\\w\\-]|$)`) | ||
const newAst = { | ||
let keyframes = [] | ||
const newAst = getNewAst(ast.stylesheet.rules.map(rule => { | ||
if (rule.type === 'keyframes') { | ||
keyframes.push(rule) | ||
} else { | ||
return matchRule(rule, regex) | ||
} | ||
}).filter(Boolean)) | ||
let matchingCss = css.stringify(newAst) || '' | ||
keyframes = keyframes.filter(({ name }) => ( | ||
name && new RegExp('[:,\\s]' + name + '[:,\\s]').test(matchingCss) | ||
)) | ||
if (keyframes.length > 0) { | ||
matchingCss += (matchingCss ? '\n' : '') + css.stringify(getNewAst(keyframes)) | ||
} | ||
return matchingCss | ||
} | ||
function getNewAst (rules) { | ||
return { | ||
type: 'stylesheet', | ||
stylesheet: { | ||
rules: [] | ||
rules: rules | ||
} | ||
} | ||
newAst.stylesheet.rules = ast.stylesheet.rules.map(rule => { | ||
if (rule.type === 'media') { | ||
const rules = rule.rules.map(rule => { | ||
if (rule.type === 'rule') { | ||
const selectors = rule.selectors.map(selector => { | ||
if (regex.test(selector)) return selector | ||
}).filter(Boolean) | ||
if (selectors.length > 0) return { ...rule, selectors } | ||
} | ||
}).filter(Boolean) | ||
if (rules.length > 0) return { ...rule, rules } | ||
} else if (rule.type === 'keyframes') { | ||
return rule | ||
} else if (rule.type === 'rule') { | ||
const selectors = rule.selectors.map(selector => { | ||
if (regex.test(selector)) return selector | ||
}).filter(Boolean) | ||
if (selectors.length > 0) return { ...rule, selectors } | ||
} | ||
}).filter(Boolean) | ||
} | ||
return css.stringify(newAst) | ||
function matchRule (rule, regex) { | ||
if (rule.rules) { | ||
const rules = rule.rules.map(rule => matchRule(rule, regex)).filter(Boolean) | ||
if (rules.length > 0) return { ...rule, rules } | ||
} else if (rule.selectors) { | ||
const selectors = rule.selectors.filter(selector => regex.test(selector)) | ||
if (selectors.length > 0) return { ...rule, selectors } | ||
} | ||
} | ||
@@ -54,0 +63,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14400
280