Skip to content

Commit

Permalink
bugfix: prefix svg ids with filename
Browse files Browse the repository at this point in the history
This will fix #8 because it will give each file an unique identifier for the respective background gradient.
  • Loading branch information
marcopixel committed Jul 15, 2020
1 parent a50ac4c commit 1232f6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
4 changes: 1 addition & 3 deletions scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ const PNG_CONFIG = {
height: 1500, // height in px
width: 1500 // width in px
};
const SVGO_OPTIONS =
"--multipass --enable=removeDimensions --disable=convertPathData,removeRasterImages";

export { ICON_DIR, SRC_DIR, OUTPUT_DIR, PNG_CONFIG, SVGO_OPTIONS };
export { ICON_DIR, SRC_DIR, OUTPUT_DIR, PNG_CONFIG };
28 changes: 24 additions & 4 deletions scripts/modules/optimizeSvg.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from "path";
import { promises as fs } from "fs";
import exec from "../util/exec";
import SVGO from "svgo";

import { ICON_DIR, OUTPUT_DIR, SVGO_OPTIONS } from "../config";
import { ICON_DIR, OUTPUT_DIR } from "../config";

export default async function optimizeSvg(iconObject: {}): Promise<void> {
// inform user that script has started
Expand Down Expand Up @@ -31,8 +31,28 @@ export default async function optimizeSvg(iconObject: {}): Promise<void> {
await fs.mkdir(outputPath, { recursive: true });
});

// execute svgo command
await exec(`svgo -i ${inputFile} -o ${outputPath} ${SVGO_OPTIONS}`);
// create svgo instance
const icon = new SVGO({
plugins: [
{ cleanupIDs: { prefix: `${op}-` } },
{
removeDimensions: true
},
{
convertPathData: true
},
{
removeRasterImages: false
}
]
});

// read file & execute svgo
const sourceFile = await fs.readFile(inputFile, "utf-8");
const optimizedIcon = await icon.optimize(sourceFile);

// write optimized icon to output path
await fs.writeFile(path.resolve(outputPath, `${op}.svg`), optimizedIcon.data);

// increase counter when finished and output it to the console
outputCount += 1;
Expand Down

0 comments on commit 1232f6b

Please sign in to comment.