index.esm.mjs 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
import postcss from 'postcss';
import valueParser from 'postcss-values-parser';
import fs from 'fs';
import path from 'path';

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  try {
    var info = gen[key](arg);
    var value = info.value;
  } catch (error) {
    reject(error);
    return;
  }

  if (info.done) {
    resolve(value);
  } else {
    Promise.resolve(value).then(_next, _throw);
  }
}

function _asyncToGenerator(fn) {
  return function () {
    var self = this,
        args = arguments;
    return new Promise(function (resolve, reject) {
      var gen = fn.apply(self, args);

      function _next(value) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
      }

      function _throw(err) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
      }

      _next(undefined);
    });
  };
}

function parse(string) {
  return valueParser(string).parse();
}

function isBlockIgnored(ruleOrDeclaration) {
  var rule = ruleOrDeclaration.selector ? ruleOrDeclaration : ruleOrDeclaration.parent;
  return /(!\s*)?postcss-custom-properties:\s*off\b/i.test(rule.toString());
}

function isRuleIgnored(rule) {
  var previous = rule.prev();
  return Boolean(isBlockIgnored(rule) || previous && previous.type === 'comment' && /(!\s*)?postcss-custom-properties:\s*ignore\s+next\b/i.test(previous.text));
}

function getCustomPropertiesFromRoot(root, opts) {
  // initialize custom selectors
  const customPropertiesFromHtmlElement = {};
  const customPropertiesFromRootPseudo = {}; // for each html or :root rule

  root.nodes.slice().forEach(rule => {
    const customPropertiesObject = isHtmlRule(rule) ? customPropertiesFromHtmlElement : isRootRule(rule) ? customPropertiesFromRootPseudo : null; // for each custom property

    if (customPropertiesObject) {
      rule.nodes.slice().forEach(decl => {
        if (isCustomDecl(decl) && !isBlockIgnored(decl)) {
          const prop = decl.prop; // write the parsed value to the custom property

          customPropertiesObject[prop] = parse(decl.value).nodes; // conditionally remove the custom property declaration

          if (!opts.preserve) {
            decl.remove();
          }
        }
      }); // conditionally remove the empty html or :root rule

      if (!opts.preserve && isEmptyParent(rule) && !isBlockIgnored(rule)) {
        rule.remove();
      }
    }
  }); // return all custom properties, preferring :root properties over html properties

  return Object.assign({}, customPropertiesFromHtmlElement, customPropertiesFromRootPseudo);
} // match html and :root rules

const htmlSelectorRegExp = /^html$/i;
const rootSelectorRegExp = /^:root$/i;
const customPropertyRegExp = /^--[A-z][\w-]*$/; // whether the node is an html or :root rule

const isHtmlRule = node => node.type === 'rule' && htmlSelectorRegExp.test(node.selector) && Object(node.nodes).length;

const isRootRule = node => node.type === 'rule' && rootSelectorRegExp.test(node.selector) && Object(node.nodes).length; // whether the node is an custom property


const isCustomDecl = node => node.type === 'decl' && customPropertyRegExp.test(node.prop); // whether the node is a parent without children


const isEmptyParent = node => Object(node.nodes).length === 0;

/* Get Custom Properties from CSS File
/* ========================================================================== */

function getCustomPropertiesFromCSSFile(_x) {
  return _getCustomPropertiesFromCSSFile.apply(this, arguments);
}
/* Get Custom Properties from Object
/* ========================================================================== */


function _getCustomPropertiesFromCSSFile() {
  _getCustomPropertiesFromCSSFile = _asyncToGenerator(function* (from) {
    const css = yield readFile(from);
    const root = postcss.parse(css, {
      from
    });
    return getCustomPropertiesFromRoot(root, {
      preserve: true
    });
  });
  return _getCustomPropertiesFromCSSFile.apply(this, arguments);
}

function getCustomPropertiesFromObject(object) {
  const customProperties = Object.assign({}, Object(object).customProperties, Object(object)['custom-properties']);

  for (const key in customProperties) {
    customProperties[key] = parse(String(customProperties[key])).nodes;
  }

  return customProperties;
}
/* Get Custom Properties from JSON file
/* ========================================================================== */


function getCustomPropertiesFromJSONFile(_x2) {
  return _getCustomPropertiesFromJSONFile.apply(this, arguments);
}
/* Get Custom Properties from JS file
/* ========================================================================== */


