Last active
August 10, 2022 09:37
-
-
Save CCXXXI/008abfcaf73cc4317daf3fc084f656f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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