Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

postcss-scopify

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-scopify - npm Package Compare versions

Comparing version 0.1.10 to 1.0.0

.github/workflows/node.js.yml

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)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc