@scarlab-icons/js 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -36,6 +36,8 @@ replace({
36
36
  })
37
37
  ```
38
38
 
39
+ > `replace()` includes every icon (~2.5 MB). Add [`@scarlab-icons/unplugin`](https://github.com/la-moore/scarlab-icons/tree/master/packages/unplugin) to your Vite or Rollup config and only the icons you use end up in the bundle, with no code changes.
40
+
39
41
  ### Data Attributes
40
42
 
41
43
  - `data-scarlab` - Icon name (required)
package/dist/index.cjs CHANGED
@@ -75,31 +75,50 @@ const DATA_ATTR_ALIASES = {
75
75
  function getIcon(name, style = DEFAULT_STYLE) {
76
76
  return icons.icons.find((icon) => icon.name === name && icon.style === style);
77
77
  }
78
+ const warned = new Set();
79
+ function warnMissing(key) {
80
+ if (warned.has(key) || typeof console === 'undefined')
81
+ return;
82
+ warned.add(key);
83
+ console.warn(`[scarlab-icons] Иконка "${key}" не найдена. ` +
84
+ 'Если вы используете @scarlab-icons/unplugin и имя вычисляется в рантайме, добавьте его в safelist.');
85
+ }
86
+ /**
87
+ * Собирает <svg> по атрибутам элемента-заглушки (data-scarlab, data-style и остальные).
88
+ * Возвращает undefined, если data-scarlab нет или иконка не найдена.
89
+ */
90
+ function attributesToSvg(attributes, params = {}) {
91
+ const list = Array.from(attributes);
92
+ const iconName = list.find((a) => a.name === 'data-scarlab')?.value;
93
+ const iconStyle = list.find((a) => a.name === 'data-style')?.value || DEFAULT_STYLE;
94
+ if (!iconName)
95
+ return undefined;
96
+ const icon = getIcon(iconName, iconStyle);
97
+ if (!icon) {
98
+ warnMissing(`${iconStyle}/${iconName}`);
99
+ return undefined;
100
+ }
101
+ const attrs = {};
102
+ for (const { name, value } of list) {
103
+ if (SERVICE_ATTRS.has(name))
104
+ continue;
105
+ attrs[DATA_ATTR_ALIASES[name] ?? name] = value;
106
+ }
107
+ return toSvg(icon, { ...params, ...attrs });
108
+ }
78
109
  function replaceByAttribute(params = {}, root = document) {
79
110
  const elements = root.querySelectorAll('[data-scarlab]');
80
111
  for (const element of elements) {
81
- const iconName = element.dataset.scarlab;
82
- const iconStyle = element.dataset.style || DEFAULT_STYLE;
83
- const icon = iconName ? getIcon(iconName, iconStyle) : undefined;
84
- if (!icon)
85
- continue;
86
- const attrs = {};
87
- for (const attribute of Array.from(element.attributes)) {
88
- const name = attribute.name;
89
- if (SERVICE_ATTRS.has(name))
90
- continue;
91
- attrs[DATA_ATTR_ALIASES[name] ?? name] = attribute.value;
92
- }
93
- element.outerHTML = toSvg(icon, {
94
- ...params,
95
- ...attrs,
96
- });
112
+ const svg = attributesToSvg(element.attributes, params);
113
+ if (svg)
114
+ element.outerHTML = svg;
97
115
  }
98
116
  }
99
117
  function replace(params = {}, root) {
100
118
  replaceByAttribute(params, root);
101
119
  }
102
120
 
121
+ exports.attributesToSvg = attributesToSvg;
103
122
  exports.getIcon = getIcon;
104
123
  exports.replace = replace;
105
124
  exports.replaceByAttribute = replaceByAttribute;
package/dist/index.d.ts CHANGED
@@ -3,9 +3,18 @@ import { IconNode } from '@scarlab-icons/icons';
3
3
  type Attrs = Record<string, string | number | boolean | null | undefined>;
4
4
  declare function toSvg(icon: IconNode, attrs?: Attrs): string;
5
5
 
6
+ interface ElementAttribute {
7
+ name: string;
8
+ value: string;
9
+ }
6
10
  declare function getIcon(name: string, style?: string): IconNode | undefined;
11
+ /**
12
+ * Собирает <svg> по атрибутам элемента-заглушки (data-scarlab, data-style и остальные).
13
+ * Возвращает undefined, если data-scarlab нет или иконка не найдена.
14
+ */
15
+ declare function attributesToSvg(attributes: Iterable<ElementAttribute>, params?: Attrs): string | undefined;
7
16
  declare function replaceByAttribute(params?: Attrs, root?: ParentNode): void;
8
17
  declare function replace(params?: Attrs, root?: ParentNode): void;
9
18
 
10
- export { getIcon, replace, replaceByAttribute, toSvg };
11
- export type { Attrs };
19
+ export { attributesToSvg, getIcon, replace, replaceByAttribute, toSvg };
20
+ export type { Attrs, ElementAttribute };
package/dist/index.js CHANGED
@@ -73,29 +73,47 @@ const DATA_ATTR_ALIASES = {
73
73
  function getIcon(name, style = DEFAULT_STYLE) {
74
74
  return icons.find((icon) => icon.name === name && icon.style === style);
75
75
  }
76
+ const warned = new Set();
77
+ function warnMissing(key) {
78
+ if (warned.has(key) || typeof console === 'undefined')
79
+ return;
80
+ warned.add(key);
81
+ console.warn(`[scarlab-icons] Иконка "${key}" не найдена. ` +
82
+ 'Если вы используете @scarlab-icons/unplugin и имя вычисляется в рантайме, добавьте его в safelist.');
83
+ }
84
+ /**
85
+ * Собирает <svg> по атрибутам элемента-заглушки (data-scarlab, data-style и остальные).
86
+ * Возвращает undefined, если data-scarlab нет или иконка не найдена.
87
+ */
88
+ function attributesToSvg(attributes, params = {}) {
89
+ const list = Array.from(attributes);
90
+ const iconName = list.find((a) => a.name === 'data-scarlab')?.value;
91
+ const iconStyle = list.find((a) => a.name === 'data-style')?.value || DEFAULT_STYLE;
92
+ if (!iconName)
93
+ return undefined;
94
+ const icon = getIcon(iconName, iconStyle);
95
+ if (!icon) {
96
+ warnMissing(`${iconStyle}/${iconName}`);
97
+ return undefined;
98
+ }
99
+ const attrs = {};
100
+ for (const { name, value } of list) {
101
+ if (SERVICE_ATTRS.has(name))
102
+ continue;
103
+ attrs[DATA_ATTR_ALIASES[name] ?? name] = value;
104
+ }
105
+ return toSvg(icon, { ...params, ...attrs });
106
+ }
76
107
  function replaceByAttribute(params = {}, root = document) {
77
108
  const elements = root.querySelectorAll('[data-scarlab]');
78
109
  for (const element of elements) {
79
- const iconName = element.dataset.scarlab;
80
- const iconStyle = element.dataset.style || DEFAULT_STYLE;
81
- const icon = iconName ? getIcon(iconName, iconStyle) : undefined;
82
- if (!icon)
83
- continue;
84
- const attrs = {};
85
- for (const attribute of Array.from(element.attributes)) {
86
- const name = attribute.name;
87
- if (SERVICE_ATTRS.has(name))
88
- continue;
89
- attrs[DATA_ATTR_ALIASES[name] ?? name] = attribute.value;
90
- }
91
- element.outerHTML = toSvg(icon, {
92
- ...params,
93
- ...attrs,
94
- });
110
+ const svg = attributesToSvg(element.attributes, params);
111
+ if (svg)
112
+ element.outerHTML = svg;
95
113
  }
96
114
  }
97
115
  function replace(params = {}, root) {
98
116
  replaceByAttribute(params, root);
99
117
  }
100
118
 
101
- export { getIcon, replace, replaceByAttribute, toSvg };
119
+ export { attributesToSvg, getIcon, replace, replaceByAttribute, toSvg };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scarlab-icons/js",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "A set of free MIT-licensed SVG icons.",
5
5
  "repository": "https://github.com/la-moore/scarlab-icons",
6
6
  "homepage": "https://scarlab-icons.la-moore.ru",