refactor(tree-wide): optimize and clean up svgs (#693)
This commit is contained in:
1 parent
f9e3916bd0
commit
051354a2e9
23 files changed
+424
-182
No files matched your search
@@ -11,6 +11,7 @@ import { log } from "@/lint/logger.ts";
|
||||
import { verifyMetadata } from "@/lint/metadata.ts";
|
||||
import { lint } from "@/lint/stylelint.ts";
|
||||
import { getUserstylesData } from "@/utils.ts";
|
||||
import stylelintConfig from "../../.stylelintrc.js";
|
||||
|
||||
const flags = parseFlags(Deno.args, { boolean: ["fix"] });
|
||||
const subDir = flags._[0]?.toString() ?? "";
|
||||
@@ -54,7 +55,9 @@ for await (const entry of stylesheets) {
|
||||
);
|
||||
|
||||
// Lint with Stylelint.
|
||||
await lint(entry, content, flags.fix).catch(() => failed = true);
|
||||
await lint(entry, content, flags.fix, stylelintConfig).catch(() =>
|
||||
failed = true
|
||||
);
|
||||
}
|
||||
|
||||
if (await checkForMissingFiles() === false) failed = true;
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import stylelint from "npm:stylelint";
|
||||
import valueParser from "npm:postcss-value-parser";
|
||||
import { optimize } from "npm:svgo";
|
||||
|
||||
const {
|
||||
createPlugin,
|
||||
utils: { report, ruleMessages, validateOptions },
|
||||
} = stylelint;
|
||||
|
||||
const ruleName = "catppuccin/optimized-svgs";
|
||||
|
||||
const meta = {
|
||||
fixable: true,
|
||||
};
|
||||
|
||||
const messages = ruleMessages(ruleName, {
|
||||
rejected: () => `Unoptimized SVG detected`,
|
||||
});
|
||||
|
||||
/** @type {import('npm:stylelint').Rule} */
|
||||
const ruleFunction = (primary, _secondary, context) => {
|
||||
return (root, result) => {
|
||||
const validOptions = validateOptions(result, ruleName, {
|
||||
actual: primary,
|
||||
possible: [true],
|
||||
});
|
||||
|
||||
if (!validOptions) return;
|
||||
|
||||
root.walkRules((rule) => {
|
||||
for (const node of rule.nodes) {
|
||||
if (node.type === "rule") {
|
||||
for (const subnode of node.nodes) {
|
||||
if (subnode.type === "atrule" && subnode.name === "svg") {
|
||||
const parsed = valueParser(subnode.value);
|
||||
|
||||
if (
|
||||
parsed.nodes.length === 1 &&
|
||||
parsed.nodes[0].type === "function" &&
|
||||
parsed.nodes[0].value === "escape" &&
|
||||
parsed.nodes[0].nodes.length === 1 &&
|
||||
parsed.nodes[0].nodes[0].type === "string"
|
||||
) {
|
||||
const svg = parsed.nodes[0].nodes[0].value;
|
||||
const optimized = optimize(svg, {
|
||||
multipass: true,
|
||||
plugins: [
|
||||
"cleanupAttrs",
|
||||
"cleanupIds",
|
||||
"collapseGroups",
|
||||
"convertPathData",
|
||||
"convertTransform",
|
||||
"convertStyleToAttrs",
|
||||
"mergePaths",
|
||||
"removeComments",
|
||||
"removeUselessDefs",
|
||||
"removeScriptElement"
|
||||
],
|
||||
}).data;
|
||||
|
||||
if (optimized !== svg) {
|
||||
if (context.fix) {
|
||||
parsed.nodes[0].nodes[0].quote = "'";
|
||||
parsed.nodes[0].nodes[0].value = optimized;
|
||||
subnode.value = parsed.toString();
|
||||
} else {
|
||||
report({
|
||||
result,
|
||||
ruleName,
|
||||
message: messages.rejected(),
|
||||
node: subnode,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
ruleFunction.ruleName = ruleName;
|
||||
ruleFunction.messages = messages;
|
||||
ruleFunction.meta = meta;
|
||||
|
||||
export default createPlugin(ruleName, ruleFunction);
|
||||
@@ -10,9 +10,17 @@ import "npm:stylelint-config-recommended";
|
||||
import { REPO_ROOT } from "@/deps.ts";
|
||||
import { log } from "@/lint/logger.ts";
|
||||
|
||||
export const lint = (entry: WalkEntry, content: string, fix: boolean) =>
|
||||
stylelint.lint({ files: entry.path, fix })
|
||||
.then(({ results }) => {
|
||||
export const lint = (
|
||||
entry: WalkEntry,
|
||||
content: string,
|
||||
fix: boolean,
|
||||
config: stylelint.Config,
|
||||
) =>
|
||||
stylelint.lint({ code: content, config, fix })
|
||||
.then(({ results, code }) => {
|
||||
if (code) {
|
||||
Deno.writeTextFileSync(entry.path, code);
|
||||
}
|
||||
results.map((result) => {
|
||||
result.warnings.map((warning) => {
|
||||
// Some cleanup for fancier logging - dims the rule name.
|
||||
|
||||
Reference in new issue
Block a user