-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tree-wide): optimize and clean up svgs (#693)
- Loading branch information
Showing
23 changed files
with
424 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.