fix(css): scope utilities under the view root so tailwind plugins stop outranking each other
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// node scripts/css.mjs [--minify] [--watch]
|
||||
//
|
||||
// Tailwind writes to a scratch file; the scoped, optionally minified result
|
||||
// is what lands in styles.css. See scope-css.mjs for why the extra step.
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { readFileSync, watch, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { scopeCss } from "./scope-css.mjs";
|
||||
|
||||
const root = new URL("..", import.meta.url).pathname;
|
||||
const args = new Set(process.argv.slice(2));
|
||||
const minify = args.has("--minify");
|
||||
const watching = args.has("--watch");
|
||||
|
||||
const input = join(root, "src/tailwind.css");
|
||||
const scratch = join(root, ".tailwind.css");
|
||||
const output = join(root, "styles.css");
|
||||
const cli = join(root, "node_modules/.bin/tailwindcss");
|
||||
|
||||
function finish() {
|
||||
const built = readFileSync(scratch, "utf8");
|
||||
writeFileSync(output, scopeCss(built, { minify }));
|
||||
console.log(`css: wrote styles.css (${minify ? "minified, " : ""}scoped)`);
|
||||
}
|
||||
|
||||
if (watching) {
|
||||
writeFileSync(scratch, "");
|
||||
const child = spawn(cli, ["-i", input, "-o", scratch, "--watch"], {
|
||||
stdio: "inherit",
|
||||
});
|
||||
let timer = null;
|
||||
watch(scratch, () => {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(() => {
|
||||
timer = null;
|
||||
try {
|
||||
finish();
|
||||
} catch (error) {
|
||||
console.error("css:", error);
|
||||
}
|
||||
}, 60);
|
||||
});
|
||||
child.on("exit", (code) => process.exit(code ?? 0));
|
||||
} else {
|
||||
const run = spawnSync(cli, ["-i", input, "-o", scratch], {
|
||||
stdio: "inherit",
|
||||
});
|
||||
if (run.status !== 0) {
|
||||
process.exit(run.status ?? 1);
|
||||
}
|
||||
finish();
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { transform } from "lightningcss";
|
||||
|
||||
/**
|
||||
* Obsidian loads every plugin's stylesheet into one document, and Tailwind
|
||||
* puts its utilities into a cascade layer called `utilities` in each of them.
|
||||
* Layers with the same name merge, so another Tailwind plugin's
|
||||
* `.hidden{display:none!important}` and this plugin's
|
||||
* `@container … .\@min-\[30rem\]\:inline-block{…!important}` end up in the
|
||||
* same layer with the same specificity, and whichever plugin loaded last
|
||||
* wins. Prefixing every selector with `:is(.beaver-root, .beaver-root *)` adds a
|
||||
* class of specificity that no unscoped utility can match, whatever the load
|
||||
* order, while still reaching the view root itself and the portal layer,
|
||||
* both of which carry `.beaver-root`.
|
||||
*
|
||||
* Theme variables move from `:root, :host` onto `.beaver-root` for the same
|
||||
* reason: a later `:root{--spacing:…}` from someone else must not resize us.
|
||||
*/
|
||||
|
||||
const ROOT = "beaver-root";
|
||||
|
||||
const SCOPE = {
|
||||
kind: "is",
|
||||
selectors: [
|
||||
[{ name: ROOT, type: "class" }],
|
||||
[
|
||||
{ name: ROOT, type: "class" },
|
||||
{ type: "combinator", value: "descendant" },
|
||||
{ type: "universal" },
|
||||
],
|
||||
],
|
||||
type: "pseudo-class",
|
||||
};
|
||||
|
||||
function isPseudo(selector, kind) {
|
||||
return (
|
||||
selector.length === 1 &&
|
||||
selector[0].type === "pseudo-class" &&
|
||||
selector[0].kind === kind
|
||||
);
|
||||
}
|
||||
|
||||
function mentions(selector, name) {
|
||||
for (const part of selector) {
|
||||
if (part.type === "class" && part.name === name) {
|
||||
return true;
|
||||
}
|
||||
if (part.selectors?.some((inner) => mentions(inner, name))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function scopeSelector(selector) {
|
||||
if (isPseudo(selector, "root")) {
|
||||
return [{ name: ROOT, type: "class" }];
|
||||
}
|
||||
// `:root, :host` is one rule; the root alone carries it, so :host goes.
|
||||
if (isPseudo(selector, "host")) {
|
||||
return [];
|
||||
}
|
||||
if (mentions(selector, ROOT)) {
|
||||
return selector;
|
||||
}
|
||||
// `*` has to open a compound, and :is() takes that place now.
|
||||
const rest = selector[0]?.type === "universal" ? selector.slice(1) : selector;
|
||||
return [SCOPE, ...rest];
|
||||
}
|
||||
|
||||
/** Rewrites a built stylesheet so its rules only reach the plugin's own DOM. */
|
||||
export function scopeCss(code, { minify = false } = {}) {
|
||||
// The selector visitor, not the rule one: handing a whole rule back makes
|
||||
// lightningcss re-read its declarations, and some of Tailwind's it cannot.
|
||||
const result = transform({
|
||||
code: Buffer.from(code),
|
||||
filename: "styles.css",
|
||||
minify,
|
||||
visitor: { Selector: scopeSelector },
|
||||
});
|
||||
return result.code.toString();
|
||||
}
|
||||
Reference in New Issue
Block a user