Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Upgrade SVGO to v3 #275

Open
wants to merge 2 commits into
base: master
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
package-lock.json
node_modules/
.DS_Store
node_modules/

desktop-output
mobile-output
82 changes: 44 additions & 38 deletions bin/optimize.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
const fs = require('fs-extra');
const glob = require('glob-promise');
const SVGO = require('svgo');
const progress = require('cli-progress');
const fs = require("fs-extra");
const glob = require("glob-promise");
const svgo = require("svgo");
const progress = require("cli-progress");

let options = {
plugins: [
{cleanupIDs: {remove: false}},
{removeStyleElement: true},
{removeUselessDefs: true},
{removeUselessStrokeAndFill: false},
{removeHiddenElems: true},
{removeEmptyText: true},
{convertShapeToPath: { convertArcs: true }},
{convertPathData: { noSpaceAfterFlags: false }},
{removeEmptyAttrs: true},
{removeEmptyContainers: true},
{mergePaths: false},
{removeTitle: true},
{removeDesc: true},
{removeDimensions: true},
{removeAttrs: {attrs: ['class', '(stroke)']}}
{
name: "cleanupIds",
params: { remove: false },
},
{ name: "removeAttrs", params: { attrs: ["class", "(stroke)"] } },
{ name: "convertShapeToPath", params: { convertArcs: true } },
{ name: "convertPathData", params: { noSpaceAfterFlags: false } },
"removeStyleElement",
"removeUselessDefs",
"removeHiddenElems",
"removeEmptyText",
"removeEmptyAttrs",
"removeEmptyContainers",
"removeTitle",
"removeDesc",
"removeDimensions",
],
multipass: true
multipass: true,
};

/**
* Reads an icon file off disk and optimizes it, saving to same location
* @param {string[]} filePaths array of relative file paths
* @param {SVGO} svgo SVGO instance with correct options
* @param {SingleBar} bar progress bar instance
* @param {string[]} filePaths array of relative file paths
* @param {svgoOptions} svgoOptions options to pass to the optimize function
* @param {SingleBar} bar progress bar instance
* @return {Promise}
*/
function optimizeIcons (filePaths, svgo, bar) {
function optimizeIcons(filePaths, svgoOptions, bar) {
var num = 0;
return Promise.all(filePaths.map((path) => {
return fs.readFile(path, 'utf-8')
.then((svg) => svgo.optimize(svg, { path }))
.then((result) => {
num++;
bar.update(num);
return fs.writeFile(path, result.data, 'utf-8');
});
}));
return Promise.all(
filePaths.map((path) => {
return fs
.readFile(path, "utf-8")
.then((svg) => svgo.optimize(svg, { ...svgoOptions, path }))
.then((result) => {
num++;
bar.update(num);
return fs.writeFile(path, result.data, "utf-8");
});
})
);
}

/**
Expand All @@ -54,15 +58,17 @@ module.exports = function (files, remove) {
if (!files) {
return Promise.resolve(true);
}
options.plugins[0] = {cleanupIDs: { remove }};
svgo = new SVGO(options);
options.plugins[0].params = { remove };
return glob(files).then((iconPaths) => {
const format = " \x1b[32m {bar} {percentage}% | {value}/{total} \x1b[0m";
const bar = new progress.SingleBar({ format }, progress.Presets.shades_classic);
const bar = new progress.SingleBar(
{ format },
progress.Presets.shades_classic
);
bar.start(iconPaths.length, 0);
return optimizeIcons(iconPaths, svgo, bar).then(() => {
return optimizeIcons(iconPaths, options, bar).then(() => {
bar.stop();
console.log("");
});
});
}
};
2 changes: 1 addition & 1 deletion docs/icons.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion icons/1-2x-24.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 icons/1-2x-32.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 icons/1-4x-24.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 icons/1-4x-32.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 icons/1-8x-24.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 icons/1-8x-32.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 icons/1x-16.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 icons/1x-32.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 icons/2d-explore-16.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 icons/2d-explore-24.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 icons/2d-explore-32.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 icons/2x-16.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 icons/2x-32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading