@hamur/icon 0.0.0-stage → 0.0.2
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/LICENSE +22 -0
- package/README.md +14 -2
- package/dist/chunks/bun-react-fhd05zxm.js +206 -0
- package/dist/chunks/bun-react-nygte91t.js +305 -0
- package/dist/src/adapters/angular.js +104 -0
- package/dist/src/adapters/html.js +47 -0
- package/dist/src/adapters/preact.js +56 -0
- package/dist/src/adapters/react.js +66 -0
- package/dist/src/adapters/solid.js +97 -0
- package/dist/src/adapters/svelte.js +6 -0
- package/dist/src/adapters/vue.js +63 -0
- package/dist/src/index.js +897 -0
- package/dist/src/testing.js +46 -0
- package/dist/types/src/adapters/angular-icon.d.ts +32 -0
- package/dist/types/src/adapters/angular.d.ts +2 -0
- package/dist/types/src/adapters/html.d.ts +8 -0
- package/dist/types/src/adapters/preact.d.ts +6 -0
- package/dist/types/src/adapters/react.d.ts +6 -0
- package/dist/types/src/adapters/solid-icon.d.ts +6 -0
- package/dist/types/src/adapters/solid.d.ts +4 -0
- package/dist/types/src/adapters/svelte.d.ts +2 -0
- package/dist/types/src/adapters/vue.d.ts +41 -0
- package/dist/types/src/build/client.d.ts +21 -0
- package/dist/types/src/build/collections.d.ts +13 -0
- package/dist/types/src/build/css.d.ts +10 -0
- package/dist/types/src/build/custom.d.ts +10 -0
- package/dist/types/src/build/endpoint.d.ts +4 -0
- package/dist/types/src/build/runtime.d.ts +3 -0
- package/dist/types/src/build/scan.d.ts +37 -0
- package/dist/types/src/clean.d.ts +3 -0
- package/dist/types/src/index.d.ts +8 -0
- package/dist/types/src/kit.d.ts +10 -0
- package/dist/types/src/locate.d.ts +4 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/name.d.ts +13 -0
- package/dist/types/src/options.d.ts +46 -0
- package/dist/types/src/render.d.ts +68 -0
- package/dist/types/src/runtime/bundle.d.ts +2 -0
- package/dist/types/src/runtime/classes.d.ts +3 -0
- package/dist/types/src/runtime/icons.d.ts +27 -0
- package/dist/types/src/runtime/index.d.ts +2 -0
- package/dist/types/src/runtime/remote.d.ts +22 -0
- package/dist/types/src/runtime/server.d.ts +3 -0
- package/dist/types/src/server/door.d.ts +1 -0
- package/dist/types/src/server/endpoint.d.ts +28 -0
- package/dist/types/src/server/handler.d.ts +1 -0
- package/dist/types/src/testing.d.ts +2 -0
- package/dist/types/src/types.d.ts +102 -0
- package/package.json +141 -4
- package/src/adapters/Icon.svelte +60 -0
- package/src/adapters/angular-icon.ts +104 -0
- package/src/adapters/angular.ts +2 -0
- package/src/adapters/html.ts +44 -0
- package/src/adapters/preact.ts +63 -0
- package/src/adapters/react.ts +80 -0
- package/src/adapters/solid-icon.ts +99 -0
- package/src/adapters/solid.ts +8 -0
- package/src/adapters/svelte.ts +2 -0
- package/src/adapters/test/IconPage.svelte +5 -0
- package/src/adapters/test/support.ts +78 -0
- package/src/adapters/vue.ts +64 -0
- package/src/build/client.ts +113 -0
- package/src/build/collections.ts +86 -0
- package/src/build/css.ts +67 -0
- package/src/build/custom.ts +120 -0
- package/src/build/endpoint.ts +16 -0
- package/src/build/runtime.ts +24 -0
- package/src/build/scan.ts +148 -0
- package/src/clean.ts +66 -0
- package/src/env.d.ts +22 -0
- package/src/index.ts +10 -0
- package/src/kit.ts +29 -0
- package/src/locate.ts +20 -0
- package/src/module.ts +35 -0
- package/src/name.ts +38 -0
- package/src/options.ts +275 -0
- package/src/render.ts +247 -0
- package/src/runtime/bundle.ts +26 -0
- package/src/runtime/classes.ts +5 -0
- package/src/runtime/icons.ts +146 -0
- package/src/runtime/index.ts +17 -0
- package/src/runtime/remote.ts +147 -0
- package/src/runtime/server.ts +7 -0
- package/src/server/door.ts +1 -0
- package/src/server/endpoint.ts +251 -0
- package/src/server/handler.ts +30 -0
- package/src/testing.ts +23 -0
- package/src/types.ts +97 -0
|
@@ -0,0 +1,897 @@
|
|
|
1
|
+
// packages/icon/src/module.ts
|
|
2
|
+
import { existsSync as existsSync3 } from "node:fs";
|
|
3
|
+
import { defineModule, updateRuntimeConfig } from "hamur/kit";
|
|
4
|
+
|
|
5
|
+
// packages/icon/src/build/client.ts
|
|
6
|
+
import { join as join3 } from "node:path";
|
|
7
|
+
import { addTemplate, updateTemplates, useLogger } from "hamur/kit";
|
|
8
|
+
|
|
9
|
+
// packages/icon/src/render.ts
|
|
10
|
+
import { sanitizeSvg } from "hamur/adapter";
|
|
11
|
+
var SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
12
|
+
function resolveIcon(collection, name) {
|
|
13
|
+
const chain = [];
|
|
14
|
+
const seen = new Set;
|
|
15
|
+
let current = name;
|
|
16
|
+
while (!Object.hasOwn(collection.icons, current)) {
|
|
17
|
+
const alias = collection.aliases && Object.hasOwn(collection.aliases, current) ? collection.aliases[current] : undefined;
|
|
18
|
+
if (!alias || seen.has(current))
|
|
19
|
+
return;
|
|
20
|
+
seen.add(current);
|
|
21
|
+
chain.push(alias);
|
|
22
|
+
current = alias.parent;
|
|
23
|
+
}
|
|
24
|
+
const icon = collection.icons[current];
|
|
25
|
+
if (typeof icon.body !== "string")
|
|
26
|
+
return;
|
|
27
|
+
const box = {
|
|
28
|
+
left: icon.left ?? collection.left ?? 0,
|
|
29
|
+
top: icon.top ?? collection.top ?? 0,
|
|
30
|
+
width: icon.width ?? collection.width ?? 16,
|
|
31
|
+
height: icon.height ?? collection.height ?? 16
|
|
32
|
+
};
|
|
33
|
+
let rotate = icon.rotate ?? 0;
|
|
34
|
+
let hFlip = icon.hFlip === true;
|
|
35
|
+
let vFlip = icon.vFlip === true;
|
|
36
|
+
for (const alias of chain.reverse()) {
|
|
37
|
+
if (alias.left !== undefined)
|
|
38
|
+
box.left = alias.left;
|
|
39
|
+
if (alias.top !== undefined)
|
|
40
|
+
box.top = alias.top;
|
|
41
|
+
if (alias.width !== undefined)
|
|
42
|
+
box.width = alias.width;
|
|
43
|
+
if (alias.height !== undefined)
|
|
44
|
+
box.height = alias.height;
|
|
45
|
+
rotate += alias.rotate ?? 0;
|
|
46
|
+
if (alias.hFlip)
|
|
47
|
+
hFlip = !hFlip;
|
|
48
|
+
if (alias.vFlip)
|
|
49
|
+
vFlip = !vFlip;
|
|
50
|
+
}
|
|
51
|
+
return transform(icon.body, box, rotate, hFlip, vFlip);
|
|
52
|
+
}
|
|
53
|
+
var payloadIcon = (data) => ({
|
|
54
|
+
body: data.body,
|
|
55
|
+
width: data.width,
|
|
56
|
+
height: data.height,
|
|
57
|
+
...data.left ? { left: data.left } : {},
|
|
58
|
+
...data.top ? { top: data.top } : {}
|
|
59
|
+
});
|
|
60
|
+
function transform(body, box, rotate, hFlip, vFlip) {
|
|
61
|
+
const steps = [];
|
|
62
|
+
let turns = rotate;
|
|
63
|
+
if (hFlip && vFlip)
|
|
64
|
+
turns += 2;
|
|
65
|
+
else if (hFlip || vFlip) {
|
|
66
|
+
steps.push(hFlip ? `translate(${box.width + box.left} ${-box.top || 0})` : `translate(${-box.left || 0} ${box.height + box.top})`, hFlip ? "scale(-1 1)" : "scale(1 -1)");
|
|
67
|
+
box.left = 0;
|
|
68
|
+
box.top = 0;
|
|
69
|
+
}
|
|
70
|
+
turns = (turns % 4 + 4) % 4;
|
|
71
|
+
if (turns === 1) {
|
|
72
|
+
const center = box.height / 2 + box.top;
|
|
73
|
+
steps.unshift(`rotate(90 ${center} ${center})`);
|
|
74
|
+
} else if (turns === 2) {
|
|
75
|
+
steps.unshift(`rotate(180 ${box.width / 2 + box.left} ${box.height / 2 + box.top})`);
|
|
76
|
+
} else if (turns === 3) {
|
|
77
|
+
const center = box.width / 2 + box.left;
|
|
78
|
+
steps.unshift(`rotate(-90 ${center} ${center})`);
|
|
79
|
+
}
|
|
80
|
+
if (turns % 2 === 1) {
|
|
81
|
+
[box.left, box.top] = [box.top, box.left];
|
|
82
|
+
[box.width, box.height] = [box.height, box.width];
|
|
83
|
+
}
|
|
84
|
+
return { body: steps.length ? `<g transform="${steps.join(" ")}">${body}</g>` : body, ...box };
|
|
85
|
+
}
|
|
86
|
+
var SANITIZED_LIMIT = 500;
|
|
87
|
+
var sanitized = new Map;
|
|
88
|
+
function iconNodes(body) {
|
|
89
|
+
let nodes = sanitized.get(body);
|
|
90
|
+
if (nodes === undefined) {
|
|
91
|
+
try {
|
|
92
|
+
nodes = sanitizeSvg(body, "icon");
|
|
93
|
+
} catch {
|
|
94
|
+
nodes = [];
|
|
95
|
+
}
|
|
96
|
+
if (sanitized.size >= SANITIZED_LIMIT)
|
|
97
|
+
sanitized.delete(sanitized.keys().next().value);
|
|
98
|
+
} else
|
|
99
|
+
sanitized.delete(body);
|
|
100
|
+
sanitized.set(body, nodes);
|
|
101
|
+
return nodes;
|
|
102
|
+
}
|
|
103
|
+
function svgValue(value, uid) {
|
|
104
|
+
return typeof value === "string" ? value : value.map((part) => typeof part === "string" ? part : `${uid}-${part.id}`).join("");
|
|
105
|
+
}
|
|
106
|
+
function svgAttributes(node, uid) {
|
|
107
|
+
const attrs = {};
|
|
108
|
+
for (const [name, value] of Object.entries(node.attrs))
|
|
109
|
+
attrs[name] = svgValue(value, uid);
|
|
110
|
+
if (node.style)
|
|
111
|
+
attrs.style = node.style.map((declaration) => `${declaration.name}:${svgValue(declaration.value, uid)}`).join(";");
|
|
112
|
+
return attrs;
|
|
113
|
+
}
|
|
114
|
+
var ESCAPES = { "&": "&", "<": "<", ">": ">", '"': """ };
|
|
115
|
+
var escapeHtml = (text) => text.replace(/[&<>"]/g, (char) => ESCAPES[char]);
|
|
116
|
+
var ATTRIBUTE_NAME = /^[^\s"'<>/=]+$/;
|
|
117
|
+
var attributesMarkup = (attrs) => Object.entries(attrs).filter(([name]) => ATTRIBUTE_NAME.test(name)).map(([name, value]) => ` ${name}="${escapeHtml(value)}"`).join("");
|
|
118
|
+
function svgMarkup(nodes, uid) {
|
|
119
|
+
let out = "";
|
|
120
|
+
for (const node of nodes) {
|
|
121
|
+
if (typeof node === "string" || Array.isArray(node))
|
|
122
|
+
out += escapeHtml(svgValue(node, uid));
|
|
123
|
+
else
|
|
124
|
+
out += `<${node.tag}${attributesMarkup(svgAttributes(node, uid))}>${svgMarkup(node.children, uid)}</${node.tag}>`;
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
function iconRule(className, data, nodes, where) {
|
|
129
|
+
const markup = svgMarkup(nodes, "i");
|
|
130
|
+
const svg = `<svg xmlns="${SVG_NAMESPACE}" viewBox="${data.left} ${data.top} ${data.width} ${data.height}" width="${data.width}" height="${data.height}">${markup}</svg>`;
|
|
131
|
+
const selector = `.${className.replaceAll(":", "\\:")}`;
|
|
132
|
+
const paint = /currentcolor/i.test(markup) ? "background-color: currentColor; -webkit-mask: var(--svg) no-repeat center / 100% 100%; mask: var(--svg) no-repeat center / 100% 100%;" : "background: var(--svg) no-repeat center / 100% 100%;";
|
|
133
|
+
return `${where ? `:where(${selector})` : selector} { display: inline-block; width: 1em; height: 1em; ${paint} --svg: url("data:image/svg+xml,${encodeURIComponent(svg)}"); }`;
|
|
134
|
+
}
|
|
135
|
+
function iconStyle(rules, layer) {
|
|
136
|
+
const text = [...rules].join(`
|
|
137
|
+
`);
|
|
138
|
+
return layer === undefined ? text : `@layer ${layer} {
|
|
139
|
+
${text}
|
|
140
|
+
}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// packages/icon/src/build/custom.ts
|
|
144
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
145
|
+
import { join, relative, sep } from "node:path";
|
|
146
|
+
import { sanitizeSvg as sanitizeSvg2 } from "hamur/adapter";
|
|
147
|
+
|
|
148
|
+
// packages/icon/src/name.ts
|
|
149
|
+
var NAME_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
150
|
+
function pair(text) {
|
|
151
|
+
const at = text.indexOf(":");
|
|
152
|
+
if (at === -1)
|
|
153
|
+
return;
|
|
154
|
+
const prefix = text.slice(0, at);
|
|
155
|
+
const name = text.slice(at + 1);
|
|
156
|
+
return NAME_PATTERN.test(prefix) && NAME_PATTERN.test(name) ? { prefix, name } : undefined;
|
|
157
|
+
}
|
|
158
|
+
function normalizeName(input, prefixes, selectorPrefix = "i-") {
|
|
159
|
+
if (selectorPrefix !== "" && input.startsWith(selectorPrefix)) {
|
|
160
|
+
const rest = input.slice(selectorPrefix.length);
|
|
161
|
+
if (rest.includes(":")) {
|
|
162
|
+
const found = pair(rest);
|
|
163
|
+
if (found)
|
|
164
|
+
return found;
|
|
165
|
+
} else {
|
|
166
|
+
for (const prefix of [...prefixes].sort((a, b) => b.length - a.length)) {
|
|
167
|
+
const name = rest.slice(prefix.length + 1);
|
|
168
|
+
if (rest.startsWith(`${prefix}-`) && NAME_PATTERN.test(name))
|
|
169
|
+
return { prefix, name };
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return pair(input);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// packages/icon/src/build/custom.ts
|
|
177
|
+
import { DiagnosticError } from "hamur/kit";
|
|
178
|
+
var SVG_FILE = /\.svg$/i;
|
|
179
|
+
var DEFAULT_SIZE = 24;
|
|
180
|
+
var PAINT = new Set([
|
|
181
|
+
"fill",
|
|
182
|
+
"fill-opacity",
|
|
183
|
+
"fill-rule",
|
|
184
|
+
"clip-rule",
|
|
185
|
+
"stroke",
|
|
186
|
+
"stroke-width",
|
|
187
|
+
"stroke-linecap",
|
|
188
|
+
"stroke-linejoin",
|
|
189
|
+
"stroke-miterlimit",
|
|
190
|
+
"stroke-dasharray",
|
|
191
|
+
"stroke-dashoffset",
|
|
192
|
+
"stroke-opacity"
|
|
193
|
+
]);
|
|
194
|
+
var text = (value) => typeof value === "string" ? value : value.map((part) => typeof part === "string" ? part : part.id).join("");
|
|
195
|
+
function plain(nodes) {
|
|
196
|
+
return nodes.map((node) => {
|
|
197
|
+
if (typeof node === "string" || Array.isArray(node))
|
|
198
|
+
return text(node);
|
|
199
|
+
return {
|
|
200
|
+
tag: node.tag,
|
|
201
|
+
attrs: Object.fromEntries(Object.entries(node.attrs).map(([name, value]) => [name, text(value)])),
|
|
202
|
+
...node.style ? { style: node.style.map((declaration) => ({ name: declaration.name, value: text(declaration.value) })) } : {},
|
|
203
|
+
children: plain(node.children)
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
function box(attrs) {
|
|
208
|
+
const viewBox = attrs.viewBox === undefined ? [] : text(attrs.viewBox).trim().split(/[\s,]+/).map(Number);
|
|
209
|
+
if (viewBox.length === 4 && viewBox.every(Number.isFinite) && viewBox[2] > 0 && viewBox[3] > 0)
|
|
210
|
+
return { left: viewBox[0], top: viewBox[1], width: viewBox[2], height: viewBox[3] };
|
|
211
|
+
const size = (name) => {
|
|
212
|
+
const value = attrs[name] === undefined ? "" : text(attrs[name]);
|
|
213
|
+
const number = value.endsWith("%") ? Number.NaN : Number.parseFloat(value);
|
|
214
|
+
return Number.isFinite(number) && number > 0 ? number : DEFAULT_SIZE;
|
|
215
|
+
};
|
|
216
|
+
return { left: 0, top: 0, width: size("width"), height: size("height") };
|
|
217
|
+
}
|
|
218
|
+
function readSvgIcon(source, shown) {
|
|
219
|
+
const root = sanitizeSvg2(source, shown).find((node) => typeof node === "object" && !Array.isArray(node) && node.tag === "svg");
|
|
220
|
+
if (!root)
|
|
221
|
+
throw new DiagnosticError("HAMUR_E5905", "not an SVG file (no <svg> element)", { at: shown });
|
|
222
|
+
const { left, top, width, height } = box(root.attrs);
|
|
223
|
+
const paint = Object.entries(root.attrs).filter(([name]) => PAINT.has(name));
|
|
224
|
+
const children = paint.length ? [{ tag: "g", attrs: Object.fromEntries(paint), children: root.children }] : root.children;
|
|
225
|
+
return { body: svgMarkup(plain(children), ""), width, height, ...left ? { left } : {}, ...top ? { top } : {} };
|
|
226
|
+
}
|
|
227
|
+
function files(dir, recursive, segments = []) {
|
|
228
|
+
const found = [];
|
|
229
|
+
for (const name of readdirSync(dir).sort()) {
|
|
230
|
+
if (name.startsWith("."))
|
|
231
|
+
continue;
|
|
232
|
+
const file = join(dir, name);
|
|
233
|
+
if (statSync(file).isDirectory()) {
|
|
234
|
+
if (recursive)
|
|
235
|
+
found.push(...files(file, true, [...segments, name]));
|
|
236
|
+
} else if (SVG_FILE.test(name))
|
|
237
|
+
found.push({ file, segments: [...segments, name.replace(SVG_FILE, "")] });
|
|
238
|
+
}
|
|
239
|
+
return found;
|
|
240
|
+
}
|
|
241
|
+
function readCustomCollection(rootDir, entry) {
|
|
242
|
+
const icons = {};
|
|
243
|
+
const origin = {};
|
|
244
|
+
for (const { file, segments } of files(entry.dir, entry.recursive)) {
|
|
245
|
+
const shown = relative(rootDir, file).split(sep).join("/");
|
|
246
|
+
const name = segments.join("-");
|
|
247
|
+
if (!NAME_PATTERN.test(name))
|
|
248
|
+
throw new DiagnosticError("HAMUR_E5906", `icon name ${JSON.stringify(name)} must match ${NAME_PATTERN}`, {
|
|
249
|
+
at: shown
|
|
250
|
+
});
|
|
251
|
+
if (Object.hasOwn(icons, name))
|
|
252
|
+
throw new DiagnosticError("HAMUR_E5907", `icon name ${JSON.stringify(name)} is also the name of ${origin[name]}`, { at: shown });
|
|
253
|
+
icons[name] = readSvgIcon(readFileSync(file, "utf8"), shown);
|
|
254
|
+
origin[name] = shown;
|
|
255
|
+
}
|
|
256
|
+
return { prefix: entry.prefix, icons };
|
|
257
|
+
}
|
|
258
|
+
var isCustomFile = (entries, file) => SVG_FILE.test(file) ? entries.filter((entry) => !relative(entry.dir, file).startsWith("..") && relative(entry.dir, file) !== "") : [];
|
|
259
|
+
|
|
260
|
+
// packages/icon/src/build/scan.ts
|
|
261
|
+
import { readdirSync as readdirSync2, readFileSync as readFileSync2 } from "node:fs";
|
|
262
|
+
import { join as join2, relative as relative2, sep as sep2 } from "node:path";
|
|
263
|
+
var SCAN_EXTENSIONS = /\.(?:[cm]?[jt]sx?|vue|svelte|css|html|mdx?)$/;
|
|
264
|
+
var DECLARATIONS = /\.d\.[cm]?ts$/;
|
|
265
|
+
var LITERAL = /"([^"\\\n]*)"|'([^'\\\n]*)'|`([^`\\$]*)`/g;
|
|
266
|
+
var MARKUP_SEPARATORS = /[\s"'<>=]+/;
|
|
267
|
+
function* literalTokens(text, separator = /\s+/) {
|
|
268
|
+
for (const match of text.matchAll(LITERAL)) {
|
|
269
|
+
for (const token of (match[1] ?? match[2] ?? match[3] ?? "").split(separator))
|
|
270
|
+
if (token !== "")
|
|
271
|
+
yield token;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function scanText(text, prefixes, selectorPrefix) {
|
|
275
|
+
const found = new Set;
|
|
276
|
+
for (const token of literalTokens(text)) {
|
|
277
|
+
const name = normalizeName(token, prefixes, selectorPrefix);
|
|
278
|
+
if (name && prefixes.includes(name.prefix))
|
|
279
|
+
found.add(`${name.prefix}:${name.name}`);
|
|
280
|
+
}
|
|
281
|
+
return [...found];
|
|
282
|
+
}
|
|
283
|
+
function scanClassText(text, prefixes, selectorPrefix, stylesheet = false) {
|
|
284
|
+
const found = new Map;
|
|
285
|
+
const add = (token) => {
|
|
286
|
+
if (selectorPrefix === "" || !token.startsWith(selectorPrefix) || found.has(token))
|
|
287
|
+
return;
|
|
288
|
+
const name = normalizeName(token, prefixes, selectorPrefix);
|
|
289
|
+
if (name && prefixes.includes(name.prefix))
|
|
290
|
+
found.set(token, { class: token, name: `${name.prefix}:${name.name}` });
|
|
291
|
+
};
|
|
292
|
+
if (stylesheet) {
|
|
293
|
+
const escaped = selectorPrefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
294
|
+
for (const match of text.matchAll(new RegExp(`\\.(${escaped}[a-z0-9_-]+(?:\\\\:[a-z0-9_-]+)?)`, "gi")))
|
|
295
|
+
add(match[1].replace("\\", ""));
|
|
296
|
+
} else
|
|
297
|
+
for (const token of literalTokens(text, MARKUP_SEPARATORS))
|
|
298
|
+
add(token);
|
|
299
|
+
return [...found.values()];
|
|
300
|
+
}
|
|
301
|
+
function walk(dir, skip, out) {
|
|
302
|
+
let entries;
|
|
303
|
+
try {
|
|
304
|
+
entries = readdirSync2(dir, { withFileTypes: true });
|
|
305
|
+
} catch {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
for (const entry of entries) {
|
|
309
|
+
const path = join2(dir, entry.name);
|
|
310
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules" || skip.has(path))
|
|
311
|
+
continue;
|
|
312
|
+
if (entry.isDirectory())
|
|
313
|
+
walk(path, skip, out);
|
|
314
|
+
else if (SCAN_EXTENSIONS.test(entry.name) && !DECLARATIONS.test(entry.name))
|
|
315
|
+
out.push(path);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function findSources(roots, appRoot, rules) {
|
|
319
|
+
const include = rules.include.map((pattern) => new Bun.Glob(pattern));
|
|
320
|
+
const exclude = rules.exclude.map((pattern) => new Bun.Glob(pattern));
|
|
321
|
+
const seen = new Set;
|
|
322
|
+
const out = [];
|
|
323
|
+
for (const root of roots) {
|
|
324
|
+
const files = [];
|
|
325
|
+
for (const dir of root.dirs)
|
|
326
|
+
walk(dir, new Set(root.skip), files);
|
|
327
|
+
for (const file of files) {
|
|
328
|
+
if (seen.has(file))
|
|
329
|
+
continue;
|
|
330
|
+
const own = relative2(root.rootDir, file).split(sep2).join("/");
|
|
331
|
+
if (include.length > 0 && !include.some((glob) => glob.match(own)) || exclude.some((glob) => glob.match(own)))
|
|
332
|
+
continue;
|
|
333
|
+
seen.add(file);
|
|
334
|
+
out.push({ file, relative: relative2(appRoot, file).split(sep2).join("/") });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
function scanSources(sources, prefixes, selectorPrefix) {
|
|
340
|
+
const found = new Map;
|
|
341
|
+
for (const source of sources) {
|
|
342
|
+
let text;
|
|
343
|
+
try {
|
|
344
|
+
text = readFileSync2(source.file, "utf8");
|
|
345
|
+
} catch {
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
for (const name of scanText(text, prefixes, selectorPrefix))
|
|
349
|
+
if (!found.has(name))
|
|
350
|
+
found.set(name, source.relative);
|
|
351
|
+
}
|
|
352
|
+
return found;
|
|
353
|
+
}
|
|
354
|
+
function scanClassSources(sources, prefixes, selectorPrefix) {
|
|
355
|
+
const found = new Map;
|
|
356
|
+
for (const source of sources) {
|
|
357
|
+
let text;
|
|
358
|
+
try {
|
|
359
|
+
text = readFileSync2(source.file, "utf8");
|
|
360
|
+
} catch {
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
for (const entry of scanClassText(text, prefixes, selectorPrefix, source.file.endsWith(".css")))
|
|
364
|
+
if (!found.has(entry.class))
|
|
365
|
+
found.set(entry.class, { name: entry.name, file: source.relative });
|
|
366
|
+
}
|
|
367
|
+
return found;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// packages/icon/src/build/client.ts
|
|
371
|
+
import { DiagnosticError as DiagnosticError2 } from "hamur/kit";
|
|
372
|
+
var LISTED = "icon.clientBundle.icons";
|
|
373
|
+
function createClientBundle(names, collections) {
|
|
374
|
+
const payload = {};
|
|
375
|
+
const warnings = [];
|
|
376
|
+
for (const [full, where] of [...names].sort(([a], [b]) => a.localeCompare(b))) {
|
|
377
|
+
const at = full.indexOf(":");
|
|
378
|
+
const prefix = full.slice(0, at);
|
|
379
|
+
const name = full.slice(at + 1);
|
|
380
|
+
const collection = Object.hasOwn(collections, prefix) ? collections[prefix] : undefined;
|
|
381
|
+
const data = collection && resolveIcon(collection, name);
|
|
382
|
+
if (!data) {
|
|
383
|
+
warnings.push(`[icon] "${full}" (${where}) is not in the ${prefix} collection`);
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
(payload[prefix] ??= { icons: {} }).icons[name] = payloadIcon(data);
|
|
387
|
+
}
|
|
388
|
+
return { payload, warnings };
|
|
389
|
+
}
|
|
390
|
+
function bundleSizeError(payload, sizeLimitKb) {
|
|
391
|
+
const bytes = Buffer.byteLength(JSON.stringify(payload));
|
|
392
|
+
return bytes > sizeLimitKb * 1024 ? `icon client bundle is ${Math.ceil(bytes / 1024)} KB, over icon.clientBundle.sizeLimitKb ${sizeLimitKb}` : undefined;
|
|
393
|
+
}
|
|
394
|
+
function createWarner() {
|
|
395
|
+
const log = useLogger("icon");
|
|
396
|
+
const warned = new Set;
|
|
397
|
+
return (message) => {
|
|
398
|
+
if (warned.has(message))
|
|
399
|
+
return;
|
|
400
|
+
warned.add(message);
|
|
401
|
+
log.warn(message);
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
function scanRoots(hamur) {
|
|
405
|
+
const { rootDir, srcDir, serverDir, layers } = hamur.options;
|
|
406
|
+
return [
|
|
407
|
+
{ rootDir, dirs: [srcDir, join3(rootDir, "shared")], skip: [serverDir] },
|
|
408
|
+
...layers.map((layer) => ({
|
|
409
|
+
rootDir: layer.rootDir,
|
|
410
|
+
dirs: [layer.srcDir, join3(layer.rootDir, "shared")],
|
|
411
|
+
skip: [layer.serverDir]
|
|
412
|
+
}))
|
|
413
|
+
];
|
|
414
|
+
}
|
|
415
|
+
function setupClient(options, hamur, collections, warn = createWarner()) {
|
|
416
|
+
const { clientBundle } = options;
|
|
417
|
+
const log = useLogger("icon");
|
|
418
|
+
let bundle = {};
|
|
419
|
+
function build() {
|
|
420
|
+
const names = new Map;
|
|
421
|
+
for (const icon of clientBundle.icons)
|
|
422
|
+
names.set(icon, LISTED);
|
|
423
|
+
if (clientBundle.scan !== false) {
|
|
424
|
+
const rules = clientBundle.scan === true ? { include: [], exclude: [] } : clientBundle.scan;
|
|
425
|
+
const sources = findSources(scanRoots(hamur), hamur.options.rootDir, rules);
|
|
426
|
+
for (const [name, file] of scanSources(sources, Object.keys(collections), options.cssSelectorPrefix))
|
|
427
|
+
if (!names.has(name))
|
|
428
|
+
names.set(name, file);
|
|
429
|
+
}
|
|
430
|
+
const built = createClientBundle(names, collections);
|
|
431
|
+
for (const warning of built.warnings)
|
|
432
|
+
warn(warning);
|
|
433
|
+
const error = bundleSizeError(built.payload, clientBundle.sizeLimitKb);
|
|
434
|
+
if (error)
|
|
435
|
+
throw new DiagnosticError2("HAMUR_E5904", error);
|
|
436
|
+
bundle = built.payload;
|
|
437
|
+
}
|
|
438
|
+
addTemplate({ filename: "icon/client", getContents: () => `export default ${JSON.stringify(bundle)};
|
|
439
|
+
` });
|
|
440
|
+
hamur.hooks.hook("modules:done", build);
|
|
441
|
+
if (!hamur.options.dev || clientBundle.scan === false)
|
|
442
|
+
return;
|
|
443
|
+
hamur.hooks.hook("dev:change", async ({ relative, file }) => {
|
|
444
|
+
if (!SCAN_EXTENSIONS.test(relative) && isCustomFile(options.customCollections, file).length === 0)
|
|
445
|
+
return;
|
|
446
|
+
const before = JSON.stringify(bundle);
|
|
447
|
+
try {
|
|
448
|
+
build();
|
|
449
|
+
} catch (error) {
|
|
450
|
+
log.error(error instanceof Error ? error.message : String(error));
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (JSON.stringify(bundle) !== before)
|
|
454
|
+
await updateTemplates({ filter: ({ filename }) => filename === "icon/client" });
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// packages/icon/src/build/collections.ts
|
|
459
|
+
import { existsSync, mkdirSync, readdirSync as readdirSync3, readFileSync as readFileSync3, rmSync, writeFileSync } from "node:fs";
|
|
460
|
+
import { join as join4 } from "node:path";
|
|
461
|
+
import { addServerAssets, useLogger as useLogger2 } from "hamur/kit";
|
|
462
|
+
|
|
463
|
+
// packages/icon/src/kit.ts
|
|
464
|
+
import { DiagnosticError as DiagnosticError3 } from "hamur/kit";
|
|
465
|
+
var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
466
|
+
async function callCollectionsHook(hamur, collections) {
|
|
467
|
+
await hamur.hooks.callHook("icon:collections", collections);
|
|
468
|
+
for (const [prefix, collection] of Object.entries(collections)) {
|
|
469
|
+
if (!isObject(collection) || !isObject(collection.icons))
|
|
470
|
+
throw new DiagnosticError3("HAMUR_E5902", `icon:collections: collections[${JSON.stringify(prefix)}] must be Iconify JSON, { prefix, icons }`);
|
|
471
|
+
}
|
|
472
|
+
return collections;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// packages/icon/src/build/collections.ts
|
|
476
|
+
var ICONIFY_JSON = "@iconify-json/";
|
|
477
|
+
function listedCollections(rootDir) {
|
|
478
|
+
const file = join4(rootDir, "package.json");
|
|
479
|
+
if (!existsSync(file))
|
|
480
|
+
return [];
|
|
481
|
+
const manifest = JSON.parse(readFileSync3(file, "utf8"));
|
|
482
|
+
const names = [...Object.keys(manifest.dependencies ?? {}), ...Object.keys(manifest.devDependencies ?? {})];
|
|
483
|
+
return [
|
|
484
|
+
...new Set(names.filter((name) => name.startsWith(ICONIFY_JSON)).map((name) => name.slice(ICONIFY_JSON.length)))
|
|
485
|
+
].filter((prefix) => NAME_PATTERN.test(prefix)).sort();
|
|
486
|
+
}
|
|
487
|
+
function bundleLine(sizes) {
|
|
488
|
+
const parts = Object.entries(sizes).sort(([a], [b]) => a.localeCompare(b)).map(([prefix, bytes]) => `${prefix} ${(bytes / 1024 / 1024).toFixed(1)} MB`);
|
|
489
|
+
return parts.length === 0 ? undefined : `[icon] server bundle: ${parts.join(", ")}`;
|
|
490
|
+
}
|
|
491
|
+
function setupCollections(options, hamur, prefixes, known = {}) {
|
|
492
|
+
const dir = join4(hamur.options.buildDir, "icons");
|
|
493
|
+
mkdirSync(dir, { recursive: true });
|
|
494
|
+
addServerAssets({ name: "icons", dir });
|
|
495
|
+
hamur.hooks.hook("modules:done", async () => {
|
|
496
|
+
const collections = {};
|
|
497
|
+
for (const [prefix, file] of Object.entries(options.installed))
|
|
498
|
+
collections[prefix] = JSON.parse(readFileSync3(file, "utf8"));
|
|
499
|
+
for (const entry of options.customCollections)
|
|
500
|
+
collections[entry.prefix] = readCustomCollection(hamur.options.rootDir, entry);
|
|
501
|
+
await callCollectionsHook(hamur, collections);
|
|
502
|
+
for (const name of readdirSync3(dir))
|
|
503
|
+
if (name.endsWith(".json"))
|
|
504
|
+
rmSync(join4(dir, name));
|
|
505
|
+
const sizes = {};
|
|
506
|
+
for (const [prefix, collection] of Object.entries(collections)) {
|
|
507
|
+
const text = JSON.stringify(collection);
|
|
508
|
+
sizes[prefix] = Buffer.byteLength(text);
|
|
509
|
+
writeFileSync(join4(dir, `${prefix}.json`), text);
|
|
510
|
+
}
|
|
511
|
+
const line = bundleLine(sizes);
|
|
512
|
+
if (line)
|
|
513
|
+
useLogger2("icon").info(line);
|
|
514
|
+
prefixes.splice(0, prefixes.length, ...Object.keys(collections).sort());
|
|
515
|
+
Object.assign(known, collections);
|
|
516
|
+
});
|
|
517
|
+
if (!hamur.options.dev || options.customCollections.length === 0)
|
|
518
|
+
return;
|
|
519
|
+
hamur.hooks.hook("dev:change", (change) => {
|
|
520
|
+
const touched = isCustomFile(options.customCollections, change.file);
|
|
521
|
+
if (touched.length === 0)
|
|
522
|
+
return;
|
|
523
|
+
try {
|
|
524
|
+
for (const entry of touched) {
|
|
525
|
+
const collection = readCustomCollection(hamur.options.rootDir, entry);
|
|
526
|
+
writeFileSync(join4(dir, `${entry.prefix}.json`), JSON.stringify(collection));
|
|
527
|
+
known[entry.prefix] = collection;
|
|
528
|
+
}
|
|
529
|
+
} catch (error) {
|
|
530
|
+
useLogger2("icon").error(error instanceof Error ? error.message : String(error));
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
change.rebuildServer = true;
|
|
534
|
+
change.reload = true;
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// packages/icon/src/locate.ts
|
|
539
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
540
|
+
import { dirname, join as join5 } from "node:path";
|
|
541
|
+
var collectionCandidates = (prefix, full) => [
|
|
542
|
+
`@iconify-json/${prefix}/icons.json`,
|
|
543
|
+
...full ? [`@iconify/json/json/${prefix}.json`] : []
|
|
544
|
+
];
|
|
545
|
+
function locateCollection(rootDir, prefix, full) {
|
|
546
|
+
const candidates = collectionCandidates(prefix, full);
|
|
547
|
+
for (let dir = rootDir;; dir = dirname(dir)) {
|
|
548
|
+
for (const candidate of candidates) {
|
|
549
|
+
const file = join5(dir, "node_modules", candidate);
|
|
550
|
+
if (existsSync2(file))
|
|
551
|
+
return file;
|
|
552
|
+
}
|
|
553
|
+
if (dirname(dir) === dir)
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// packages/icon/src/build/css.ts
|
|
559
|
+
import { addAppPlugin, addTemplate as addTemplate2, createResolver, updateTemplates as updateTemplates2, useLogger as useLogger3 } from "hamur/kit";
|
|
560
|
+
function createClassesCss(classes, collections, options, warn) {
|
|
561
|
+
const rules = [];
|
|
562
|
+
for (const [className, { name, file }] of [...classes].sort(([a], [b]) => a.localeCompare(b))) {
|
|
563
|
+
const at = name.indexOf(":");
|
|
564
|
+
const prefix = name.slice(0, at);
|
|
565
|
+
const collection = Object.hasOwn(collections, prefix) ? collections[prefix] : undefined;
|
|
566
|
+
const data = collection && resolveIcon(collection, name.slice(at + 1));
|
|
567
|
+
if (!data) {
|
|
568
|
+
warn(`[icon] "${name}" (${file}) is not in the ${prefix} collection`);
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
rules.push(iconRule(className, data, iconNodes(data.body), options.cssWherePseudo));
|
|
572
|
+
}
|
|
573
|
+
return rules.length === 0 ? "" : `${iconStyle(rules, options.cssLayer)}
|
|
574
|
+
`;
|
|
575
|
+
}
|
|
576
|
+
function setupCss(options, hamur, collections, warn) {
|
|
577
|
+
if (!options.cssClasses)
|
|
578
|
+
return;
|
|
579
|
+
const { clientBundle } = options;
|
|
580
|
+
let css = "";
|
|
581
|
+
function build() {
|
|
582
|
+
const rules = typeof clientBundle.scan === "object" ? clientBundle.scan : { include: [], exclude: [] };
|
|
583
|
+
const sources = findSources(scanRoots(hamur), hamur.options.rootDir, rules);
|
|
584
|
+
css = createClassesCss(scanClassSources(sources, Object.keys(collections), options.cssSelectorPrefix), collections, options, warn);
|
|
585
|
+
}
|
|
586
|
+
addTemplate2({ filename: "icon/classes.css", getContents: () => css });
|
|
587
|
+
addAppPlugin({ src: createResolver(new URL("../../src/build/css.ts", import.meta.url).href).resolve("../runtime/classes.ts"), mode: "all" });
|
|
588
|
+
hamur.hooks.hook("modules:done", build);
|
|
589
|
+
if (!hamur.options.dev)
|
|
590
|
+
return;
|
|
591
|
+
hamur.hooks.hook("dev:change", async ({ relative }) => {
|
|
592
|
+
if (!SCAN_EXTENSIONS.test(relative))
|
|
593
|
+
return;
|
|
594
|
+
const before = css;
|
|
595
|
+
try {
|
|
596
|
+
build();
|
|
597
|
+
} catch (error) {
|
|
598
|
+
useLogger3("icon").error(error instanceof Error ? error.message : String(error));
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
if (css !== before)
|
|
602
|
+
await updateTemplates2({ filter: ({ filename }) => filename === "icon/classes.css" });
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// packages/icon/src/build/endpoint.ts
|
|
607
|
+
import { addServer, addTemplate as addTemplate3, createResolver as createResolver2 } from "hamur/kit";
|
|
608
|
+
|
|
609
|
+
// packages/icon/src/options.ts
|
|
610
|
+
import { isAbsolute, resolve } from "node:path";
|
|
611
|
+
import { DiagnosticError as DiagnosticError4, isEnvRef } from "hamur/kit";
|
|
612
|
+
var OPTION_KEYS = [
|
|
613
|
+
"provider",
|
|
614
|
+
"iconifyApiEndpoint",
|
|
615
|
+
"fallbackToApi",
|
|
616
|
+
"fetchTimeout",
|
|
617
|
+
"serverBundle",
|
|
618
|
+
"clientBundle",
|
|
619
|
+
"customCollections",
|
|
620
|
+
"cssLayer",
|
|
621
|
+
"cssSelectorPrefix",
|
|
622
|
+
"cssWherePseudo",
|
|
623
|
+
"cssClasses",
|
|
624
|
+
"size",
|
|
625
|
+
"class",
|
|
626
|
+
"mode",
|
|
627
|
+
"aliases"
|
|
628
|
+
];
|
|
629
|
+
var CLIENT_BUNDLE_KEYS = ["icons", "scan", "sizeLimitKb"];
|
|
630
|
+
var CUSTOM_KEYS = ["prefix", "dir", "recursive"];
|
|
631
|
+
var PROVIDERS = ["server", "iconify", "none"];
|
|
632
|
+
var SELECTOR_PREFIX = /^[a-z_][a-z0-9_-]*$/i;
|
|
633
|
+
var FILE_PRESETS = new Set(["static", "cloudflare-static"]);
|
|
634
|
+
var isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
635
|
+
var isPlainRecord = (value) => isObject2(value) && Object.getPrototypeOf(value) === Object.prototype && !isEnvRef(value);
|
|
636
|
+
var shown = (value) => typeof value === "function" ? "a function" : JSON.stringify(value) ?? String(value);
|
|
637
|
+
var isPositiveInteger = (value) => typeof value === "number" && Number.isInteger(value) && value > 0;
|
|
638
|
+
function fail(message) {
|
|
639
|
+
throw new DiagnosticError4("HAMUR_E5901", message);
|
|
640
|
+
}
|
|
641
|
+
function checkKeys(input, allowed, at) {
|
|
642
|
+
for (const key of Object.keys(input))
|
|
643
|
+
if (!allowed.includes(key))
|
|
644
|
+
fail(`${at}.${key} is not an option; use ${allowed.join(", ")}`);
|
|
645
|
+
}
|
|
646
|
+
function checkBoolean(value, at) {
|
|
647
|
+
if (typeof value !== "boolean")
|
|
648
|
+
fail(`${at} must be a boolean (got ${shown(value)})`);
|
|
649
|
+
return value;
|
|
650
|
+
}
|
|
651
|
+
function checkPositive(value, at) {
|
|
652
|
+
if (!isPositiveInteger(value))
|
|
653
|
+
fail(`${at} must be a positive integer (got ${shown(value)})`);
|
|
654
|
+
return value;
|
|
655
|
+
}
|
|
656
|
+
function resolveEndpoint(value) {
|
|
657
|
+
let url;
|
|
658
|
+
try {
|
|
659
|
+
url = typeof value === "string" ? new URL(value) : undefined;
|
|
660
|
+
} catch {}
|
|
661
|
+
if (!url || url.protocol !== "http:" && url.protocol !== "https:")
|
|
662
|
+
fail(`icon.iconifyApiEndpoint must be an http or https URL (got ${shown(value)})`);
|
|
663
|
+
return value.replace(/\/+$/, "");
|
|
664
|
+
}
|
|
665
|
+
function resolveServerBundle(value, ctx) {
|
|
666
|
+
const installed = {};
|
|
667
|
+
if (value === false)
|
|
668
|
+
return installed;
|
|
669
|
+
if (value === "local") {
|
|
670
|
+
for (const prefix of ctx.listed) {
|
|
671
|
+
const file = ctx.locate(prefix, false);
|
|
672
|
+
if (file)
|
|
673
|
+
installed[prefix] = file;
|
|
674
|
+
}
|
|
675
|
+
return installed;
|
|
676
|
+
}
|
|
677
|
+
const collections = isObject2(value) ? value.collections : undefined;
|
|
678
|
+
if (!isObject2(value) || Object.keys(value).some((key) => key !== "collections") || !Array.isArray(collections) || collections.some((entry) => typeof entry !== "string")) {
|
|
679
|
+
fail(`icon.serverBundle must be "local", false or { collections: string[] } (got ${shown(value)})`);
|
|
680
|
+
}
|
|
681
|
+
for (const [index, prefix] of collections.entries()) {
|
|
682
|
+
const file = NAME_PATTERN.test(prefix) ? ctx.locate(prefix, true) : undefined;
|
|
683
|
+
if (!file)
|
|
684
|
+
fail(`icon.serverBundle.collections[${index}] ${shown(prefix)} is not installed; add @iconify-json/${prefix} to the app's dependencies`);
|
|
685
|
+
installed[prefix] = file;
|
|
686
|
+
}
|
|
687
|
+
return installed;
|
|
688
|
+
}
|
|
689
|
+
function resolveClientBundle(value) {
|
|
690
|
+
const out = { icons: [], scan: true, sizeLimitKb: 256 };
|
|
691
|
+
if (value === undefined)
|
|
692
|
+
return out;
|
|
693
|
+
if (!isObject2(value))
|
|
694
|
+
fail(`icon.clientBundle must be an object (got ${shown(value)})`);
|
|
695
|
+
checkKeys(value, CLIENT_BUNDLE_KEYS, "icon.clientBundle");
|
|
696
|
+
if (value.icons !== undefined) {
|
|
697
|
+
if (!Array.isArray(value.icons))
|
|
698
|
+
fail(`icon.clientBundle.icons must be an array of "prefix:name" (got ${shown(value.icons)})`);
|
|
699
|
+
for (const [index, icon] of value.icons.entries()) {
|
|
700
|
+
const [prefix, name, more] = typeof icon === "string" ? icon.split(":") : [];
|
|
701
|
+
if (more !== undefined || !NAME_PATTERN.test(prefix ?? "") || !NAME_PATTERN.test(name ?? ""))
|
|
702
|
+
fail(`icon.clientBundle.icons[${index}] ${shown(icon)} must be "prefix:name" (got ${shown(icon)})`);
|
|
703
|
+
}
|
|
704
|
+
out.icons = [...value.icons];
|
|
705
|
+
}
|
|
706
|
+
if (value.scan !== undefined) {
|
|
707
|
+
const scan = value.scan;
|
|
708
|
+
const list = (entry) => entry === undefined || Array.isArray(entry) && entry.every((item) => typeof item === "string");
|
|
709
|
+
if (typeof scan === "boolean")
|
|
710
|
+
out.scan = scan;
|
|
711
|
+
else if (isObject2(scan) && Object.keys(scan).every((key) => key === "include" || key === "exclude") && list(scan.include) && list(scan.exclude)) {
|
|
712
|
+
out.scan = {
|
|
713
|
+
include: [...scan.include ?? []],
|
|
714
|
+
exclude: [...scan.exclude ?? []]
|
|
715
|
+
};
|
|
716
|
+
} else
|
|
717
|
+
fail(`icon.clientBundle.scan must be a boolean or { include?, exclude? } (got ${shown(scan)})`);
|
|
718
|
+
}
|
|
719
|
+
if (value.sizeLimitKb !== undefined)
|
|
720
|
+
out.sizeLimitKb = checkPositive(value.sizeLimitKb, "icon.clientBundle.sizeLimitKb");
|
|
721
|
+
return out;
|
|
722
|
+
}
|
|
723
|
+
function resolveCustomCollections(value, installed, ctx) {
|
|
724
|
+
if (value === undefined)
|
|
725
|
+
return [];
|
|
726
|
+
if (!Array.isArray(value))
|
|
727
|
+
fail(`icon.customCollections must be an array of { prefix, dir, recursive? } (got ${shown(value)})`);
|
|
728
|
+
const out = [];
|
|
729
|
+
for (const [index, entry] of value.entries()) {
|
|
730
|
+
const at = `icon.customCollections[${index}]`;
|
|
731
|
+
if (!isObject2(entry))
|
|
732
|
+
fail(`${at} must be { prefix, dir, recursive? } (got ${shown(entry)})`);
|
|
733
|
+
checkKeys(entry, CUSTOM_KEYS, at);
|
|
734
|
+
const { prefix, dir } = entry;
|
|
735
|
+
if (typeof prefix !== "string" || !NAME_PATTERN.test(prefix))
|
|
736
|
+
fail(`${at}.prefix ${shown(prefix)} must match ${NAME_PATTERN}`);
|
|
737
|
+
if (typeof dir !== "string" || dir === "")
|
|
738
|
+
fail(`${at}.dir must be a non-empty string (got ${shown(dir)})`);
|
|
739
|
+
const path = isAbsolute(dir) ? dir : [ctx.rootDir, ...ctx.layerRoots ?? []].map((root) => resolve(root, dir)).find((candidate) => ctx.exists(candidate)) ?? resolve(ctx.rootDir, dir);
|
|
740
|
+
if (!ctx.exists(path))
|
|
741
|
+
fail(`${at}.dir ${shown(dir)} does not exist`);
|
|
742
|
+
if (Object.hasOwn(installed, prefix))
|
|
743
|
+
fail(`${at}.prefix ${shown(prefix)} is already an installed collection`);
|
|
744
|
+
if (out.some((seen) => seen.prefix === prefix))
|
|
745
|
+
fail(`${at}.prefix ${shown(prefix)} is used twice`);
|
|
746
|
+
out.push({
|
|
747
|
+
prefix,
|
|
748
|
+
dir: path,
|
|
749
|
+
recursive: entry.recursive === undefined ? false : checkBoolean(entry.recursive, `${at}.recursive`)
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
return out;
|
|
753
|
+
}
|
|
754
|
+
function iconDefaults(input) {
|
|
755
|
+
const raw = isObject2(input) ? input : {};
|
|
756
|
+
const out = {};
|
|
757
|
+
if (raw.size !== undefined) {
|
|
758
|
+
if (typeof raw.size !== "string" && !(typeof raw.size === "number" && raw.size > 0))
|
|
759
|
+
fail(`icon.size must be a string or a positive number (got ${shown(raw.size)})`);
|
|
760
|
+
out.size = raw.size;
|
|
761
|
+
}
|
|
762
|
+
if (raw.class !== undefined) {
|
|
763
|
+
if (typeof raw.class !== "string")
|
|
764
|
+
fail(`icon.class must be a string (got ${shown(raw.class)})`);
|
|
765
|
+
out.class = raw.class;
|
|
766
|
+
}
|
|
767
|
+
if (raw.mode !== undefined) {
|
|
768
|
+
if (raw.mode !== "svg" && raw.mode !== "css")
|
|
769
|
+
fail(`icon.mode ${shown(raw.mode)} is not "svg" or "css"`);
|
|
770
|
+
out.mode = raw.mode;
|
|
771
|
+
}
|
|
772
|
+
if (raw.aliases !== undefined) {
|
|
773
|
+
const aliases = raw.aliases;
|
|
774
|
+
if (!isObject2(aliases) || !Object.values(aliases).every((target) => typeof target === "string"))
|
|
775
|
+
fail(`icon.aliases must be an object of strings (got ${shown(aliases)})`);
|
|
776
|
+
out.aliases = { ...aliases };
|
|
777
|
+
}
|
|
778
|
+
return out;
|
|
779
|
+
}
|
|
780
|
+
function iconDefaultsUnder(defaults, app) {
|
|
781
|
+
const own = isObject2(app) ? app : {};
|
|
782
|
+
const out = {};
|
|
783
|
+
for (const [key, value] of Object.entries(defaults)) {
|
|
784
|
+
const set = own[key];
|
|
785
|
+
if (key === "aliases" && isPlainRecord(set)) {
|
|
786
|
+
const aliases = Object.fromEntries(Object.entries(value).filter(([name]) => set[name] === undefined));
|
|
787
|
+
if (Object.keys(aliases).length)
|
|
788
|
+
out.aliases = aliases;
|
|
789
|
+
} else if (set === undefined)
|
|
790
|
+
out[key] = value;
|
|
791
|
+
}
|
|
792
|
+
return out;
|
|
793
|
+
}
|
|
794
|
+
function resolveOptions(input, ctx) {
|
|
795
|
+
const raw = input ?? {};
|
|
796
|
+
if (!isObject2(raw))
|
|
797
|
+
fail(`icon must be an object (got ${shown(raw)})`);
|
|
798
|
+
checkKeys(raw, OPTION_KEYS, "icon");
|
|
799
|
+
let provider = FILE_PRESETS.has(ctx.preset) ? "none" : "server";
|
|
800
|
+
if (raw.provider !== undefined) {
|
|
801
|
+
if (!PROVIDERS.includes(raw.provider))
|
|
802
|
+
fail(`icon.provider ${shown(raw.provider)} is not "server", "iconify" or "none"`);
|
|
803
|
+
provider = raw.provider;
|
|
804
|
+
}
|
|
805
|
+
const installed = resolveServerBundle(raw.serverBundle ?? "local", ctx);
|
|
806
|
+
const out = {
|
|
807
|
+
provider,
|
|
808
|
+
iconifyApiEndpoint: raw.iconifyApiEndpoint === undefined ? "https://api.iconify.design" : resolveEndpoint(raw.iconifyApiEndpoint),
|
|
809
|
+
fallbackToApi: raw.fallbackToApi === undefined ? false : checkBoolean(raw.fallbackToApi, "icon.fallbackToApi"),
|
|
810
|
+
fetchTimeout: raw.fetchTimeout === undefined ? 1500 : checkPositive(raw.fetchTimeout, "icon.fetchTimeout"),
|
|
811
|
+
installed,
|
|
812
|
+
clientBundle: resolveClientBundle(raw.clientBundle),
|
|
813
|
+
customCollections: resolveCustomCollections(raw.customCollections, installed, ctx),
|
|
814
|
+
cssSelectorPrefix: "i-",
|
|
815
|
+
cssWherePseudo: raw.cssWherePseudo === undefined ? true : checkBoolean(raw.cssWherePseudo, "icon.cssWherePseudo"),
|
|
816
|
+
cssClasses: raw.cssClasses === undefined ? false : checkBoolean(raw.cssClasses, "icon.cssClasses")
|
|
817
|
+
};
|
|
818
|
+
if (raw.cssSelectorPrefix !== undefined) {
|
|
819
|
+
if (typeof raw.cssSelectorPrefix !== "string" || !SELECTOR_PREFIX.test(raw.cssSelectorPrefix))
|
|
820
|
+
fail(`icon.cssSelectorPrefix ${shown(raw.cssSelectorPrefix)} must match ${SELECTOR_PREFIX}`);
|
|
821
|
+
out.cssSelectorPrefix = raw.cssSelectorPrefix;
|
|
822
|
+
}
|
|
823
|
+
if (raw.cssLayer !== undefined) {
|
|
824
|
+
if (typeof raw.cssLayer !== "string" || raw.cssLayer === "")
|
|
825
|
+
fail(`icon.cssLayer must be a non-empty string (got ${shown(raw.cssLayer)})`);
|
|
826
|
+
out.cssLayer = raw.cssLayer;
|
|
827
|
+
}
|
|
828
|
+
return out;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// packages/icon/src/build/endpoint.ts
|
|
832
|
+
function setupEndpoint(options, hamur) {
|
|
833
|
+
if (options.provider === "none" || FILE_PRESETS.has(hamur.options.preset))
|
|
834
|
+
return;
|
|
835
|
+
const endpoint = {
|
|
836
|
+
dev: hamur.options.dev,
|
|
837
|
+
fallbackToApi: options.fallbackToApi,
|
|
838
|
+
iconifyApiEndpoint: options.iconifyApiEndpoint,
|
|
839
|
+
fetchTimeout: options.fetchTimeout
|
|
840
|
+
};
|
|
841
|
+
addTemplate3({ filename: "icon/endpoint", getContents: () => `export default ${JSON.stringify(endpoint)};
|
|
842
|
+
` });
|
|
843
|
+
addServer(createResolver2(new URL("../../src/build/endpoint.ts", import.meta.url).href).resolve("../server/door.ts"));
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
// packages/icon/src/build/runtime.ts
|
|
847
|
+
import { addAppPlugin as addAppPlugin2, addTemplate as addTemplate4, createResolver as createResolver3 } from "hamur/kit";
|
|
848
|
+
function setupRuntime(options, prefixes) {
|
|
849
|
+
addTemplate4({
|
|
850
|
+
filename: "icon/options",
|
|
851
|
+
getContents: () => {
|
|
852
|
+
const runtime = {
|
|
853
|
+
prefixes: [...prefixes].sort((a, b) => b.length - a.length || a.localeCompare(b)),
|
|
854
|
+
serverBundle: [...prefixes],
|
|
855
|
+
provider: options.provider,
|
|
856
|
+
iconifyApiEndpoint: options.iconifyApiEndpoint,
|
|
857
|
+
fetchTimeout: options.fetchTimeout,
|
|
858
|
+
...options.cssLayer === undefined ? {} : { cssLayer: options.cssLayer },
|
|
859
|
+
cssSelectorPrefix: options.cssSelectorPrefix,
|
|
860
|
+
cssWherePseudo: options.cssWherePseudo
|
|
861
|
+
};
|
|
862
|
+
return `export default ${JSON.stringify(runtime)};
|
|
863
|
+
`;
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
addAppPlugin2({ src: createResolver3(new URL("../../src/build/runtime.ts", import.meta.url).href).resolve("../runtime/server.ts"), mode: "server" });
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
// packages/icon/src/module.ts
|
|
870
|
+
var module_default = defineModule({
|
|
871
|
+
meta: { name: "icon", configKey: "icon", hooks: ["icon:collections"] },
|
|
872
|
+
setup(input, hamur) {
|
|
873
|
+
const { rootDir, preset } = hamur.options;
|
|
874
|
+
const options = resolveOptions(input, {
|
|
875
|
+
rootDir,
|
|
876
|
+
layerRoots: hamur.options.layers.map((layer) => layer.rootDir),
|
|
877
|
+
preset,
|
|
878
|
+
exists: existsSync3,
|
|
879
|
+
listed: listedCollections(rootDir),
|
|
880
|
+
locate: (prefix, full) => locateCollection(rootDir, prefix, full)
|
|
881
|
+
});
|
|
882
|
+
const defaults = iconDefaultsUnder(iconDefaults(input), hamur.options.runtimeConfig.public?.icon);
|
|
883
|
+
if (Object.keys(defaults).length)
|
|
884
|
+
updateRuntimeConfig({ public: { icon: defaults } });
|
|
885
|
+
const prefixes = [];
|
|
886
|
+
const collections = {};
|
|
887
|
+
setupCollections(options, hamur, prefixes, collections);
|
|
888
|
+
const warn = createWarner();
|
|
889
|
+
setupClient(options, hamur, collections, warn);
|
|
890
|
+
setupCss(options, hamur, collections, warn);
|
|
891
|
+
setupRuntime(options, prefixes);
|
|
892
|
+
setupEndpoint(options, hamur);
|
|
893
|
+
}
|
|
894
|
+
});
|
|
895
|
+
export {
|
|
896
|
+
module_default as default
|
|
897
|
+
};
|