Created
March 27, 2019 08:05
-
-
Save ehsavoie/323e9e2dc0d848a9538165789f1c09ed to your computer and use it in GitHub Desktop.
JS script to minimize an SVG image
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
var svgToMiniDataURI = require("mini-svg-data-uri"); | |
var http = require('http'); | |
var https = require('https'); | |
const url = process.argv[2]; | |
var client = http; | |
if (url.startsWith("https")){ | |
client = https; | |
} | |
client.get(url, (res) => { | |
const { statusCode } = res; | |
const contentType = res.headers['content-type']; | |
let error; | |
if (statusCode !== 200) { | |
error = new Error('Request Failed.\n' + | |
`Status Code: ${statusCode}`); | |
} | |
if (error) { | |
console.error(error.message); | |
// consume response data to free up memory | |
res.resume(); | |
return; | |
} | |
res.setEncoding('utf8'); | |
let rawData = ''; | |
res.on('data', (chunk) => { rawData += chunk; }); | |
res.on('end', () => { | |
try { | |
var optimizedSVGDataURI = svgToMiniDataURI(rawData); | |
console.log(optimizedSVGDataURI); | |
} catch (e) { | |
console.error(e.message); | |
} | |
}); | |
}).on('error', (e) => { | |
console.error(`Got error: ${e.message}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment