postcss-apply
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -10,2 +10,7 @@ # postcss-apply change Log | ||
## [0.11.0] - 2018-08-11 | ||
### Changed | ||
* Upgrade PostCSS to version 7. | ||
**Breaking** Removes support for Node.js versions lower than 6. | ||
## [0.10.0] - 2018-04-16 | ||
@@ -69,3 +74,4 @@ ### Added | ||
[Unreleased]: https://github.com/pascalduez/postcss-apply/compare/0.10.0...HEAD | ||
[Unreleased]: https://github.com/pascalduez/postcss-apply/compare/0.11.0...HEAD | ||
[0.11.0]: https://github.com/pascalduez/postcss-apply/compare/0.9.0...0.11.0 | ||
[0.10.0]: https://github.com/pascalduez/postcss-apply/compare/0.9.0...0.10.0 | ||
@@ -72,0 +78,0 @@ [0.9.0]: https://github.com/pascalduez/postcss-apply/compare/0.8.0...0.9.0 |
@@ -15,15 +15,13 @@ 'use strict'; | ||
exports.default = (0, _postcss.plugin)('postcss-apply', function (options) { | ||
return function (css, result) { | ||
var visitor = new _visitor2.default(options); | ||
visitor.result = result; | ||
exports.default = (0, _postcss.plugin)('postcss-apply', options => (css, result) => { | ||
const visitor = new _visitor2.default(options); | ||
visitor.result = result; | ||
visitor.prepend(); | ||
visitor.prepend(); | ||
css.walkRules(visitor.collect); | ||
css.walkRules(visitor.collect); | ||
visitor.resolveNested(); | ||
visitor.resolveNested(); | ||
css.walkAtRules('apply', visitor.resolve); | ||
}; | ||
css.walkAtRules('apply', visitor.resolve); | ||
}); |
@@ -8,4 +8,4 @@ 'use strict'; | ||
function kebabify(prop) { | ||
var upperToHyphen = function upperToHyphen(match, offset, string) { | ||
var addDash = offset && string.charAt(offset - 1) !== '-'; | ||
const upperToHyphen = (match, offset, string) => { | ||
const addDash = offset && string.charAt(offset - 1) !== '-'; | ||
@@ -18,4 +18,2 @@ return (addDash ? '-' : '') + match.toLowerCase(); | ||
var isPlainObject = exports.isPlainObject = function isPlainObject(arg) { | ||
return Object.prototype.toString.call(arg) === '[object Object]'; | ||
}; | ||
const isPlainObject = exports.isPlainObject = arg => Object.prototype.toString.call(arg) === '[object Object]'; |
@@ -12,5 +12,4 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
/* eslint-disable no-param-reassign */ | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _balancedMatch = require('balanced-match'); | ||
@@ -28,176 +27,165 @@ | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/* eslint-disable no-param-reassign */ | ||
const RE_PROP_SET = /^(--)([\w-]+)(\s*)([:]?)$/; | ||
var RE_PROP_SET = /^(--)([\w-]+)(\s*)([:]?)$/; | ||
class Visitor { | ||
var Visitor = function Visitor(options) { | ||
var _this = this; | ||
constructor(options) { | ||
this.cache = {}; | ||
this.result = {}; | ||
this.options = {}; | ||
this.defaults = { | ||
preserve: false, | ||
sets: {} | ||
}; | ||
_classCallCheck(this, Visitor); | ||
this.prepend = () => { | ||
const sets = this.options.sets; | ||
this.cache = {}; | ||
this.result = {}; | ||
this.options = {}; | ||
this.defaults = { | ||
preserve: false, | ||
sets: {} | ||
}; | ||
// $FlowFixMe | ||
this.prepend = function () { | ||
var sets = _this.options.sets; | ||
Object.keys(sets).forEach(setName => { | ||
const newRule = _postcss2.default.rule({ selector: `--${setName}` }); | ||
// $FlowFixMe | ||
// $FlowFixMe | ||
const set = sets[setName]; | ||
Object.keys(sets).forEach(function (setName) { | ||
var newRule = _postcss2.default.rule({ selector: `--${setName}` }); | ||
if (typeof set === 'string') { | ||
newRule.prepend(set); | ||
} else if ((0, _utils.isPlainObject)(set)) { | ||
(0, _entries2.default)(set).forEach(([prop, value]) => { | ||
newRule.prepend(_postcss2.default.decl({ prop: (0, _utils.kebabify)(prop), value })); | ||
}); | ||
} else { | ||
throw new Error(`Unrecognized set type \`${typeof set}\`, must be an object or string.`); | ||
} | ||
// $FlowFixMe | ||
var set = sets[setName]; | ||
this.cache[setName] = newRule; | ||
}); | ||
}; | ||
if (typeof set === 'string') { | ||
newRule.prepend(set); | ||
} else if ((0, _utils.isPlainObject)(set)) { | ||
(0, _entries2.default)(set).forEach(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
prop = _ref2[0], | ||
value = _ref2[1]; | ||
this.collect = rule => { | ||
const matches = RE_PROP_SET.exec(rule.selector); | ||
newRule.prepend(_postcss2.default.decl({ prop: (0, _utils.kebabify)(prop), value })); | ||
}); | ||
} else { | ||
throw new Error(`Unrecognized set type \`${typeof set}\`, must be an object or string.`); | ||
if (!matches) { | ||
return; | ||
} | ||
_this.cache[setName] = newRule; | ||
}); | ||
}; | ||
const setName = matches[2]; | ||
const parent = rule.parent; | ||
this.collect = function (rule) { | ||
var matches = RE_PROP_SET.exec(rule.selector); | ||
if (!matches) { | ||
return; | ||
} | ||
if (parent.selector !== ':root') { | ||
rule.warn(this.result, 'Custom property set ignored: not scoped to top-level `:root` ' + `(--${setName}` + `${parent.type === 'rule' ? ` declared in ${parent.selector}` : ''})`); | ||
var setName = matches[2]; | ||
var parent = rule.parent; | ||
if (parent.type === 'root') { | ||
rule.remove(); | ||
} | ||
return; | ||
} | ||
if (parent.selector !== ':root') { | ||
rule.warn(_this.result, 'Custom property set ignored: not scoped to top-level `:root` ' + `(--${setName}` + `${parent.type === 'rule' ? ` declared in ${parent.selector}` : ''})`); | ||
// Custom property sets override each other wholly, | ||
// rather than cascading together like colliding style rules do. | ||
// @see: https://tabatkins.github.io/specs/css-apply-rule/#defining | ||
const newRule = rule.clone(); | ||
this.cache[setName] = newRule; | ||
if (parent.type === 'root') { | ||
rule.remove(); | ||
if (!this.options.preserve) { | ||
removeCommentBefore(rule); | ||
safeRemoveRule(rule); | ||
} | ||
return; | ||
} | ||
if (!parent.nodes.length) { | ||
parent.remove(); | ||
} | ||
}; | ||
// Custom property sets override each other wholly, | ||
// rather than cascading together like colliding style rules do. | ||
// @see: https://tabatkins.github.io/specs/css-apply-rule/#defining | ||
var newRule = rule.clone(); | ||
_this.cache[setName] = newRule; | ||
this.resolveNested = () => { | ||
Object.keys(this.cache).forEach(rule => { | ||
this.cache[rule].walkAtRules('apply', atRule => { | ||
this.resolve(atRule); | ||
if (!_this.options.preserve) { | ||
removeCommentBefore(rule); | ||
safeRemoveRule(rule); | ||
} | ||
if (!parent.nodes.length) { | ||
parent.remove(); | ||
} | ||
}; | ||
this.resolveNested = function () { | ||
Object.keys(_this.cache).forEach(function (rule) { | ||
_this.cache[rule].walkAtRules('apply', function (atRule) { | ||
_this.resolve(atRule); | ||
// @TODO honor `preserve` option. | ||
atRule.remove(); | ||
// @TODO honor `preserve` option. | ||
atRule.remove(); | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; | ||
this.resolve = function (atRule) { | ||
var ancestor = atRule.parent; | ||
this.resolve = atRule => { | ||
let ancestor = atRule.parent; | ||
while (ancestor && ancestor.type !== 'rule') { | ||
ancestor = ancestor.parent; | ||
} | ||
while (ancestor && ancestor.type !== 'rule') { | ||
ancestor = ancestor.parent; | ||
} | ||
if (!ancestor) { | ||
atRule.warn(_this.result, 'The @apply rule can only be declared inside Rule type nodes.'); | ||
if (!ancestor) { | ||
atRule.warn(this.result, 'The @apply rule can only be declared inside Rule type nodes.'); | ||
atRule.remove(); | ||
return; | ||
} | ||
atRule.remove(); | ||
return; | ||
} | ||
if (isDefinition(atRule.parent)) { | ||
return; | ||
} | ||
if (isDefinition(atRule.parent)) { | ||
return; | ||
} | ||
var param = getParamValue(atRule.params); | ||
var matches = RE_PROP_SET.exec(param); | ||
const param = getParamValue(atRule.params); | ||
const matches = RE_PROP_SET.exec(param); | ||
if (!matches) { | ||
return; | ||
} | ||
if (!matches) { | ||
return; | ||
} | ||
var setName = matches[2]; | ||
var parent = atRule.parent; | ||
const setName = matches[2]; | ||
const parent = atRule.parent; | ||
if (!(setName in _this.cache)) { | ||
atRule.warn(_this.result, `No custom property set declared for \`${setName}\`.`); | ||
if (!(setName in this.cache)) { | ||
atRule.warn(this.result, `No custom property set declared for \`${setName}\`.`); | ||
return; | ||
} | ||
return; | ||
} | ||
var newRule = _this.cache[setName].clone(); | ||
cleanIndent(newRule); | ||
const newRule = this.cache[setName].clone(); | ||
cleanIndent(newRule); | ||
if (_this.options.preserve) { | ||
parent.insertBefore(atRule, newRule.nodes); | ||
if (this.options.preserve) { | ||
parent.insertBefore(atRule, newRule.nodes); | ||
return; | ||
} | ||
return; | ||
} | ||
atRule.replaceWith(newRule.nodes); | ||
}; | ||
atRule.replaceWith(newRule.nodes); | ||
}; | ||
this.options = _extends({}, this.defaults, options); | ||
} | ||
this.options = _extends({}, this.defaults, options); | ||
} | ||
/** | ||
* Prepend JS defined sets into the cache before parsing. | ||
* This means CSS defined sets will overrides them if they share the same name. | ||
*/ | ||
/** | ||
* Prepend JS defined sets into the cache before parsing. | ||
* This means CSS defined sets will overrides them if they share the same name. | ||
*/ | ||
/** | ||
* Collect all `:root` declared property sets and save them. | ||
*/ | ||
/** | ||
* Collect all `:root` declared property sets and save them. | ||
*/ | ||
/** | ||
* Replace nested `@apply` at-rules declarations. | ||
*/ | ||
/** | ||
* Replace nested `@apply` at-rules declarations. | ||
*/ | ||
/** | ||
* Replace `@apply` at-rules declarations with provided custom property set. | ||
*/ | ||
; | ||
/** | ||
* Replace `@apply` at-rules declarations with provided custom property set. | ||
*/ | ||
} | ||
/** | ||
* Helper: return whether the rule is a custom property set definition. | ||
*/ | ||
exports.default = Visitor; /** | ||
* Helper: return whether the rule is a custom property set definition. | ||
*/ | ||
exports.default = Visitor; | ||
function isDefinition(rule) { | ||
return !!rule.selector && RE_PROP_SET.exec(rule.selector) && rule.parent && !!rule.parent.selector && rule.parent.selector === ':root'; | ||
return !!rule.selector && !!RE_PROP_SET.exec(rule.selector) && rule.parent && !!rule.parent.selector && rule.parent.selector === ':root'; | ||
} | ||
@@ -218,3 +206,3 @@ | ||
function cleanIndent(rule) { | ||
rule.walkDecls(function (decl) { | ||
rule.walkDecls(decl => { | ||
if (typeof decl.raws.before === 'string') { | ||
@@ -242,3 +230,3 @@ decl.raws.before = decl.raws.before.replace(/[^\S\n\r]{2,}/, ' '); | ||
function removeCommentBefore(node) { | ||
var previousNode = node.prev(); | ||
const previousNode = node.prev(); | ||
@@ -245,0 +233,0 @@ if (previousNode && previousNode.type === 'comment') { |
{ | ||
"name": "postcss-apply", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "PostCSS plugin enabling custom properties sets references", | ||
@@ -29,2 +29,5 @@ "keywords": [ | ||
], | ||
"engines": { | ||
"node": ">=6.0.0" | ||
}, | ||
"scripts": { | ||
@@ -55,23 +58,23 @@ "lint": "eslint src/ test/", | ||
"balanced-match": "^1.0.0", | ||
"postcss": "^6.0.21" | ||
"postcss": "^7.0.2" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-eslint": "^8.2.3", | ||
"babel-eslint": "^8.2.6", | ||
"babel-plugin-transform-class-properties": "^6.24.1", | ||
"babel-plugin-transform-object-entries": "^1.0.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-preset-flow": "^6.23.0", | ||
"common-tags": "^1.7.2", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb-base": "^12.1.0", | ||
"common-tags": "^1.8.0", | ||
"eslint": "^5.3.0", | ||
"eslint-config-airbnb-base": "^13.0.0", | ||
"eslint-config-prettier": "^2.9.0", | ||
"eslint-plugin-flowtype": "^2.46.2", | ||
"eslint-plugin-import": "^2.11.0", | ||
"eslint-plugin-jest": "^21.15.0", | ||
"flow-bin": "^0.69.0", | ||
"eslint-plugin-flowtype": "^2.50.0", | ||
"eslint-plugin-import": "^2.13.0", | ||
"eslint-plugin-jest": "^21.20.2", | ||
"flow-bin": "^0.78.0", | ||
"flow-coverage-report": "^0.5.0", | ||
"jest-cli": "^22.4.3", | ||
"npm-run-all": "^4.1.2", | ||
"jest-cli": "^23.5.0", | ||
"npm-run-all": "^4.1.3", | ||
"opn-cli": "^3.1.0", | ||
@@ -78,0 +81,0 @@ "postcss-custom-properties": "^7.0.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
17287
198
+ Addedpicocolors@0.2.1(transitive)
+ Addedpostcss@7.0.39(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedpostcss@6.0.23(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedpostcss@^7.0.2