function _getCustomPropertiesFromJSONFile() {
  _getCustomPropertiesFromJSONFile = _asyncToGenerator(function* (from) {
    const object = yield readJSON(from);
    return getCustomPropertiesFromObject(object);
  });
  return _getCustomPropertiesFromJSONFile.apply(this, arguments);
}

function getCustomPropertiesFromJSFile(_x3) {
  return _getCustomPropertiesFromJSFile.apply(this, arguments);
}
/* Get Custom Properties from Imports
/* ========================================================================== */


function _getCustomPropertiesFromJSFile() {
  _getCustomPropertiesFromJSFile = _asyncToGenerator(function* (from) {
    const object = yield import(from);
    return getCustomPropertiesFromObject(object);
  });
  return _getCustomPropertiesFromJSFile.apply(this, arguments);
}

function getCustomPropertiesFromImports(sources) {
  return sources.map(source => {
    if (source instanceof Promise) {
      return source;
    } else if (source instanceof Function) {
      return source();
    } // read the source as an object


    const opts = source === Object(source) ? source : {
      from: String(source)
    }; // skip objects with Custom Properties

    if (opts.customProperties || opts['custom-properties']) {
      return opts;
    } // source pathname


    const from = path.resolve(String(opts.from || '')); // type of file being read from

    const type = (opts.type || path.extname(from).slice(1)).toLowerCase();
    return {
      type,
      from
    };
  }).reduce(
  /*#__PURE__*/
  function () {
    var _ref = _asyncToGenerator(function* (customProperties, source) {
      const _ref2 = yield source,
            type = _ref2.type,
            from = _ref2.from;

      if (type === 'css') {
        return Object.assign((yield customProperties), (yield getCustomPropertiesFromCSSFile(from)));
      }

      if (type === 'js') {
        return Object.assign((yield customProperties), (yield getCustomPropertiesFromJSFile(from)));
      }

      if (type === 'json') {
        return Object.assign((yield customProperties), (yield getCustomPropertiesFromJSONFile(from)));
      }

      return Object.assign((yield customProperties), (yield getCustomPropertiesFromObject((yield source))));
    });

    return function (_x4, _x5) {
      return _ref.apply(this, arguments);
    };
  }(), {});
}
/* Helper utilities
/* ========================================================================== */

const readFile = from => new Promise((resolve, reject) => {
  fs.readFile(from, 'utf8', (error, result) => {
    if (error) {
      reject(error);
    } else {
      resolve(result);
    }
  });
});

const readJSON =
/*#__PURE__*/
function () {
  var _ref3 = _asyncToGenerator(function* (from) {
    return JSON.parse((yield readFile(from)));
  });

  return function readJSON(_x6) {
    return _ref3.apply(this, arguments);
  };
}();

function transformValueAST(root, customProperties) {
  if (root.nodes && root.nodes.length) {
    root.nodes.slice().forEach(child => {
      if (isVarFunction(child)) {
        // eslint-disable-next-line no-unused-vars
        const _child$nodes$slice = child.nodes.slice(1, -1),
              propertyNode = _child$nodes$slice[0],
              comma = _child$nodes$slice[1],
              fallbacks = _child$nodes$slice.slice(2);

        const name = propertyNode.value;

        if (name in Object(customProperties)) {
          // conditionally replace a known custom property
          const nodes = asClonedArrayWithBeforeSpacing(customProperties[name], child.raws.before);
          child.replaceWith(...nodes);
          retransformValueAST({
            nodes
          }, customProperties, name);
        } else if (fallbacks.length) {
          // conditionally replace a custom property with a fallback
          const index = root.nodes.indexOf(child);

          if (index !== -1) {
            root.nodes.splice(index, 1, ...asClonedArrayWithBeforeSpacing(fallbacks, child.raws.before));
          }

          transformValueAST(root, customProperties);
        }
      } else {
        transformValueAST(child, customProperties);
      }
    });
  }

  return root;
} // retransform the current ast without a custom property (to prevent recursion)

function retransformValueAST(root, customProperties, withoutProperty) {
  const nextCustomProperties = Object.assign({}, customProperties);
  delete nextCustomProperties[withoutProperty];
  return transformValueAST(root, nextCustomProperties);
} // match var() functions


const varRegExp = /^var$/i; // whether the node is a var() function

const isVarFunction = node => node.type === 'func' && varRegExp.test(node.value) && Object(node.nodes).length > 0; // return an array with its nodes cloned, preserving the raw


