ceri-icon
Version:
webpack based - load only what you need - svg inline icons
60 lines (49 loc) • 1.6 kB
JavaScript
var Icons, VirtualModulePlugin, allIcons, ext, file, fs, i, iconPath, len, path, ref;
path = require("path");
fs = require("fs-extra");
ext = path.extname(__filename);
if (ext === ".coffee") {
require("coffeescript/register");
}
iconPath = require("./icon-path");
VirtualModulePlugin = require('virtual-module-webpack-plugin');
allIcons = {};
ref = fs.readdirSync(iconPath);
for (i = 0, len = ref.length; i < len; i++) {
file = ref[i];
allIcons[path.basename(file, ".json")] = require(path.resolve(iconPath, file));
}
module.exports = Icons = class Icons {
constructor(options) {
var alias, icon, iconName, iconset, iname, j, k, len1, len2, name, ref1, result, set, tmp;
result = {};
for (j = 0, len1 = options.length; j < len1; j++) {
name = options[j];
tmp = name.split("-");
set = tmp.shift();
tmp = tmp.join("-");
iconset = allIcons[set];
if ((iname = iconset.aliases[tmp]) == null) {
iname = tmp;
}
if ((icon = iconset.icons[iname]) != null) {
iconName = set + "-" + iname;
result[iconName] = icon;
if (icon.aliases != null) {
ref1 = icon.aliases;
for (k = 0, len2 = ref1.length; k < len2; k++) {
alias = ref1[k];
result[set + "-" + alias] = iconName;
}
delete icon.aliases;
}
} else {
throw new Error(`ceri-icon: icon '${name}' not found`);
}
}
return new VirtualModulePlugin({
path: path.resolve(__dirname, "_ceri-icon.json"),
contents: JSON.stringify(result)
});
}
};