Skip to content

Commit d25741a

Browse files
authored
Merge pull request #1548 from easyops-cn/steve/icon-no-range-request
feat(icon): remove supports for range request
2 parents 4747f55 + 8e4c604 commit d25741a

File tree

6 files changed

+39
-243
lines changed

6 files changed

+39
-243
lines changed

bricks/icons/scripts/pre-build.js

Lines changed: 13 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
import path from "node:path";
66
import { fileURLToPath } from "node:url";
77
import { existsSync, mkdirSync, rmSync } from "node:fs";
8-
import { readdir, readFile, writeFile } from "node:fs/promises";
8+
import { readdir, writeFile } from "node:fs/promises";
99
import { createRequire } from "node:module";
10-
import { createHash } from "node:crypto";
1110
import { far } from "@fortawesome/free-regular-svg-icons";
1211
import { fas } from "@fortawesome/free-solid-svg-icons";
1312
import { fab } from "@fortawesome/free-brands-svg-icons";
@@ -42,11 +41,6 @@ const tasks = [];
4241
default: [],
4342
};
4443

45-
const ranges = {};
46-
const allSvg = [];
47-
const iconsWithPath = [];
48-
let cursor = -2;
49-
5044
tasks.push(
5145
(async () => {
5246
const list = await readdir(legacyEasyOpsIconsPath, {
@@ -71,62 +65,19 @@ const tasks = [];
7165
}
7266
} else if (ext === ".svg") {
7367
allIcons[item.name].push(iconName);
74-
iconsWithPath.push([
75-
item.name,
76-
path.join(legacyEasyOpsIconsPath, item.name, icon),
77-
]);
7868
}
7969
}
8070
} else if (item.name.endsWith(".svg")) {
8171
const iconName = item.name.substring(0, item.name.length - 4);
8272
allIcons.default.push(iconName);
83-
iconsWithPath.push([
84-
"default",
85-
path.join(legacyEasyOpsIconsPath, item.name),
86-
]);
8773
}
8874
})
8975
);
9076

91-
const hashes = [];
92-
93-
// Have to be sequential
94-
for (const [category, iconPath] of iconsWithPath) {
95-
const svg = await readFile(iconPath);
96-
allSvg.push(svg);
97-
98-
let groupRanges = ranges[category];
99-
if (!_.has(ranges, category)) {
100-
groupRanges = ranges[category] = [];
101-
}
102-
cursor += svg.length + 1;
103-
groupRanges.push(cursor);
104-
105-
const sha1 = createHash("sha1");
106-
sha1.update(svg);
107-
hashes.push(sha1.digest("hex").substring(0, 8));
108-
}
109-
110-
// Let final hash to be irrelevant to the order of icons
111-
hashes.sort();
112-
const sha1 = createHash("sha1");
113-
sha1.update(hashes.join(""));
114-
ranges._hash = sha1.digest("hex").substring(0, 8);
115-
116-
await Promise.all([
117-
writeFile(
118-
path.resolve(newEasyOpsIconsPath, "icons.json"),
119-
JSON.stringify(allIcons)
120-
),
121-
writeFile(
122-
path.resolve(newEasyOpsIconsPath, "ranges.json"),
123-
JSON.stringify(ranges)
124-
),
125-
writeFile(
126-
path.resolve(newEasyOpsIconsPath, `all.${ranges._hash}.svg`),
127-
allSvg.join("\n")
128-
),
129-
]);
77+
await writeFile(
78+
path.resolve(newEasyOpsIconsPath, "icons.json"),
79+
JSON.stringify(allIcons)
80+
);
13081
})()
13182
);
13283
}
@@ -152,14 +103,8 @@ const tasks = [];
152103
allIcons[category] = [];
153104
}
154105

155-
const ranges = {};
156-
const allJson = [];
157-
let cursor = -2;
158-
159106
tasks.push(
160107
(async () => {
161-
const hashes = [];
162-
163108
await Promise.all(
164109
Object.entries(iconCategories).map(async ([category, pack]) => {
165110
const aliasMap = (aliasMapByCategory[category] = {});
@@ -174,20 +119,6 @@ const tasks = [];
174119
aliasMap[alias] = item.iconName;
175120
}
176121
const content = JSON.stringify(item);
177-
178-
let groupRanges = ranges[category];
179-
if (!_.has(ranges, category)) {
180-
groupRanges = ranges[category] = [];
181-
}
182-
cursor += content.length + 1;
183-
groupRanges.push(cursor);
184-
185-
allJson.push(content);
186-
187-
const sha1 = createHash("sha1");
188-
sha1.update(content);
189-
hashes.push(sha1.digest("hex").substring(0, 8));
190-
191122
return writeFile(
192123
path.resolve(generatedDir, `${category}/${item.iconName}.json`),
193124
content
@@ -197,11 +128,6 @@ const tasks = [];
197128
})
198129
);
199130

200-
hashes.sort();
201-
const sha1 = createHash("sha1");
202-
sha1.update(hashes.join(""));
203-
ranges._hash = sha1.digest("hex").substring(0, 8);
204-
205131
await Promise.all([
206132
writeFile(
207133
path.resolve(generatedDir, "alias.json"),
@@ -211,14 +137,6 @@ const tasks = [];
211137
path.resolve(generatedDir, "icons.json"),
212138
JSON.stringify(allIcons)
213139
),
214-
writeFile(
215-
path.resolve(generatedDir, "ranges.json"),
216-
JSON.stringify(ranges)
217-
),
218-
writeFile(
219-
path.resolve(generatedDir, `all.${ranges._hash}.json`),
220-
allJson.join("\n")
221-
),
222140
]);
223141
})()
224142
);
@@ -240,14 +158,8 @@ const tasks = [];
240158
allIcons[theme] = [];
241159
}
242160

243-
const ranges = {};
244-
const allSvg = [];
245-
let cursor = -2;
246-
247161
tasks.push(
248162
(async () => {
249-
const hashes = [];
250-
251163
await Promise.all(
252164
Object.values(antdIcons).map((icon) => {
253165
if (!themes.includes(icon.theme)) {
@@ -262,45 +174,17 @@ const tasks = [];
262174
},
263175
});
264176

265-
let groupRanges = ranges[icon.theme];
266-
if (!_.has(ranges, icon.theme)) {
267-
groupRanges = ranges[icon.theme] = [];
268-
}
269-
cursor += svg.length + 1;
270-
groupRanges.push(cursor);
271-
272-
allSvg.push(svg);
273-
274-
const sha1 = createHash("sha1");
275-
sha1.update(svg);
276-
hashes.push(sha1.digest("hex").substring(0, 8));
277-
278177
return writeFile(
279178
path.resolve(generatedDir, icon.theme, `${icon.name}.svg`),
280179
svg
281180
);
282181
})
283182
);
284183

285-
hashes.sort();
286-
const sha1 = createHash("sha1");
287-
sha1.update(hashes.join(""));
288-
ranges._hash = sha1.digest("hex").substring(0, 8);
289-
290-
await Promise.all([
291-
writeFile(
292-
path.resolve(generatedDir, "icons.json"),
293-
JSON.stringify(allIcons)
294-
),
295-
writeFile(
296-
path.resolve(generatedDir, "ranges.json"),
297-
JSON.stringify(ranges)
298-
),
299-
writeFile(
300-
path.resolve(generatedDir, `all.${ranges._hash}.svg`),
301-
allSvg.join("\n")
302-
),
303-
]);
184+
await writeFile(
185+
path.resolve(generatedDir, "icons.json"),
186+
JSON.stringify(allIcons)
187+
);
304188
})()
305189
);
306190
}
@@ -322,10 +206,7 @@ const tasks = [];
322206
[defaultCategory]: [],
323207
};
324208

325-
const ranges = {};
326-
const allSvg = [];
327209
const iconsWithPath = [];
328-
let cursor = -2;
329210

