custom.cjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. const createDebugger = require('debug');
  3. const loader_utils = require('./utils.cjs');
  4. const svg_trim = require('../svg/trim.cjs');
  5. require('../svg/build.cjs');
  6. require('../icon/defaults.cjs');
  7. require('../customisations/defaults.cjs');
  8. require('../svg/size.cjs');
  9. require('../svg/defs.cjs');
  10. function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
  11. const createDebugger__default = /*#__PURE__*/_interopDefaultCompat(createDebugger);
  12. const debug = createDebugger__default("@iconify-loader:custom");
  13. async function getCustomIcon(custom, collection, icon, options) {
  14. let result;
  15. debug(`${collection}:${icon}`);
  16. try {
  17. if (typeof custom === "function") {
  18. result = await custom(icon);
  19. } else {
  20. const inline = custom[icon];
  21. result = typeof inline === "function" ? await inline() : inline;
  22. }
  23. } catch (err) {
  24. console.warn(
  25. `Failed to load custom icon "${icon}" in "${collection}":`,
  26. err
  27. );
  28. return;
  29. }
  30. if (result) {
  31. const cleanupIdx = result.indexOf("<svg");
  32. if (cleanupIdx > 0)
  33. result = result.slice(cleanupIdx);
  34. const { transform } = options?.customizations ?? {};
  35. result = typeof transform === "function" ? await transform(result, collection, icon) : result;
  36. if (!result.startsWith("<svg")) {
  37. console.warn(
  38. `Custom icon "${icon}" in "${collection}" is not a valid SVG`
  39. );
  40. return result;
  41. }
  42. return await loader_utils.mergeIconProps(
  43. options?.customizations?.trimCustomSvg === true ? svg_trim.trimSVG(result) : result,
  44. collection,
  45. icon,
  46. options,
  47. void 0
  48. );
  49. }
  50. }
  51. exports.getCustomIcon = getCustomIcon;