Skip to content

Commit

Permalink
fix svgo + vite config
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Chong committed Sep 2, 2024
1 parent 58d6cb8 commit 413ac10
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
13 changes: 4 additions & 9 deletions packages/components/svgo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import {
const {
removeRootSVGElement,
replaceAttrKeys,
replaceColor,
replaceId,
} from "./svgoUtils"
} = require("./svgoUtils")

export type SVGOItem = {
type SVGOItem = {
type: "element"
name: string
attributes: { [key: string]: string }
children: SVGOItem[]
}

export type RootItem = {
type: string
children: SVGOItem[]
}

describe("removeRootSVGElement", () => {
const svgChildren: SVGOItem[] = [
{
Expand All @@ -40,7 +35,7 @@ describe("removeRootSVGElement", () => {
}

it("Replaces the <svg> element with its children", () => {
const rootItem: RootItem = {
const rootItem = {
type: "root",
children: [svgElement],
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Certain attributes work in html, but not React.
// If an attribute needs to be converted to its React counterpart,

import { RootItem, SVGOItem } from "./svgo.spec"

// it can be added here.
export const attrKeysToReplace = [
const attrKeysToReplace = [
{
original: "xlink:href",
replacement: "href",
Expand Down Expand Up @@ -35,7 +32,7 @@ export const attrKeysToReplace = [
},
]

export const replaceAttrKeys = (child: SVGOItem): void => {
const replaceAttrKeys = child => {
attrKeysToReplace.forEach(({ original, replacement }) => {
if (child.attributes.hasOwnProperty(original)) {
child.attributes[replacement] = child.attributes[original]
Expand All @@ -46,7 +43,7 @@ export const replaceAttrKeys = (child: SVGOItem): void => {

// Figma/ Sketch don't allow us to set 'currentColor' when exporting SVGs.
// We have a convention to export the color as #000 or #0000000, then change it here.
export const replaceColor = (child: SVGOItem): void => {
const replaceColor = child => {
const hexCodesToReplace = [
"#000",
"#000000",
Expand All @@ -65,7 +62,7 @@ export const replaceColor = (child: SVGOItem): void => {
// Ids aren't necessarily unique when coming from figma, causing
// Icon components to reference different, already-rendered Icons
// with duplicate ids. This function allows us to set unique ids.
export const replaceId = (child: SVGOItem): void => {
const replaceId = child => {
if (child.attributes.id) {
child.attributes.id = "UNIQUE_ID"
}
Expand All @@ -76,7 +73,7 @@ export const replaceId = (child: SVGOItem): void => {

// We remove the outermost <svg> element here, because we wrap its children in
// our custom SVG component later
export const removeRootSVGElement = (item: RootItem): void => {
const removeRootSVGElement = item => {
if (item.type !== "root") return

const hasSingleSVGChild =
Expand All @@ -89,7 +86,7 @@ export const removeRootSVGElement = (item: RootItem): void => {

// Recurses through all elements and child elements of root item
// Note: first item is not an element, it's children are the elements.
export const recurse = (item: RootItem): void => {
const recurse = item => {
item.children.forEach(child => {
replaceAttrKeys(child)
replaceColor(child)
Expand All @@ -98,3 +95,11 @@ export const recurse = (item: RootItem): void => {
recurse(child)
})
}

module.exports = {
recurse,
removeRootSVGElement,
replaceAttrKeys,
replaceColor,
replaceId,
}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default {
),
},
test: {
include: ["**/*.spec.ts?(x)", "!**/*.jest.spec.ts?(x)"],
include: ["**/*.spec.ts?(x)"],
environment: "jsdom",
globals: true,
setupFiles: path.resolve(__dirname, "./vitest.setup.ts"),
Expand Down

0 comments on commit 413ac10

Please sign in to comment.