postcss.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  3. var postcss = _interopDefault(require('postcss'));
  4. const mediaRegExp = /^media$/i;
  5. const prefersInterfaceRegExp = /\(\s*prefers-color-scheme\s*:\s*(dark|light|no-preference)\s*\)/i;
  6. const colorIndexByStyle = {
  7. dark: 48,
  8. light: 70,
  9. 'no-preference': 22
  10. };
  11. const prefersInterfaceReplacer = ($0, style) => `(color-index: ${colorIndexByStyle[style.toLowerCase()]})`;
  12. var postcss$1 = postcss.plugin('postcss-prefers-color-scheme', opts => {
  13. const preserve = 'preserve' in Object(opts) ? opts.preserve : true;
  14. return root => {
  15. root.walkAtRules(mediaRegExp, atRule => {
  16. const params = atRule.params;
  17. const altParams = params.replace(prefersInterfaceRegExp, prefersInterfaceReplacer);
  18. if (params !== altParams) {
  19. if (preserve) {
  20. atRule.cloneBefore({
  21. params: altParams
  22. });
  23. } else {
  24. atRule.params = altParams;
  25. }
  26. }
  27. });
  28. };
  29. });
  30. module.exports = postcss$1;