const asClonedArrayWithBeforeSpacing = (array, beforeSpacing) => {
  const clonedArray = asClonedArray(array, null);

  if (clonedArray[0]) {
    clonedArray[0].raws.before = beforeSpacing;
  }

  return clonedArray;
}; // return an array with its nodes cloned


const asClonedArray = (array, parent) => array.map(node => asClonedNode(node, parent)); // return a cloned node


const asClonedNode = (node, parent) => {
  const cloneNode = new node.constructor(node);

  for (const key in node) {
    if (key === 'parent') {
      cloneNode.parent = parent;
    } else if (Object(node[key]).constructor === Array) {
      cloneNode[key] = asClonedArray(node.nodes, cloneNode);
    } else if (Object(node[key]).constructor === Object) {
      cloneNode[key] = Object.assign({}, node[key]);
    }
  }

  return cloneNode;
};

var transformProperties = ((root, customProperties, opts) => {
  // walk decls that can be transformed
  root.walkDecls(decl => {
    if (isTransformableDecl(decl) && !isRuleIgnored(decl)) {
      const originalValue = decl.value;
      const valueAST = parse(originalValue);
      const value = String(transformValueAST(valueAST, customProperties)); // conditionally transform values that have changed

      if (value !== originalValue) {
        if (opts.preserve) {
          decl.cloneBefore({
            value
          });
        } else {
          decl.value = value;
        }
      }
    }
  });
}); // match custom properties

const customPropertyRegExp$1 = /^--[A-z][\w-]*$/; // match custom property inclusions

const customPropertiesRegExp = /(^|[^\w-])var\([\W\w]+\)/; // whether the declaration should be potentially transformed

const isTransformableDecl = decl => !customPropertyRegExp$1.test(decl.prop) && customPropertiesRegExp.test(decl.value);

/* Write Custom Properties to CSS File
/* ========================================================================== */

function writeCustomPropertiesToCssFile(_x, _x2) {
  return _writeCustomPropertiesToCssFile.apply(this, arguments);
}
/* Write Custom Properties to JSON file
/* ========================================================================== */


function _writeCustomPropertiesToCssFile() {
  _writeCustomPropertiesToCssFile = _asyncToGenerator(function* (to, customProperties) {
    const cssContent = Object.keys(customProperties).reduce((cssLines, name) => {
      cssLines.push(`\t${name}: ${customProperties[name]};`);
      return cssLines;
    }, []).join('\n');
    const css = `:root {\n${cssContent}\n}\n`;
    yield writeFile(to, css);
  });
  return _writeCustomPropertiesToCssFile.apply(this, arguments);
}

function writeCustomPropertiesToJsonFile(_x3, _x4) {
  return _writeCustomPropertiesToJsonFile.apply(this, arguments);
}
/* Write Custom Properties to Common JS file
/* ========================================================================== */


function _writeCustomPropertiesToJsonFile() {
  _writeCustomPropertiesToJsonFile = _asyncToGenerator(function* (to, customProperties) {
    const jsonContent = JSON.stringify({
      'custom-properties': customProperties
    }, null, '  ');
    const json = `${jsonContent}\n`;
    yield writeFile(to, json);
  });
  return _writeCustomPropertiesToJsonFile.apply(this, arguments);
}

function writeCustomPropertiesToCjsFile(_x5, _x6) {
  return _writeCustomPropertiesToCjsFile.apply(this, arguments);
}
/* Write Custom Properties to Module JS file
/* ========================================================================== */


function _writeCustomPropertiesToCjsFile() {
  _writeCustomPropertiesToCjsFile = _asyncToGenerator(function* (to, customProperties) {
    const jsContents = Object.keys(customProperties).reduce((jsLines, name) => {
      jsLines.push(`\t\t'${escapeForJS(name)}': '${escapeForJS(customProperties[name])}'`);
      return jsLines;
    }, []).join(',\n');
    const js = `module.exports = {\n\tcustomProperties: {\n${jsContents}\n\t}\n};\n`;
    yield writeFile(to, js);
  });
  return _writeCustomPropertiesToCjsFile.apply(this, arguments);
}

function writeCustomPropertiesToMjsFile(_x7, _x8) {
  return _writeCustomPropertiesToMjsFile.apply(this, arguments);
}
/* Write Custom Properties to Exports
/* ========================================================================== */


