modern.cjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. const svg_build = require('../svg/build.cjs');
  3. const iconSet_getIcon = require('../icon-set/get-icon.cjs');
  4. const svg_size = require('../svg/size.cjs');
  5. const loader_utils = require('./utils.cjs');
  6. const createDebugger = require('debug');
  7. const customisations_defaults = require('../customisations/defaults.cjs');
  8. require('../icon/defaults.cjs');
  9. require('../svg/defs.cjs');
  10. require('../icon/merge.cjs');
  11. require('../icon/transformations.cjs');
  12. require('../icon-set/tree.cjs');
  13. function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
  14. const createDebugger__default = /*#__PURE__*/_interopDefaultCompat(createDebugger);
  15. const debug = createDebugger__default("@iconify-loader:icon");
  16. async function searchForIcon(iconSet, collection, ids, options) {
  17. let iconData;
  18. const { customize } = options?.customizations ?? {};
  19. for (const id of ids) {
  20. iconData = iconSet_getIcon.getIconData(iconSet, id);
  21. if (iconData) {
  22. debug(`${collection}:${id}`);
  23. let defaultCustomizations = {
  24. ...customisations_defaults.defaultIconCustomisations
  25. };
  26. if (typeof customize === "function") {
  27. iconData = Object.assign({}, iconData);
  28. defaultCustomizations = customize(
  29. defaultCustomizations,
  30. iconData,
  31. `${collection}:${id}`
  32. ) ?? defaultCustomizations;
  33. }
  34. const {
  35. attributes: { width, height, ...restAttributes },
  36. body
  37. } = svg_build.iconToSVG(iconData, defaultCustomizations);
  38. const scale = options?.scale;
  39. return await loader_utils.mergeIconProps(
  40. // DON'T remove space on <svg >
  41. `<svg >${body}</svg>`,
  42. collection,
  43. id,
  44. options,
  45. () => {
  46. return { ...restAttributes };
  47. },
  48. (props) => {
  49. const check = (prop, defaultValue) => {
  50. const propValue = props[prop];
  51. let value;
  52. if (!svg_build.isUnsetKeyword(propValue)) {
  53. if (propValue) {
  54. return;
  55. }
  56. if (typeof scale === "number") {
  57. if (scale) {
  58. value = svg_size.calculateSize(
  59. // Base on result from iconToSVG() or 1em
  60. defaultValue ?? "1em",
  61. scale
  62. );
  63. }
  64. } else {
  65. value = defaultValue;
  66. }
  67. }
  68. if (!value) {
  69. delete props[prop];
  70. } else {
  71. props[prop] = value;
  72. }
  73. };
  74. check("width", width);
  75. check("height", height);
  76. }
  77. );
  78. }
  79. }
  80. }
  81. exports.searchForIcon = searchForIcon;