postcss-scopify
Advanced tools
Comparing version 0.1.10 to 1.0.0
130
index.js
'use strict'; | ||
var postcss = require('postcss'); | ||
var selectorParser = require('postcss-selector-parser'); | ||
var conditionalGroupRules = ['media','supports','document']; | ||
const conditionalGroupRules = ['media','supports','document']; | ||
module.exports = postcss.plugin('postcss-scopify', scopify); | ||
function scopify(scope, options) { | ||
options = options || {}; | ||
// special case for the '&' selector, resolves to scope | ||
var processor = selectorParser(function (selectors) { | ||
var hasNestingSelector = false; | ||
selectors.walkNesting(function (selector) { | ||
hasNestingSelector = true; | ||
selector.replaceWith( | ||
selectorParser.string({value: scope}) | ||
); | ||
}); | ||
if (!hasNestingSelector) { | ||
selectors.first.prepend( | ||
selectorParser.string({value: scope + ' '}) | ||
); | ||
} | ||
}); | ||
return function(root) { | ||
// guard statment- allow only valid scopes | ||
if(!isValidScope(scope)){ | ||
throw root.error('invalid scope', { plugin: 'postcss-scopify' }); | ||
} | ||
root.walkRules(function (rule) { | ||
// skip scoping of special rules (certain At-rules, nested, etc') | ||
if(!isRuleScopable(rule)){ | ||
return rule; | ||
} | ||
rule.selectors = rule.selectors.map(function(selector) { | ||
if (isScopeApplied(selector,scope)) { | ||
return selector; | ||
} | ||
return processor.processSync(selector); | ||
}); | ||
}); | ||
}; | ||
const isObject = item => { | ||
return item != null && typeof item === 'object' && Array.isArray(item) === false; | ||
} | ||
@@ -61,4 +15,4 @@ | ||
*/ | ||
function isScopeApplied(selector,scope) { | ||
var selectorTopScope = selector.split(" ",1)[0]; | ||
const isScopeApplied = (selector, scope) => { | ||
const selectorTopScope = selector.split(" ",1)[0]; | ||
return selectorTopScope === scope; | ||
@@ -72,10 +26,7 @@ } | ||
*/ | ||
function isValidScope(scope) { | ||
if (scope){ | ||
return scope.indexOf(',') === -1; | ||
} | ||
else{ | ||
const isValidScope = scope => { | ||
if (!scope){ | ||
return false; | ||
} | ||
return scope.indexOf(',') === -1; | ||
} | ||
@@ -86,19 +37,62 @@ | ||
* | ||
* @param {rule} rule | ||
* @param {Rule} rule | ||
*/ | ||
function isRuleScopable(rule){ | ||
const isRuleScopable = rule => { | ||
if(rule.parent.type !== 'root') { | ||
if (rule.parent.type === 'atrule' && conditionalGroupRules.indexOf(rule.parent.name) > -1){ | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
return rule.parent.type === 'atrule' && conditionalGroupRules.indexOf(rule.parent.name) > -1; | ||
} else { | ||
return true; | ||
} | ||
} | ||
else { | ||
return true; | ||
/** | ||
* extract the scope from the input given by caller | ||
* | ||
* @param {string | Record<string, string>} options | ||
*/ | ||
const extractScope = (options) => { | ||
if (typeof options === 'string') { | ||
return options; | ||
} else if (isObject(options) && options.scope) { | ||
return options.scope | ||
} | ||
return null; | ||
} | ||
const scopify = (options) => { | ||
return { | ||
postcssPlugin: 'postcss-scopify', | ||
Once (root, { result }) { | ||
const scope = extractScope(options); | ||
// guard statment- allow only valid scopes | ||
if(!isValidScope(scope)){ | ||
throw root.error('invalid scope', { plugin: 'postcss-scopify' }); | ||
} | ||
root.walkRules(rule => { | ||
// skip scoping of special rules (certain At-rules, nested, etc') | ||
if(!isRuleScopable(rule)){ | ||
return rule; | ||
} | ||
rule.selectors = rule.selectors.map(selector => { | ||
if (isScopeApplied(selector,scope)) { | ||
return selector; | ||
} | ||
// special case for a top level '&' selector, resolves to scope | ||
if (selector.includes('&')) { | ||
return selector.replace(/&/g, scope); | ||
} | ||
return scope + ' ' + selector; | ||
}); | ||
}); | ||
}, | ||
}; | ||
} | ||
module.exports = scopify; | ||
module.exports.postcss = true; |
{ | ||
"name": "postcss-scopify", | ||
"version": "0.1.10", | ||
"version": "1.0.0", | ||
"description": "PostCSS plugin that adds a user input scope to each selector", | ||
@@ -24,12 +24,15 @@ "keywords": [ | ||
"dependencies": { | ||
"postcss": "^5.0.0", | ||
"postcss-selector-parser": "^6.0.2" | ||
"postcss-selector-parser": "^6.0.6" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.0.2" | ||
"mocha": "^8.4.0", | ||
"postcss": "^8.3.0" | ||
}, | ||
"peerDependencies": { | ||
"postcss": "^8.3.0" | ||
}, | ||
"scripts": { | ||
"test": "istanbul cover ./node_modules/mocha/bin/_mocha -- -u exports test/*.js" | ||
"test": "istanbul cover ./node_modules/.bin/mocha test/*.js" | ||
} | ||
} |
# PostCSS Scopify | ||
[![Build status][travis-image]][travis-url] | ||
[![Node.js CI](https://github.com/pazams/postcss-scopify/actions/workflows/node.js.yml/badge.svg)](https://github.com/pazams/postcss-scopify/actions/workflows/node.js.yml) | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
@@ -42,3 +42,3 @@ [![Downloads][downloads-image]][downloads-url] | ||
__Example output__ | ||
`scopify('#scope')` | ||
`scopify('#scope')` or `scopify({scope: '#scope'})` | ||
```css | ||
@@ -80,2 +80,7 @@ #scope .foo, #scope .boo h1 { | ||
## Change Log | ||
### v1.0.0 | ||
- update all deps to latest versions | ||
- update plugin syntax for postCss v8+ | ||
- allow pass scope as object (because postcss+webpack) | ||
### v0.1.8 | ||
@@ -82,0 +87,0 @@ closes [#10](https://github.com/pazams/postcss-scopify/issues/10) |
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
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
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
60093
18
499
0
95
3
+ Addednanoid@3.3.7(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedpostcss@8.4.49(transitive)
+ Addedsource-map-js@1.2.1(transitive)
- Removedpostcss@^5.0.0
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedchalk@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhas-flag@1.0.0(transitive)
- Removedjs-base64@2.6.4(transitive)
- Removedpostcss@5.2.18(transitive)
- Removedsource-map@0.5.7(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.03.2.3(transitive)