@fangzhongya/icons 0.0.14 → 0.0.15

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.
@@ -1,77 +1,191 @@
1
+ import {
2
+ getIconifySVG,
3
+ icons
4
+ } from "../chunk-Y2JUP3JH.js";
5
+ import "../chunk-MOHILOKE.js";
1
6
  import "../chunk-MLKGABMK.js";
2
7
 
3
8
  // packages/vite/index2.ts
4
- var virtualModuleId = "virtual:fang-icon";
5
- function simpleFangIcon(options = {}) {
6
- const { type = "svg", directory = "./svg" } = options;
7
- const resolvedVirtualModuleId = "\0" + virtualModuleId;
8
- return {
9
- name: "simple-fang-icon",
10
- resolveId(id) {
11
- if (id === virtualModuleId) {
12
- return resolvedVirtualModuleId;
9
+ function parseAttributes(attrString) {
10
+ const attributes = { name: "" };
11
+ const attrRegex = /(?:@|:|v-bind:)?([\w-]+)(?:="([^"]*)"|='([^']*)')/g;
12
+ let match;
13
+ while ((match = attrRegex.exec(attrString)) !== null) {
14
+ const name = match[1];
15
+ if (name) {
16
+ const value = match[2] || match[3] || "";
17
+ if (name === "name") {
18
+ attributes.name = value;
19
+ } else if (value === "true" || value === "false") {
20
+ attributes[name] = value === "true";
21
+ } else if (!isNaN(Number(value)) && value !== "") {
22
+ attributes[name] = Number(value);
23
+ } else {
24
+ attributes[name] = value;
13
25
  }
14
- return void 0;
15
- },
16
- load(id) {
17
- if (id === resolvedVirtualModuleId) {
18
- return `
19
- import { defineComponent, h, defineAsyncComponent, resolveComponent, computed } from 'vue'
20
- import FangIcon from '@fangzhongya/icons/icon/index'
21
-
22
- export default defineComponent({
23
- props: {
24
- name: {
25
- type: [String, Object],
26
- required: true
27
- },
28
- type: {
29
- type: String,
30
- default: '${type}'
31
- },
32
- directory: {
33
- type: String,
34
- default: '${directory}'
35
26
  }
36
- },
37
- setup(props, { attrs }) {
38
- const IconComponent = computed(() => {
39
- const iconName = props.name;
40
- // \u5982\u679C\u76F4\u63A5\u4F20\u9012\u4E86\u7EC4\u4EF6\uFF0C\u76F4\u63A5\u4F7F\u7528
41
- if (typeof iconName === 'string' && props.type === 'svg') {
42
- // SVG \u7C7B\u578B\uFF1A\u4ECE @fangzhongya/icons \u5E93\u5BFC\u5165
43
- return defineAsyncComponent(() =>
44
- import(/* @vite-ignore */'node_modules/@fangzhongya/icons/vue/' + iconName)
45
- .then(module => module.default)
46
- .catch(() => {
47
- // \u5982\u679C\u5BFC\u5165\u5931\u8D25\uFF0C\u5C1D\u8BD5\u89E3\u6790\u7EC4\u4EF6
48
- const component = resolveComponent(iconName);
49
- if (component) {
50
- return component;
51
- }
52
- // \u5982\u679C\u7EC4\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8FD4\u56DE\u5360\u4F4D\u7B26
53
- return {
54
- setup: () => () => h('span', { class: 'icon-error' }, \`Icon \${iconName} not found\`)
55
- };
56
- })
57
- );
27
+ }
28
+ return attributes;
29
+ }
30
+ function hasStringNameAttribute(attrString) {
31
+ const nameRegex = /(?:^|\s)name="[^"]*"/i;
32
+ const singleQuoteRegex = /(?:^|\s)name='[^']*'/i;
33
+ return nameRegex.test(attrString) || singleQuoteRegex.test(attrString);
34
+ }
35
+ function isOnlyWhitespace(content) {
36
+ return !content || /^\s*$/.test(content);
37
+ }
38
+ function findComponents(code, name) {
39
+ const components = [];
40
+ for (const componentName of name) {
41
+ let currentIndex = 0;
42
+ while (currentIndex < code.length) {
43
+ const startTagIndex = code.indexOf(
44
+ `<${componentName}`,
45
+ currentIndex
46
+ );
47
+ if (startTagIndex === -1) break;
48
+ let tagEndIndex = code.indexOf(">", startTagIndex);
49
+ if (tagEndIndex === -1) break;
50
+ const startTag = code.slice(startTagIndex, tagEndIndex + 1);
51
+ const attrString = startTag.slice(componentName.length + 1, -1).trim();
52
+ if (!hasStringNameAttribute(attrString)) {
53
+ currentIndex = startTagIndex + 1;
54
+ continue;
58
55
  }
59
- return iconName;
60
- });
61
-
62
- return () => {
63
- return h(FangIcon, {
64
- ...attrs,
65
- type: props.type,
66
- name: IconComponent.value,
67
- directory: props.directory
56
+ const attributes = parseAttributes(attrString);
57
+ if (!attributes.name) {
58
+ currentIndex = startTagIndex + 1;
59
+ continue;
60
+ }
61
+ const isSelfClosing = startTag.endsWith("/>");
62
+ let endIndex;
63
+ let fullMatch;
64
+ if (isSelfClosing) {
65
+ endIndex = tagEndIndex + 1;
66
+ fullMatch = startTag;
67
+ } else {
68
+ const endTag = `</${componentName}>`;
69
+ let endTagIndex = code.indexOf(endTag, tagEndIndex + 1);
70
+ if (endTagIndex === -1) {
71
+ currentIndex = startTagIndex + 1;
72
+ continue;
73
+ }
74
+ const content = code.slice(tagEndIndex + 1, endTagIndex);
75
+ if (!isOnlyWhitespace(content)) {
76
+ currentIndex = startTagIndex + 1;
77
+ continue;
78
+ }
79
+ endIndex = endTagIndex + endTag.length;
80
+ fullMatch = code.slice(startTagIndex, endIndex);
81
+ }
82
+ components.push({
83
+ componentName,
84
+ fullMatch,
85
+ start: startTagIndex,
86
+ end: endIndex,
87
+ attributes,
88
+ isSelfClosing
68
89
  });
69
- };
90
+ currentIndex = endIndex;
91
+ }
70
92
  }
71
- })
72
- `;
93
+ return components;
94
+ }
95
+ function getText(iconName, match) {
96
+ const svg = getIconifySVG(icons, iconName, 'v-bind="scope"');
97
+ const s = match.fullMatch.replace(`name="${iconName}"`, "").replace(`name='${iconName}'`, "").replace(`</${match.componentName}>`, "");
98
+ const text = `${s}<template #default="scope">${svg}</template></${match.componentName}>`;
99
+ return { text, imptext: "" };
100
+ }
101
+ function replaceComponent(match, customReplacement) {
102
+ const { attributes } = match;
103
+ const iconName = attributes.name;
104
+ if (customReplacement) {
105
+ return customReplacement(iconName, attributes);
106
+ }
107
+ let svgContent = icons.icons[iconName];
108
+ if (!svgContent) {
109
+ return { text: match.fullMatch, imptext: "" };
110
+ }
111
+ if (!attributes.type || attributes.type === "svg") {
112
+ if (true) {
113
+ return getText(iconName, match);
114
+ } else {
115
+ return getVue(iconName, match);
116
+ }
117
+ }
118
+ return { text: match.fullMatch, imptext: "" };
119
+ }
120
+ function shouldProcess(id) {
121
+ const validExtensions = [".vue", ".jsx", ".tsx", ".js", ".ts"];
122
+ const hasValidExtension = validExtensions.some((ext) => id.endsWith(ext));
123
+ if (!hasValidExtension) return false;
124
+ if (id.includes("/node_modules/")) return false;
125
+ return true;
126
+ }
127
+ function simpleFangIcon(options) {
128
+ const { name = ["FangIcon"], customReplacement } = options;
129
+ const targetComponents = Array.isArray(name) ? name : [name];
130
+ return {
131
+ name: "vite-plugin-fang-icon-replacer",
132
+ // 使用 enforce 确保在 Vue 插件之前执行
133
+ enforce: "pre",
134
+ // 转换代码
135
+ transform(code, id) {
136
+ if (!shouldProcess(id)) {
137
+ return null;
138
+ }
139
+ try {
140
+ const components = findComponents(code, targetComponents);
141
+ if (components.length === 0) {
142
+ return null;
143
+ }
144
+ let transformedCode = code;
145
+ let replacedCount = 0;
146
+ const imports = /* @__PURE__ */ new Set();
147
+ for (let i = components.length - 1; i >= 0; i--) {
148
+ const component = components[i];
149
+ if (component) {
150
+ const { text, imptext } = replaceComponent(
151
+ component,
152
+ customReplacement
153
+ );
154
+ imports.add(imptext);
155
+ if (text !== component.fullMatch) {
156
+ transformedCode = transformedCode.slice(0, component.start) + text + transformedCode.slice(component.end);
157
+ replacedCount++;
158
+ }
159
+ }
160
+ }
161
+ if (/.vue$/.test(id)) {
162
+ const i = transformedCode.indexOf("</script>");
163
+ transformedCode = transformedCode.slice(0, i) + [...imports].join(";") + transformedCode.slice(i);
164
+ } else {
165
+ transformedCode = [...imports].join(";") + transformedCode;
166
+ }
167
+ if (replacedCount > 0) {
168
+ return {
169
+ code: transformedCode,
170
+ map: null
171
+ };
172
+ }
173
+ } catch (error) {
174
+ console.error(`\u5904\u7406\u6587\u4EF6 ${id} \u65F6\u51FA\u9519:`, error);
73
175
  }
74
176
  return null;
177
+ },
178
+ // 处理热更新
179
+ handleHotUpdate({
180
+ file,
181
+ server
182
+ }) {
183
+ if (shouldProcess(file)) {
184
+ server.ws.send({
185
+ type: "full-reload"
186
+ });
187
+ return [];
188
+ }
75
189
  }
76
190
  };
77
191
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@fangzhongya/icons",
3
3
  "private": false,
4
4
  "type": "module",
5
- "version": "0.0.14",
5
+ "version": "0.0.15",
6
6
  "description ": "个人图标库",
7
7
  "author": "fangzhongya ",
8
8
  "license": "MIT",