2021-12-11 20:36:36 +00:00
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import type { IconifyJSON } from '@iconify/types';
|
2022-01-10 11:17:05 +00:00
|
|
|
import type { FullIconifyIcon } from '../icon';
|
2022-01-10 11:21:34 +00:00
|
|
|
import { iconToSVG } from '../svg/build';
|
|
|
|
import { getIconData } from '../icon-set/get-icon';
|
2022-01-10 12:43:35 +00:00
|
|
|
import { mergeIconProps, tryInstallPkg } from './utils';
|
2021-12-11 20:36:36 +00:00
|
|
|
import createDebugger from 'debug';
|
|
|
|
import { isPackageExists, resolveModule } from 'local-pkg';
|
2022-01-10 11:17:05 +00:00
|
|
|
import { defaults as DefaultIconCustomizations } from '../customisations';
|
2022-01-10 12:43:35 +00:00
|
|
|
import type { IconCustomizations } from './types';
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2022-01-10 11:17:05 +00:00
|
|
|
const debug = createDebugger('@iconify-loader:icon');
|
|
|
|
const debugModern = createDebugger('@iconify-loader:modern');
|
|
|
|
const debugLegacy = createDebugger('@iconify-loader:legacy');
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2021-12-11 20:36:36 +00:00
|
|
|
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
|
|
|
const isLegacyExists = isPackageExists('@iconify/json');
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2022-01-10 16:08:40 +00:00
|
|
|
export async function loadCollection(
|
|
|
|
name: string,
|
|
|
|
autoInstall = false
|
|
|
|
): Promise<IconifyJSON | undefined> {
|
2021-12-11 20:36:36 +00:00
|
|
|
if (!_collections[name]) {
|
|
|
|
_collections[name] = task();
|
|
|
|
}
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2021-12-11 20:36:36 +00:00
|
|
|
return _collections[name];
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
async function task(): Promise<IconifyJSON | undefined> {
|
2021-12-11 20:36:36 +00:00
|
|
|
let jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
|
|
|
if (jsonPath) {
|
|
|
|
debugModern(name);
|
|
|
|
}
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
if (!jsonPath && isLegacyExists) {
|
2021-12-11 20:36:36 +00:00
|
|
|
jsonPath = resolveModule(`@iconify/json/json/${name}.json`);
|
|
|
|
if (jsonPath) {
|
|
|
|
debugLegacy(name);
|
|
|
|
}
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!jsonPath && !isLegacyExists && autoInstall) {
|
2021-12-11 20:36:36 +00:00
|
|
|
await tryInstallPkg(`@iconify-json/${name}`);
|
|
|
|
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (jsonPath) {
|
2021-12-11 20:36:36 +00:00
|
|
|
return JSON.parse(await fs.readFile(jsonPath, 'utf8'));
|
2022-01-10 16:08:40 +00:00
|
|
|
} else {
|
2021-12-11 20:36:36 +00:00
|
|
|
debugModern(`failed to load ${name}`);
|
|
|
|
return undefined;
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 12:43:35 +00:00
|
|
|
export async function searchForIcon(
|
2021-12-12 10:08:44 +00:00
|
|
|
iconSet: IconifyJSON,
|
|
|
|
collection: string,
|
|
|
|
ids: string[],
|
2022-01-10 16:08:40 +00:00
|
|
|
iconCustomizations?: IconCustomizations
|
2022-01-10 12:43:35 +00:00
|
|
|
): Promise<string | undefined> {
|
2021-12-11 20:36:36 +00:00
|
|
|
let iconData: FullIconifyIcon | null;
|
2022-01-10 16:08:40 +00:00
|
|
|
const {
|
|
|
|
customize,
|
|
|
|
additionalProps = {},
|
|
|
|
iconCustomizer,
|
|
|
|
} = iconCustomizations || {};
|
2021-12-09 21:12:00 +00:00
|
|
|
for (const id of ids) {
|
2021-12-11 20:36:36 +00:00
|
|
|
iconData = getIconData(iconSet, id, true);
|
2021-12-09 21:12:00 +00:00
|
|
|
if (iconData) {
|
2021-12-11 20:36:36 +00:00
|
|
|
debug(`${collection}:${id}`);
|
2022-01-10 16:08:40 +00:00
|
|
|
const defaultCustomizations = { ...DefaultIconCustomizations };
|
2021-12-12 10:08:44 +00:00
|
|
|
const { attributes, body } = iconToSVG(
|
|
|
|
iconData,
|
2022-01-10 16:08:40 +00:00
|
|
|
typeof customize === 'function'
|
|
|
|
? customize(defaultCustomizations)
|
|
|
|
: defaultCustomizations
|
2021-12-12 10:08:44 +00:00
|
|
|
);
|
2022-01-10 12:43:35 +00:00
|
|
|
return await mergeIconProps(
|
|
|
|
`<svg>${body}</svg>`,
|
|
|
|
collection,
|
|
|
|
id,
|
|
|
|
additionalProps,
|
|
|
|
() => attributes,
|
2022-01-10 16:08:40 +00:00
|
|
|
iconCustomizer
|
|
|
|
);
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|