DEV: Introduce postcss for autoprefix and light-dark() polyfill (#31393)

Introduces PostCSS at the end of our CSS compilation pipeline. For now,
just adds autoprefixer and light-dark polyfill.
This commit is contained in:
David Taylor
2025-02-20 14:40:27 +00:00
committed by GitHub
parent 1b33a9900f
commit 087e8e4bdb
11 changed files with 273 additions and 44 deletions

View File

@ -0,0 +1,49 @@
/* eslint-disable no-bitwise */
// Simple, fast hashing function (djb2)
const hashString = (str) => {
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) + hash + str.charCodeAt(i);
hash = hash >>> 0; // Convert to unsigned 32-bit integer
}
return hash.toString(16).slice(0, 8);
};
/**
* Adds a hash of the current file as a prefix to variables
* introduced by the light-dark polyfill. This ensures that
* usage across different files cannot conflict.
*/
const namespacesToPrefix = ["--csstools-light-dark-toggle-"];
export default function postcssVariablePrefixer() {
let hash;
return {
postcssPlugin: "postcss-var-prefixer",
prepare(result) {
hash = hashString(result.root.source.input.css);
},
Declaration(declaration) {
const prop = declaration.prop;
for (const prefix of namespacesToPrefix) {
if (declaration.prop.startsWith(prefix)) {
declaration.prop = `--${hash}-${prop.slice(2)}`;
}
if (declaration.value.includes(prefix)) {
declaration.value = declaration.value.replaceAll(
prefix,
`--${hash}-${prefix.slice(2)}`
);
}
}
},
};
}
postcssVariablePrefixer.postcss = true;