Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add svgo #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export default {
// Callback function that is called when the script is generating the icon name
// This is useful if you want to modify the icon name before it is written to the file
iconNameTransformer: (iconName) => iconName
// Svgo, defaults to { enabled: true }
svgo: { options: { plugins: [] } }
}),
],
};
Expand Down
98 changes: 92 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"dependencies": {
"chalk": "^4.1.2",
"glob": "^10.3.12",
"node-html-parser": "^6.1.13"
"node-html-parser": "^6.1.13",
"svgo": "^3.3.2"
},
"peerDependencies": {
"vite": ">=5.2.0"
Expand All @@ -77,12 +78,12 @@
"@vitest/coverage-v8": "^1.5.2",
"eslint": "8.56",
"eslint-plugin-unused-imports": "^3.1.0",
"happy-dom": "^14.7.1",
"husky": "^9.0.11",
"npm-run-all": "^4.1.5",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
"happy-dom": "^14.7.1",
"husky": "^9.0.11",
"vitest": "^1.5.2",
"vite": "5.2.11"
"vite": "5.2.11",
"vitest": "^1.5.2"
}
}
}
25 changes: 24 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chalk from "chalk";
import type { Plugin } from "vite";
import { normalizePath } from "vite";
import { mkdir } from "node:fs/promises";
import { optimize, type Config as SvgoConfig } from "svgo";

interface PluginProps {
withTypes?: boolean;
Expand All @@ -16,6 +17,10 @@ interface PluginProps {
fileName?: string;
cwd?: string;
iconNameTransformer?: (fileName: string) => string;
svgo?: {
enabled?: boolean;
options?: SvgoConfig;
};
}

const generateIcons = async ({
Expand All @@ -26,6 +31,7 @@ const generateIcons = async ({
cwd,
fileName = "sprite.svg",
iconNameTransformer,
svgo,
}: PluginProps) => {
const cwdToUse = cwd ?? process.cwd();
const inputDirRelative = path.relative(cwdToUse, inputDir);
Expand All @@ -46,6 +52,7 @@ const generateIcons = async ({
outputPath: path.join(outputDir, fileName),
outputDirRelative,
iconNameTransformer,
svgo,
});

if (withTypes) {
Expand Down Expand Up @@ -80,18 +87,28 @@ async function generateSvgSprite({
outputPath,
outputDirRelative,
iconNameTransformer,
svgo,
}: {
files: string[];
inputDir: string;
outputPath: string;
outputDirRelative?: string;
iconNameTransformer?: (fileName: string) => string;
svgo?: {
enabled?: boolean;
options?: SvgoConfig;
};
Comment on lines +97 to +100
Copy link

@silvenon silvenon Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for an alternative API:

Suggested change
svgo?: {
enabled?: boolean;
options?: SvgoConfig;
};
svgo?: boolean | SvgoConfig

to simplify, and also eliminate this impossible state:

{
  svgo: {
    enabled: false,
    options: {
      // ...
    }
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's a pretty good idea

}) {
// Each SVG becomes a symbol and we wrap them all in a single SVG
const symbols = await Promise.all(
files.map(async (file) => {
const fileName = transformIconName(file, iconNameTransformer ?? fileNameToCamelCase);
const input = await fs.readFile(path.join(inputDir, file), "utf8");
let input = await fs.readFile(path.join(inputDir, file), "utf8");
if (svgo?.enabled ?? true) {
input = await optimizeSvg(input, {
...svgo?.options,
});
}

const root = parse(input);
const svg = root.querySelector("svg");
Expand Down Expand Up @@ -121,6 +138,10 @@ async function generateSvgSprite({
return writeIfChanged(outputPath, output, `🖼️ Generated SVG spritesheet in ${chalk.green(outputDirRelative)}`);
}

async function optimizeSvg(svg: string, svgoOptions?: SvgoConfig) {
return optimize(svg, svgoOptions).data;
}

async function generateTypes({ names, outputPath }: { names: string[]; outputPath: string }) {
const output = [
"// This file is generated by icon spritesheet generator",
Expand Down Expand Up @@ -169,6 +190,7 @@ export const iconsSpritesheet: (args: PluginProps) => any = ({
fileName,
cwd,
iconNameTransformer,
svgo,
}) => {
const iconGenerator = async () =>
generateIcons({
Expand All @@ -178,6 +200,7 @@ export const iconsSpritesheet: (args: PluginProps) => any = ({
typesOutputFile,
fileName,
iconNameTransformer,
svgo,
});
return {
name: "icon-spritesheet-generator",
Expand Down
5 changes: 2 additions & 3 deletions test-apps/remix-vite/app/icons/sprite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test-apps/remix-vite/app/icons/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const iconNames = [
"Test",
"De",
"C",
"BasicShapes",
"B",
"A",
] as const
Expand Down
19 changes: 19 additions & 0 deletions test-apps/remix-vite/icons/basic-shapes.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test-apps/remix-vite/icons/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.