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

postcss-modules-local-by-default

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-modules-local-by-default - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

7

CHANGELOG.md

@@ -9,2 +9,6 @@ # Change Log

## [0.0.7] - 2015-05-30
### Changed
- Migrated to `css-selector-tokenizer`.
## [0.0.6] - 2015-05-28

@@ -38,3 +42,3 @@ ### Changed

[unreleased]: https://github.com/postcss-modules-local-by-default/compare/v0.0.6...HEAD
[unreleased]: https://github.com/postcss-modules-local-by-default/compare/v0.0.7...HEAD
[0.0.2]: https://github.com/postcss-modules-local-by-default/compare/v0.0.1...v0.0.2

@@ -45,1 +49,2 @@ [0.0.3]: https://github.com/postcss-modules-local-by-default/compare/v0.0.2...v0.0.3

[0.0.6]: https://github.com/postcss-modules-local-by-default/compare/v0.0.5...v0.0.6
[0.0.7]: https://github.com/postcss-modules-local-by-default/compare/v0.0.6...v0.0.7

74

index.js
var postcss = require('postcss');
var Tokenizer = require('css-selector-tokenizer');
var ESCAPED_DOT = ' ___LOCAL_SCOPE__ESCAPED_DOT___ ';
function localizeNodes(nodes) {
var isGlobalContext = false;
module.exports = postcss.plugin('postcss-modules-local-by-default', function (config) {
var options = config || {};
return function(css, result) {
css.eachRule(function(rule) {
rule.selector = rule.selector
.split(',')
.map(transformSelector.bind(null, options, rule))
.join(',');
});
};
});
return nodes
.map(function(node, i) {
var newNode = node;
function transformSelector(options, rule, selector) {
var trimmedSelector = selector.trim();
if (isGlobal(newNode)) {
isGlobalContext = true;
return null;
}
if (options.lint) {
if (!/^\./.test(trimmedSelector) && !/^\:local/.test(trimmedSelector)) {
if (!/^\:global/.test(trimmedSelector)) {
throw rule.error('Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global '+trimmedSelector+'"', { plugin: 'postcss-modules-local-by-default' });
if (newNode.type === 'spacing' && isGlobal(nodes[i-1])) {
return null;
}
}
}
return selector
.replace(/\:global\((.*?)\)/g, escapeDots)
.replace(/\:global (.*)/g, escapeDots)
.replace(/(\:extends\((.*?)\))/g, escapeDots)
.replace(/\.local\[(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)\]/g, '.$1') // source: http://stackoverflow.com/questions/448981/what-characters-are-valid-in-css-class-names-selectors
.replace(/\:local\(\.(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)\)/g, '.$1')
.replace(/\.(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)/g, ':local(.$1)')
.replace(new RegExp(ESCAPED_DOT, 'g'), '.');
if (!isGlobalContext && node.type === 'class') {
newNode = { type: 'nested-pseudo-class', name: 'local', nodes: [node] }
} else if (isNestedGlobal(newNode)) {
newNode = node.nodes[0];
} else if (!isNestedLocal(newNode) && newNode.nodes) {
newNode.nodes = localizeNodes(newNode.nodes);
}
return newNode;
}).filter(function(node) {
return node !== null
});
}
function escapeDots(match, p1) {
return p1.replace(/\./g, ESCAPED_DOT);
function isGlobal(node) {
return node.type === 'pseudo-class' && node.name === 'global';
}
function isNestedGlobal(node) {
return node.type === 'nested-pseudo-class' && node.name === 'global';
}
function isNestedLocal(node) {
return node.type === 'nested-pseudo-class' && node.name === 'local';
}
module.exports = postcss.plugin('postcss-modules-local-by-default', function () {
return function(css, result) {
css.eachRule(function(rule) {
var selector = Tokenizer.parse(rule.selector);
selector.nodes = localizeNodes(selector.nodes);
rule.selector = Tokenizer.stringify(selector).trim();
});
};
});
{
"name": "postcss-modules-local-by-default",
"version": "0.0.6",
"version": "0.0.7",
"description": "A CSS Modules transform to make local scope the default",

@@ -18,2 +18,3 @@ "keywords": [

"dependencies": {
"css-selector-tokenizer": "^0.3.1",
"postcss": "^4.1.5"

@@ -20,0 +21,0 @@ },

@@ -5,3 +5,3 @@ [![Build Status][ci-img]][ci] [![npm][npm-img]][npm]

Transforms:
Transformation examples:

@@ -28,2 +28,3 @@ ```css

.foo:global(.bar) .baz { ... } /* => */ :local(.foo).bar :local(.baz) { ... }
```

@@ -30,0 +31,0 @@ ## Development

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