Update SVG optimiser script

This commit is contained in:
jeremy@jermolene.com 2021-04-16 09:20:39 +01:00
parent ac022ec79f
commit caec6bc3fe
1 changed files with 43 additions and 41 deletions

View File

@ -5,52 +5,52 @@ Optimise the SVGs in ./core/images using SVGO from https://github.com/svg/svgo
Install SVGO with the following command in the root of the repo: Install SVGO with the following command in the root of the repo:
npm install svgo npm install svgo@2.3.0
*/ */
"use strict"; "use strict";
var fs = require("fs"), var fs = require("fs"),
path = require("path"), path = require("path"),
SVGO = require("svgo"), { optimize } = require("svgo"),
svgo = new SVGO({ config = {
plugins: [ plugins: [
{cleanupAttrs: true}, 'cleanupAttrs',
{removeDoctype: true}, 'removeDoctype',
{removeXMLProcInst: true}, 'removeXMLProcInst',
{removeComments: true}, 'removeComments',
{removeMetadata: true}, 'removeMetadata',
{removeTitle: true}, 'removeTitle',
{removeDesc: true}, 'removeDesc',
{removeUselessDefs: true}, 'removeUselessDefs',
{removeEditorsNSData: true}, 'removeEditorsNSData',
{removeEmptyAttrs: true}, 'removeEmptyAttrs',
{removeHiddenElems: true}, 'removeHiddenElems',
{removeEmptyText: true}, 'removeEmptyText',
{removeEmptyContainers: true}, 'removeEmptyContainers',
{removeViewBox: false}, // 'removeViewBox',
{cleanupEnableBackground: true}, 'cleanupEnableBackground',
{convertStyleToAttrs: true}, 'convertStyleToAttrs',
{convertColors: true}, 'convertColors',
{convertPathData: true}, 'convertPathData',
{convertTransform: true}, 'convertTransform',
{removeUnknownsAndDefaults: true}, 'removeUnknownsAndDefaults',
{removeNonInheritableGroupAttrs: true}, 'removeNonInheritableGroupAttrs',
{removeUselessStrokeAndFill: true}, 'removeUselessStrokeAndFill',
{removeUnusedNS: true}, 'removeUnusedNS',
{cleanupIDs: true}, 'cleanupIDs',
{cleanupNumericValues: true}, 'cleanupNumericValues',
{moveElemsAttrsToGroup: true}, 'moveElemsAttrsToGroup',
{moveGroupAttrsToElems: true}, 'moveGroupAttrsToElems',
{collapseGroups: true}, 'collapseGroups',
{removeRasterImages: false}, // 'removeRasterImages',
{mergePaths: true}, 'mergePaths',
{convertShapeToPath: true}, 'convertShapeToPath',
{sortAttrs: true}, 'sortAttrs',
{removeDimensions: false}, //'removeDimensions',
{removeAttrs: {attrs: "(stroke|fill)"}} {name: 'removeAttrs', params: { attrs: '(stroke|fill)' } }
] ]
}); };
var basepath = "./core/images/", var basepath = "./core/images/",
files = fs.readdirSync(basepath).sort(); files = fs.readdirSync(basepath).sort();
@ -66,12 +66,14 @@ files.forEach(function(filename) {
fakeSVG = body.join("\n"); fakeSVG = body.join("\n");
// A hack to make the new-journal-button work // A hack to make the new-journal-button work
fakeSVG = fakeSVG.replace("<<now \"DD\">>","&lt;&lt;now &quot;DD&quot;&gt;&gt;"); fakeSVG = fakeSVG.replace("<<now \"DD\">>","&lt;&lt;now &quot;DD&quot;&gt;&gt;");
svgo.optimize(fakeSVG, {path: filepath}).then(function(result) { config.path = filepath;
var result = optimize(fakeSVG,config);
if(result) {
var newSVG = header.join("\n") + "\n\n" + result.data.replace("&lt;&lt;now &quot;DD&quot;&gt;&gt;","<<now \"DD\">>"); var newSVG = header.join("\n") + "\n\n" + result.data.replace("&lt;&lt;now &quot;DD&quot;&gt;&gt;","<<now \"DD\">>");
fs.writeFileSync(filepath,newSVG); fs.writeFileSync(filepath,newSVG);
},function(err) { } else {
console.log("Error " + err + " with " + filename) console.log("Error " + err + " with " + filename)
process.exit(); process.exit();
}); };
} }
}); });