mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 14:12:10 +08:00

Introduces PostCSS at the end of our CSS compilation pipeline. For now, just adds autoprefixer and light-dark polyfill.
50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
/* 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;
|