index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require('postcss');
  6. var _has = require('has');
  7. var _has2 = _interopRequireDefault(_has);
  8. var _browserslist = require('browserslist');
  9. var _browserslist2 = _interopRequireDefault(_browserslist);
  10. var _caniuseApi = require('caniuse-api');
  11. var _fromInitial = require('../data/fromInitial.json');
  12. var _fromInitial2 = _interopRequireDefault(_fromInitial);
  13. var _toInitial = require('../data/toInitial.json');
  14. var _toInitial2 = _interopRequireDefault(_toInitial);
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. const initial = 'initial';
  17. exports.default = (0, _postcss.plugin)('postcss-reduce-initial', () => {
  18. return (css, result) => {
  19. const resultOpts = result.opts || {};
  20. const browsers = (0, _browserslist2.default)(null, {
  21. stats: resultOpts.stats,
  22. path: __dirname,
  23. env: resultOpts.env
  24. });
  25. const initialSupport = (0, _caniuseApi.isSupported)('css-initial-value', browsers);
  26. css.walkDecls(decl => {
  27. const lowerCasedProp = decl.prop.toLowerCase();
  28. if (initialSupport && (0, _has2.default)(_toInitial2.default, lowerCasedProp) && decl.value.toLowerCase() === _toInitial2.default[lowerCasedProp]) {
  29. decl.value = initial;
  30. return;
  31. }
  32. if (decl.value.toLowerCase() !== initial || !_fromInitial2.default[lowerCasedProp]) {
  33. return;
  34. }
  35. decl.value = _fromInitial2.default[lowerCasedProp];
  36. });
  37. };
  38. });
  39. module.exports = exports['default'];