gmi-web/css.js

115 lines
3.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);
2021-02-20 21:22:37 +00:00
const schemes = rules.find(({ media }) => media)
? `<meta name="color-scheme" content="dark light">`
2021-02-20 19:28:43 +00:00
: "";
2021-02-20 21:22:37 +00:00
return `\n${schemes}\n<style>${stringify(
2021-02-20 19:28:43 +00:00
{
stylesheet: { rules: reduceVariables(rules, options) },
},
{ compress: true }
)}</style>`;
}
2021-02-20 20:16:36 +00:00
export const inline = (options) => {
const rules = load(options);
2021-02-20 21:22:37 +00:00
return function inlineCSS(tag) {
2021-02-20 21:57:13 +00:00
if ((options.body || options.inline) && options.css !== "none") {
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}"` : "";
} else {
return "";
}
2021-02-20 20:16:36 +00:00
};
};
2021-02-20 19:28:43 +00:00
export function load(options) {
2021-02-20 21:57:13 +00:00
if (options.css === "none") return [];
options.css = options.css
? options.css
: options.body
? "gmi.css"
: "gmi-web.css";
2021-02-20 19:28:43 +00:00
if (
2021-02-20 21:22:37 +00:00
["gmi.css", "gmi-web.css"].includes(options.css) ||
2021-02-20 19:28:43 +00:00
existsSync(options.css)
) {
2021-02-20 21:22:37 +00:00
const internal = (file) =>
2021-02-20 19:28:43 +00:00
path.resolve(path.dirname(new URL(import.meta.url).pathname), file);
2021-02-20 20:16:36 +00:00
return parse(
2021-02-20 21:22:37 +00:00
readFileSync(
path.resolve(
["gmi-web.css", "gmi.css"].includes(options.css)
? internal(options.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>.+))/;
2021-02-22 00:26:56 +00:00
const reduce = (rule) =>
Object.assign(
rule,
rule.declarations
? {
declarations: rule.declarations.map((declaration) => {
let { key, val } = CSS_VAR.exec(declaration.value).groups;
// only one level of variable referencing is supported
key =
CSS_VAR.exec(options[key] || defaultVariables[key]).groups
.key || key;
return Object.assign(declaration, {
value: key
2021-02-22 00:35:38 +00:00
? options[key.replace("--", "")] || defaultVariables[key]
2021-02-22 00:26:56 +00:00
: declaration.value,
});
}),
}
: {}
);
2021-02-20 19:28:43 +00:00
return rules
2021-02-22 00:26:56 +00:00
.map(reduce)
2021-02-20 19:28:43 +00:00
.map((rule) => {
2021-02-20 21:22:37 +00:00
if (!rule.media) return rule;
2021-02-20 19:28:43 +00:00
return Object.assign(rule, {
2021-02-22 00:26:56 +00:00
rules: rule.rules.map(reduce),
2021-02-20 19:28:43 +00:00
});
2021-02-20 21:22:37 +00:00
})
.filter(
({ selectors, media }) =>
media || (selectors && !selectors.includes(":root"))
);
2021-02-20 19:28:43 +00:00
}