330211
tasks.push(
331212
(async () => {
@@ -341,45 +222,10 @@ const tasks = [];
341222
}
342223
}
343224

344-
const hashes = [];
345-
346-
// Have to be sequential
347-
for (const [category, iconPath] of iconsWithPath) {
348-
const svg = await readFile(iconPath);
349-
allSvg.push(svg);
350-
351-
let groupRanges = ranges[category];
352-
if (!_.has(ranges, category)) {
353-
groupRanges = ranges[category] = [];
354-
}
355-
cursor += svg.length + 1;
356-
groupRanges.push(cursor);
357-
358-
const sha1 = createHash("sha1");
359-
sha1.update(svg);
360-
hashes.push(sha1.digest("hex").substring(0, 8));
361-
}
362-
363-
// Let final hash to be irrelevant to the order of icons
364-
hashes.sort();
365-
const sha1 = createHash("sha1");
366-
sha1.update(hashes.join(""));
367-
ranges._hash = sha1.digest("hex").substring(0, 8);
368-
369-
await Promise.all([
370-
writeFile(
371-
path.resolve(generatedDir, "icons.json"),
372-
JSON.stringify(allIcons)
373-
),
374-
writeFile(
375-
path.resolve(generatedDir, "ranges.json"),
376-
JSON.stringify(ranges)
377-
),
378-
writeFile(
379-
path.resolve(generatedDir, `all.${ranges._hash}.svg`),
380-
allSvg.join("\n")
381-
),
382-
]);
225+
await writeFile(
226+
path.resolve(generatedDir, "icons.json"),
227+
JSON.stringify(allIcons)
228+
);
383229
})()
384230
);
385231
}

bricks/icons/src/antd-icon/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ class AntdIcon extends NextElement implements AntdIconProps {
8989
: undefined;
9090
const svg = await getIcon(url, {
9191
currentColor: theme !== "twotone",
92-
lib: "antd",
93-
id: `${theme}/${icon}`,
9492
});
9593
if (theme !== fixTheme(this.theme) || icon !== this.icon) {
9694
// The icon has changed

bricks/icons/src/easyops-icon/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ class EasyOpsIcon extends NextElement implements EasyOpsIconProps {
8181
} else {
8282
const svg = await getIcon(url, {
8383
currentColor: !category.startsWith("colored-"),
84-
lib: "easyops",
85-
id: `${category}/${icon}`,
8684
});
8785
if (category !== (this.category ?? "default") || icon !== this.icon) {
8886
// The icon has changed during `await getIcon(...)`

bricks/icons/src/fa-icon/index.tsx

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,15 @@ import {
99
GradientDirection,
1010
} from "../shared/DefineLinearGradient.js";
1111
import AliasJson from "./generated/alias.json";
12-
import {
13-
RangeRequest,
14-
supportsMultipartRangeRequest,
15-
} from "../shared/RangeRequest.js";
1612
import linearGradientStyleText from "../shared/DefineLinearGradient.shadow.css";
1713
import type { IconEvents, IconEventsMapping } from "../shared/interfaces.js";
1814
import sharedStyleText from "../shared/icons.shadow.css";
1915

2016
const iconCache = new Map<string, Promise<IconDefinition | null>>();
21-
const faRangeRequest = new RangeRequest("fa");
2217

2318
async function resolveIconDefinition(
2419
id: string
2520
): Promise<IconDefinition | null> {
26-
// istanbul ignore next: experimental
27-
try {
28-
if (await supportsMultipartRangeRequest()) {
29-
const content = await faRangeRequest.fetch(id);
30-
return JSON.parse(content);
31-
}
32-
} catch (error) {
33-
// eslint-disable-next-line no-console
34-
console.warn("Failed to fetch icon by range:", id, error);
35-
// Fallback to traditional fetch.
36-
}
37-
3821
try {
3922
const url = `${
4023
// istanbul ignore next

bricks/icons/src/lucide-icon/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ class LucideIcon extends NextElement implements LucideIconProps {
8383
process.env.NODE_ENV === "test" ? "" : __webpack_public_path__
8484
}chunks/lucide-icons/${icon}.svg`
8585
: undefined;
86-
const svg = await getIcon(url, {
87-
lib: "lucide",
88-
id: `default/${icon}`,
89-
});
86+
const svg = await getIcon(url);
9087
if (icon !== this.icon) {
9188
// The icon has changed
9289
return;

0 commit comments

Comments
 (0)