UNPKG

unplugin-iconify

Version:
218 lines (208 loc) 6.58 kB
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) { if (typeof require !== "undefined") return require.apply(this, arguments); throw Error('Dynamic require of "' + x + '" is not supported'); }); // src/index.ts import pathe2 from "pathe"; import { createUnplugin } from "unplugin"; // src/core/helpers/generate.ts import crypto from "crypto"; import { getIconCSS, getIconData } from "@iconify/utils"; import fse from "fs-extra"; import pathe from "pathe"; import { IconSet } from "@iconify/tools"; // src/core/log.ts import consola from "consola"; // src/core/constants.ts var PLUGIN_ABBR = "ui"; var PLUGIN_CSS_PREFIX = `unplugin-iconify-css/`; var IS_DEV = process.env.NODE_ENV === "development"; // src/core/log.ts var logger = consola.withTag(PLUGIN_ABBR).withDefaults({ level: 3 }); // src/core/helpers/loader.ts import { readFileSync } from "fs"; function locateIconSet(prefix) { try { const main = __require.resolve(`@iconify-json/${prefix}/icons.json`); const info = __require.resolve(`@iconify-json/${prefix}/info.json`); return { main, info }; } catch (e) { } try { const main = __require.resolve(`@iconify/json/json/${prefix}.json`); return { main }; } catch (e) { } } var cache = /* @__PURE__ */ Object.create(null); function loadIconSet(prefix, options) { var _a; let filename; const customIconSet = (_a = options.iconSets) == null ? void 0 : _a[prefix]; if (customIconSet) { switch (typeof customIconSet) { case "function": { const result = customIconSet(); if (options.iconSets) { options.iconSets[prefix] = result; } return result; } case "string": { filename = { main: customIconSet }; break; } default: return customIconSet; } } else { filename = locateIconSet(prefix); } if (!filename) { return; } const main = typeof filename === "string" ? filename : filename.main; if (cache[main]) { return cache[main]; } try { const result = JSON.parse(readFileSync(main, "utf8")); if (!result.info && typeof filename === "object" && filename.info) { result.info = JSON.parse(readFileSync(filename.info, "utf8")); } cache[main] = result; return result; } catch (e) { } } function ensureLoadIconSet(prefix, options) { const iconSet = loadIconSet(prefix, options); if (!iconSet) { throw new Error( `Cannot load icon set for "${prefix}". Install "@iconify-json/${prefix}" as dev dependency?` ); } return iconSet; } // src/core/helpers/generate.ts async function generateIconSetsCss(cssGenerator, options) { const { iconSets = {}, exportIcons, iconSelector, outputPath } = cssGenerator; const { onCssHashGenerated } = options || {}; let cssCode = ""; if (exportIcons) { if (Array.isArray(exportIcons)) { exportIcons.forEach((prefix) => { const iconSetJson = ensureLoadIconSet(prefix, { iconSets }); const iconSet = new IconSet(iconSetJson); iconSet.list(["icon", "variation", "alias"]).forEach((item) => { const targetIconifyIcon = getIconData(iconSetJson, item); if (!targetIconifyIcon) { return; } cssCode += getIconCSS(targetIconifyIcon, { iconSelector: iconSelector == null ? void 0 : iconSelector.replace("{prefix}", iconSet.prefix).replace("{name}", item) }); }); }); } else { Object.keys(exportIcons).forEach((prefix) => { const iconSetJson = ensureLoadIconSet(prefix, { iconSets }); const iconSet = new IconSet(iconSetJson); const iconNames = exportIcons[prefix]; const exportIconNames = iconNames instanceof RegExp ? iconSet.list(["icon", "variation", "alias"]).filter((item) => { return iconNames.test(item); }) : iconNames; exportIconNames.forEach((item) => { const targetIconifyIcon = getIconData(iconSetJson, item); if (!targetIconifyIcon) { return; } cssCode += getIconCSS(targetIconifyIcon, { iconSelector: iconSelector == null ? void 0 : iconSelector.replace("{prefix}", iconSet.prefix).replace("{name}", item) }); }); }); } } const getCssHash = () => { const cssHash = crypto.createHash("md5").update(cssCode.replace(/[ \n\t]/g, ""), "utf8").digest("hex").slice(0, 8); onCssHashGenerated == null ? void 0 : onCssHashGenerated(cssHash); logger.debug("generateIconSetsCss cssHash", cssHash); return cssHash; }; const normalizedPath = pathe.normalize( typeof outputPath === "string" ? outputPath : outputPath(getCssHash()) ); await fse.ensureDir(pathe.dirname(normalizedPath)); logger.debug("css output path", normalizedPath); await fse.writeFile(normalizedPath, cssCode, "utf-8"); } // src/index.ts var unpluginFactory = (options) => { const { debug } = options || {}; const cssHashes = {}; if (debug) { logger.level = 4; } return { name: "unplugin-iconify", buildStart: async () => { const { cssGenerators = [] } = options || {}; await Promise.all( cssGenerators.map((cssGenerator) => { return generateIconSetsCss(cssGenerator, { onCssHashGenerated: (cssHash) => { if (cssGenerator.id) { cssHashes[cssGenerator.id] = cssHash; } } }); }) ); }, resolveId(id) { if (id.startsWith(PLUGIN_CSS_PREFIX)) { logger.debug("Got resolveId", id); return id; } }, loadInclude(id) { return id.startsWith(PLUGIN_CSS_PREFIX); }, async load(id) { const normalizeId = pathe2.normalize(id); const cssGeneratorKey = id.replace(PLUGIN_CSS_PREFIX, ""); const cssHash = cssHashes[cssGeneratorKey]; logger.debug("Got load id", normalizeId); logger.debug("cssGeneratorKey", cssGeneratorKey, "cssHash", cssHash); return { code: ` // import { cssHash } from 'unplugin-iconify-css/<key>' export const cssHash = '${cssHash}' `, map: null }; } }; }; var unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory); var src_default = unplugin; export { unpluginFactory, unplugin, src_default };