🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

postcss-nested-vars

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-nested-vars - npm Package Compare versions

Comparing version
0.0.2
to
1.0.0
+19
dist/plugin.d.ts
import * as postcss from 'postcss';
declare const PostCssNestedVars: postcss.Plugin<PostCssNestedVars.Options>;
declare namespace PostCssNestedVars {
interface Options {
/**
* Global variables that can be referenced from any context.
*/
globals?: {
[key: string]: any;
};
/**
* If a variable cannot be resolved, this specifies how to handle it.
* Possible values: error, warn, silent. `warn` and `silent` modes will
* preserve the original values (e.g., `$foo` will remain `$foo`).
*/
logLevel?: string;
}
}
export = PostCssNestedVars;
"use strict";
const postcss = require("postcss");
const plugin = 'postcss-nested-vars';
const PostCssNestedVars = postcss.plugin(plugin, (options = {}) => {
options.logLevel = options.logLevel || 'error';
const errorContext = { plugin };
const specialSearchValue = /\$\(([\w\d-_]+)\)/g;
const logMap = {
error(message, node) {
throw node.error(message, errorContext);
},
warn(message, node, result) {
node.warn(result, message);
},
silent() {
// noop
}
};
const log = logMap[options.logLevel];
if (!log) {
throw new Error(`Invalid logLevel: ${options.logLevel}`);
}
const globals = {};
if (options.globals) {
Object.keys(options.globals).forEach(key => {
globals[key] = [options.globals[key]];
});
}
return (root, result) => {
walk(root, result, globals);
};
function walk(container, result, vars) {
const containerVars = {};
container.walk(node => {
if (node.type === 'rule') {
resolveContainer(node, 'selector');
return;
}
if (node.type === 'atrule') {
resolveContainer(node, 'params');
return;
}
if (node.type === 'decl') {
resolveDeclaration(node);
return;
}
});
Object.keys(containerVars).forEach(varName => {
vars[varName].pop();
});
function resolveContainer(container2, prop) {
if (container2[prop].indexOf('$(') !== -1) {
replaceAllVars(container2, prop, specialSearchValue);
}
walk(container2, result, vars);
}
function resolveDeclaration(decl) {
if (decl.prop.indexOf('$(') !== -1) {
replaceAllVars(decl, 'prop', specialSearchValue);
}
if (/^\$(?!\()/.test(decl.prop)) {
const m = decl.prop.match(/^\$([\w\d-_]+)$/);
const varName = m && m[1];
const stack = vars[varName];
if (!stack) {
vars[varName] = [];
}
if (!containerVars[varName]) {
containerVars[varName] = true;
vars[varName].push(decl.value);
}
else {
stack[stack.length - 1] = decl.value;
}
decl.remove();
return;
}
if (decl.value.indexOf('$') !== -1) {
replaceAllVars(decl, 'value', /\$([\w\d-_]+)/g);
}
}
function replaceAllVars(obj, prop, searchValue) {
obj[prop] = obj[prop].replace(searchValue, (m, varName) => {
const stack = vars[varName];
if (!stack || !stack.length) {
log(`Undefined variable: ${varName}`, obj, result);
return `$${varName}`;
}
return stack[stack.length - 1];
});
}
}
});
module.exports = PostCssNestedVars;
//# sourceMappingURL=plugin.js.map
+3
-0

@@ -0,1 +1,4 @@

## 1.0.0
- Upgrade to PostCSS 6.
## 0.0.2

@@ -2,0 +5,0 @@ - Fix code coloring on npm readme.

+47
-25
{
"name": "postcss-nested-vars",
"version": "0.0.2",
"version": "1.0.0",
"description": "PostCSS plugin for nested variables.",
"main": "dist/lib/plugin.js",
"main": "dist/plugin.js",
"types": "dist/plugin.d.ts",
"scripts": {
"prepublish": "gulp && gulp copy && npm run babelify",
"babelify": "babel build/lib --out-dir dist/lib",
"test": "gulp && npm run cover && npm run check-coverage",
"cover": "isparta cover node_modules/mocha/bin/_mocha -i build/lib/plugin.js",
"check-coverage": "istanbul check-coverage",
"watch": "sh scripts/watch"
"clean": "rimraf coverage dist *.log",
"codecov": "codecov -f coverage/lcov.info",
"compile": "tsc",
"compile:watch": "tsc --watch",
"prepublish": "npm test",
"pretest": "npm run tslint && npm run clean && npm run compile",
"test": "nyc ava",
"test:watch": "ava --watch",
"tslint": "tslint --project tsconfig.json",
"watch": "npm run test:watch"
},
"ava": {
"files": [
"dist/**/*.spec.js"
],
"source": [
"dist/**/*.js"
]
},
"nyc": {
"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100,
"include": [
"dist/**/*.js"
],
"exclude": [
"dist/**/*.spec.js"
],
"reporter": [
"lcov",
"text"
],
"cache": true,
"all": true,
"check-coverage": true
},
"repository": {

@@ -33,22 +65,12 @@ "type": "git",

"dependencies": {
"postcss": "^5.0.5"
"postcss": "^6.0.14"
},
"devDependencies": {
"babel": "^5.8.23",
"babel-core": "^5.8.23",
"babel-eslint": "^4.1.1",
"chai": "^3.2.0",
"del": "^1.2.1",
"eslint": "^1.3.1",
"event-stream": "^3.3.1",
"gulp": "^3.9.0",
"gulp-eslint": "^1.0.0",
"gulp-filter": "^3.0.1",
"gulp-plumber": "^1.0.1",
"gulp-tslint": "^3.2.0",
"gulp-typescript": "^2.8.1",
"isparta": "3.0.3",
"istanbul": "^0.3.19",
"mocha": "^2.3.0"
"@types/node": "^8.0.47",
"ava": "^0.23.0",
"nyc": "^11.3.0",
"rimraf": "^2.6.2",
"tslint": "^5.8.0",
"typescript": "^2.6.1"
}
}
+14
-14

@@ -9,4 +9,5 @@ # postcss-nested-vars

[![npm license](http://img.shields.io/npm/l/postcss-nested-vars.svg?style=flat-square)](https://www.npmjs.org/package/postcss-nested-vars)
[![Travis Build Status](https://img.shields.io/travis/jedmao/postcss-nested-vars.svg?label=unix)](https://travis-ci.org/jedmao/postcss-nested-vars)
[![AppVeyor Build Status](https://img.shields.io/appveyor/ci/jedmao/postcss-nested-vars.svg?label=windows)](https://ci.appveyor.com/project/jedmao/postcss-nested-vars)
[![Travis Build Status](https://img.shields.io/travis/jedmao/postcss-nested-vars.svg)](https://travis-ci.org/jedmao/postcss-nested-vars)
[![codecov](https://codecov.io/gh/jedmao/postcss-nested-vars/branch/master/graph/badge.svg)](https://codecov.io/gh/jedmao/postcss-nested-vars)
[![Dependency Status](https://gemnasium.com/badges/github.com/jedmao/postcss-nested-vars.svg)](https://gemnasium.com/github.com/jedmao/postcss-nested-vars)

@@ -85,6 +86,3 @@ [![npm](https://nodei.co/npm/postcss-nested-vars.svg?downloads=true)](https://nodei.co/npm/postcss-nested-vars/)

```js
postcss([
require('postcss-nested-vars')(/* options */),
// more plugins...
])
postcss([ require('postcss-nested-vars')(/* options */) ]);
```

@@ -95,9 +93,5 @@

```ts
///<reference path="node_modules/postcss-nested-vars/.d.ts" />
import postcssNestedVars from 'postcss-nested-vars';
import * as postcssNestedVars from 'postcss-nested-vars';
postcss([
postcssNestedVars(/* options */),
// more plugins...
])
postcss([ postcssNestedVars(/* options */) ]);
```

@@ -135,8 +129,14 @@

For much faster development cycles, run the following command:
For much faster development cycles, run the following commands in 2 separate processes:
```
$ npm run build:watch
```
Compiles TypeScript source into the `./dist` folder and watches for changes.
```
$ npm run watch
```
This will build scripts, run tests and watch for changes.
Runs the tests in the `./dist` folder and watches for changes.
declare module 'postcss-nested-vars' {
import plugin from 'dist/lib/plugin';
export default plugin;
}
/// <reference path="../../node_modules/postcss/postcss.d.ts" />
import postcss from 'postcss';
declare var _default: postcss.Plugin<PostCssNestedVars.Options>;
export default _default;
export declare module PostCssNestedVars {
interface Options {
/**
* Global variables that can be referenced from any context.
*/
globals?: Object;
/**
* If a variable cannot be resolved, this specifies how to handle it.
* Possible values: error, warn, silent. `warn` and `silent` modes will
* preserve the original values (e.g., `$foo` will remain `$foo`).
*/
logLevel?: string;
}
}
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _postcss = require('postcss');
var _postcss2 = _interopRequireDefault(_postcss);
var plugin = 'postcss-nested-vars';
exports['default'] = _postcss2['default'].plugin(plugin, function () {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
options.logLevel = options.logLevel || 'error';
var errorContext = { plugin: plugin };
var specialSearchValue = /\$\(([\w\d-_]+)\)/g;
var log = ({
error: function error(message, node) {
throw node.error(message, errorContext);
},
warn: function warn(message, node, result) {
node.warn(result, message);
},
silent: function silent() {
// noop
}
})[options.logLevel];
if (!log) {
throw new Error('Invalid logLevel: ' + options.logLevel);
}
var globals = {};
if (options.globals) {
Object.keys(options.globals).forEach(function (key) {
globals[key] = [options.globals[key]];
});
}
return function (root, result) {
walk(root, result, globals);
};
function walk(container, result, vars) {
var containerVars = {};
container.walk(function (node) {
if (node.type === 'rule') {
resolveContainer(node, 'selector');
return;
}
if (node.type === 'atrule') {
resolveContainer(node, 'params');
return;
}
if (node.type === 'decl') {
resolveDeclaration(node);
return;
}
});
Object.keys(containerVars).forEach(function (varName) {
vars[varName].pop();
});
function resolveContainer(container2, prop) {
if (container2[prop].indexOf('$(') !== -1) {
replaceAllVars(container2, prop, specialSearchValue);
}
walk(container2, result, vars);
}
function resolveDeclaration(decl) {
if (decl.prop.indexOf('$(') !== -1) {
replaceAllVars(decl, 'prop', specialSearchValue);
}
if (/^\$(?!\()/.test(decl.prop)) {
var m = decl.prop.match(/^\$([\w\d-_]+)$/);
var varName = m && m[1];
var stack = vars[varName];
if (!stack) {
vars[varName] = [];
}
if (!containerVars[varName]) {
containerVars[varName] = true;
vars[varName].push(decl.value);
} else {
stack[stack.length - 1] = decl.value;
}
decl.remove();
return;
}
if (decl.value.indexOf('$') !== -1) {
replaceAllVars(decl, 'value', /\$([\w\d-_]+)/g);
}
}
function replaceAllVars(obj, prop, searchValue) {
obj[prop] = obj[prop].replace(searchValue, function (m, varName) {
var stack = vars[varName];
if (!stack || !stack.length) {
log('Undefined variable: ' + varName, obj, result);
return '$' + varName;
}
return stack[stack.length - 1];
});
}
}
});
module.exports = exports['default'];

Sorry, the diff of this file is too big to display

/// <reference path="node/node.d.ts" />
/// <reference path="../node_modules/postcss/postcss.d.ts" />
/// <reference path="mocha/mocha.d.ts" />
/// <reference path="chai/chai.d.ts" />
/// <reference path="sinon-chai/sinon-chai.d.ts" />