postcss.mjs 935 B

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