Last active
September 15, 2019 22:22
-
-
Save incodemode/398af472a62de6f0deb711046220e990 to your computer and use it in GitHub Desktop.
font awesome optimizer
This file contains hidden or 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
/* Instrutions | |
1.- Download fontawesom js separate files (solid, regular, brands...) | |
2.- Edit the variables accordingly | |
3.- Run the script to get the stripped js (repeat when needed) */ | |
/* USES FIND-IN-FILE */ | |
var fif = require('find-in-files'); | |
var fs = require('fs'); | |
/* CONFIG VARS */ | |
var initiators = [ | |
'brands', | |
'regular', | |
'solid' | |
]; | |
var path = 'resources/fontawesome/fontawesome-';// where solid.js should be | |
// as in resources/fontawesome/fontawesome-solid.js note that it is more of a prefix | |
//paths where the files using the fa-name classes, notice that this is resource intensive. | |
var searchPaths = [ | |
'resources/views', | |
'resources/js', | |
'resources/external/gijgo-combined-1.9.11/js', | |
]; | |
/* MAIN CODE */ | |
// function for adding fa-names to the allowed array from directory files recursivelly | |
async function searchFiles(dir, allowed){ | |
return new Promise(function(resolve, reject){ | |
fif.findSync(/fa-[-a-z0-9]+/, dir).then(function(ans){ | |
for(fileMatchesIdx in ans){ | |
fileMatches = ans[fileMatchesIdx]; | |
for(var idx in fileMatches.matches){ | |
allowed[fileMatches.matches[idx].substr(3)] = true; | |
} | |
}; | |
resolve(allowed); | |
})}) | |
} | |
//app start | |
async function app(){ | |
//fill allowed from searchPaths | |
var allowed = []; | |
for(var idx in searchPaths){ | |
var searchPath = searchPaths[idx]; | |
allowed = await searchFiles(searchPath, allowed); | |
} | |
//show all allowed fa-names | |
console.log(allowed); | |
//for every file, rebuld it with -stripped at the end | |
for(var idx in initiators){ | |
var name = initiators[idx]; | |
var contents = fs.readFileSync(`${path}${name}.js`, 'utf8'); | |
var matchRegex = new RegExp(`(\{[^}]{500,}\})`); // regex simplification for future versions maybe | |
var match = contents.match(matchRegex); | |
var icons; | |
eval("icons = " + match[1]); | |
// icons now have the svgs | |
//some filtering here | |
var newIcons = {}; | |
for(icon in allowed){ | |
//var icon = allowed[iconIdx]; | |
if(typeof icons[icon]!= 'undefined'){ | |
newIcons[icon] = icons[icon]; | |
} | |
} | |
//encode and replace for the new file | |
var contentsStripped = contents.replace(matchRegex, JSON.stringify(newIcons)); | |
fs.writeFileSync(`${path}${name}-stripped.js`, contentsStripped); | |
console.log(`[done] ${name}`); | |
} | |
}; | |
app(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment