mirror of
https://github.com/iconify/iconify.git
synced 2025-01-26 08:38:30 +00:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
|
import { getIconsCSSData } from '@iconify/utils/lib/css/icons';
|
||
|
import { loadIconSet } from './loader';
|
||
|
import { getIconNames } from './names';
|
||
|
import type { CleanIconifyPluginOptions } from './options';
|
||
|
|
||
|
/**
|
||
|
* Get CSS rules for icons list
|
||
|
*/
|
||
|
export function getCSSRulesForIcons(
|
||
|
icons: string[] | string,
|
||
|
options: CleanIconifyPluginOptions = {}
|
||
|
): Record<string, Record<string, string>> {
|
||
|
const rules = Object.create(null) as Record<string, Record<string, string>>;
|
||
|
|
||
|
// Get all icons
|
||
|
const prefixes = getIconNames(icons);
|
||
|
|
||
|
// Parse all icon sets
|
||
|
for (const prefix in prefixes) {
|
||
|
const iconSet = loadIconSet(prefix, options);
|
||
|
if (!iconSet) {
|
||
|
throw new Error(`Cannot load icon set for "${prefix}"`);
|
||
|
}
|
||
|
const generated = getIconsCSSData(
|
||
|
iconSet,
|
||
|
Array.from(prefixes[prefix]),
|
||
|
options
|
||
|
);
|
||
|
|
||
|
const result = generated.common
|
||
|
? [generated.common, ...generated.css]
|
||
|
: generated.css;
|
||
|
result.forEach((item) => {
|
||
|
const selector =
|
||
|
item.selector instanceof Array
|
||
|
? item.selector.join(', ')
|
||
|
: item.selector;
|
||
|
rules[selector] = item.rules;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return rules;
|
||
|
}
|