function _writeCustomPropertiesToMjsFile() {
  _writeCustomPropertiesToMjsFile = _asyncToGenerator(function* (to, customProperties) {
    const mjsContents = Object.keys(customProperties).reduce((mjsLines, name) => {
      mjsLines.push(`\t'${escapeForJS(name)}': '${escapeForJS(customProperties[name])}'`);
      return mjsLines;
    }, []).join(',\n');
    const mjs = `export const customProperties = {\n${mjsContents}\n};\n`;
    yield writeFile(to, mjs);
  });
  return _writeCustomPropertiesToMjsFile.apply(this, arguments);
}

function writeCustomPropertiesToExports(customProperties, destinations) {
  return Promise.all(destinations.map(
  /*#__PURE__*/
  function () {
    var _ref = _asyncToGenerator(function* (destination) {
      if (destination instanceof Function) {
        yield destination(defaultCustomPropertiesToJSON(customProperties));
      } else {
        // read the destination as an object
        const opts = destination === Object(destination) ? destination : {
          to: String(destination)
        }; // transformer for Custom Properties into a JSON-compatible object

        const toJSON = opts.toJSON || defaultCustomPropertiesToJSON;

        if ('customProperties' in opts) {
          // write directly to an object as customProperties
          opts.customProperties = toJSON(customProperties);
        } else if ('custom-properties' in opts) {
          // write directly to an object as custom-properties
          opts['custom-properties'] = toJSON(customProperties);
        } else {
          // destination pathname
          const to = String(opts.to || ''); // type of file being written to

          const type = (opts.type || path.extname(opts.to).slice(1)).toLowerCase(); // transformed Custom Properties

          const customPropertiesJSON = toJSON(customProperties);

          if (type === 'css') {
            yield writeCustomPropertiesToCssFile(to, customPropertiesJSON);
          }

          if (type === 'js') {
            yield writeCustomPropertiesToCjsFile(to, customPropertiesJSON);
          }

          if (type === 'json') {
            yield writeCustomPropertiesToJsonFile(to, customPropertiesJSON);
          }

          if (type === 'mjs') {
            yield writeCustomPropertiesToMjsFile(to, customPropertiesJSON);
          }
        }
      }
    });

    return function (_x9) {
      return _ref.apply(this, arguments);
    };
  }()));
}
/* Helper utilities
/* ========================================================================== */

const defaultCustomPropertiesToJSON = customProperties => {
  return Object.keys(customProperties).reduce((customPropertiesJSON, key) => {
    customPropertiesJSON[key] = String(customProperties[key]);
    return customPropertiesJSON;
  }, {});
};

const writeFile = (to, text) => new Promise((resolve, reject) => {
  fs.writeFile(to, text, error => {
    if (error) {
      reject(error);
    } else {
      resolve();
    }
  });
});

const escapeForJS = string => string.replace(/\\([\s\S])|(')/g, '\\$1$2').replace(/\n/g, '\\n').replace(/\r/g, '\\r');

var index = postcss.plugin('postcss-custom-properties', opts => {
  // whether to preserve custom selectors and rules using them
  const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : true; // sources to import custom selectors from

  const importFrom = [].concat(Object(opts).importFrom || []); // destinations to export custom selectors to

  const exportTo = [].concat(Object(opts).exportTo || []); // promise any custom selectors are imported

  const customPropertiesPromise = getCustomPropertiesFromImports(importFrom); // synchronous transform

  const syncTransform = root => {
    const customProperties = getCustomPropertiesFromRoot(root, {
      preserve
    });
    transformProperties(root, customProperties, {
      preserve
    });
  }; // asynchronous transform


  const asyncTransform =
  /*#__PURE__*/
  function () {
    var _ref = _asyncToGenerator(function* (root) {
      const customProperties = Object.assign({}, (yield customPropertiesPromise), getCustomPropertiesFromRoot(root, {
        preserve
      }));
      yield writeCustomPropertiesToExports(customProperties, exportTo);
      transformProperties(root, customProperties, {
        preserve
      });
    });

    return function asyncTransform(_x) {
      return _ref.apply(this, arguments);
    };
  }(); // whether to return synchronous function if no asynchronous operations are requested


  const canReturnSyncFunction = importFrom.length === 0 && exportTo.length === 0;
  return canReturnSyncFunction ? syncTransform : asyncTransform;
});

export default index;
//# sourceMappingURL=index.esm.mjs.map