Skip to content

Instantly share code, notes, and snippets.

@CCXXXI
Last active August 10, 2022 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CCXXXI/008abfcaf73cc4317daf3fc084f656f0 to your computer and use it in GitHub Desktop.
Save CCXXXI/008abfcaf73cc4317daf3fc084f656f0 to your computer and use it in GitHub Desktop.
import glob from "glob";
import {readFile, writeFile} from 'fs';
import {optimize, OptimizedSvg} from "svgo";
glob("**/*.html", (err, files) => {
if (err) throw err;
files.forEach(file => {
readFile(file, 'utf8', (err, content) => {
if (err) throw err;
writeFile(file,
content.replace(/<svg[^>]*>/, (svg) => {
const opt = optimize(svg);
if ("data" in opt) {
return opt.data;
} else {
throw opt.error;
}
}),
(err) => {
if (err) throw err;
}
)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment