gmi-web/css.js

98 lines
3 KiB
JavaScript
Raw Normal View History

2021-02-20 19:28:43 +00:00
import { readFileSync, existsSync } from "fs";
2021-02-18 23:16:05 +00:00
// TODO import.meta.resolve is supposed to accomplish this without "path"
2021-02-15 20:56:41 +00:00
import path from "path";
import { stringify, parse } from "css";
2021-02-20 19:28:43 +00:00
export function style(options) {
if (options.inline || options.css === "none") return "";
const rules = load(options);
const schemes = rules.find(({ media }) => media === "preferes-color-scheme")
? `<meta name="color-scheme" content="dark light">\n`
: "";
return `<style>${stringify(
{
stylesheet: { rules: reduceVariables(rules, options) },
},
{ compress: true }
)}</style>`;
}
2021-02-20 20:16:36 +00:00
export const inline = (options) => {
const rules = load(options);
return (tag) => {
if (!options.inline || options.css === "none") return "";
const styles = reduceVariables(rules, options)
.filter(({ selectors }) => selectors && selectors.includes(tag))
.reduce((style, { declarations }) => {
declarations.forEach(({ property, value }) => {
style = `${style}${property}:${value};`;
});
return style;
}, "");
return styles !== "" ? ` style="${styles}"` : "";
};
};
2021-02-20 19:28:43 +00:00
export function load(options) {
options.css =
2021-02-20 20:16:36 +00:00
options.css ||
(!options.body || !options.inline ? "gmi-web.css" : "gmi.css");
console.log("load:", options.css);
2021-02-20 19:28:43 +00:00
if (
["gmi", "web", "gmi.css", "gmi-web.css"].includes(options.css) ||
existsSync(options.css)
) {
const packageRoot = (file) =>
path.resolve(path.dirname(new URL(import.meta.url).pathname), file);
2021-02-20 20:16:36 +00:00
return parse(
2021-02-20 19:28:43 +00:00
readFileSync(
path.resolve(
["gmi-web.css", "web"].includes(options.css)
? packageRoot("gmi-web.css")
: ["gmi.css", "gmi"].includes(options.css)
? packageRoot("gmi.css")
: resolve(options.css)
),
"utf-8"
)
).stylesheet.rules
} else {
2021-02-20 19:28:43 +00:00
throw new Error(`Cannot find file ${options.css}.`);
}
2021-02-15 20:56:41 +00:00
}
2021-02-20 19:28:43 +00:00
export function rootVariables(rules) {
const root = rules.find(
({ selectors }) => selectors && selectors.includes(":root")
);
if (!root) return {};
return root.declarations.reduce(
(obj, { property, value }) =>
!/^\-\-/.test(property) ? obj : Object.assign(obj, { [property]: value }),
{}
);
}
function reduceVariables(rules, options) {
const defaultVariables = rootVariables(rules);
const CSS_VAR = /(^var\((?<key>.+)\)|(?<val>.+))/;
return rules
.filter(({ selectors }) => selectors && !selectors.includes(":root"))
.map((rule) => {
return Object.assign(rule, {
declarations: rule.declarations.map((declaration) => {
let { key, val } = CSS_VAR.exec(declaration.value).groups;
// only one level of variable referencing is supported
2021-02-20 20:16:36 +00:00
key =
CSS_VAR.exec(options[key] || defaultVariables[key]).groups.key ||
key;
2021-02-20 19:28:43 +00:00
return Object.assign(declaration, {
2021-02-20 20:16:36 +00:00
value: key
? options[key] || defaultVariables[key]
: declaration.value,
2021-02-20 19:28:43 +00:00
});
}),
});
});
}