Skip to content

Commit a0fa4dd

Browse files
committed
feat(icons-material): export all icons in the index file
1 parent d85ddb3 commit a0fa4dd

File tree

6 files changed

+61
-14
lines changed

6 files changed

+61
-14
lines changed

.changeset/loud-brooms-know.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@suid/icons-material": minor
3+
---
4+
5+
Export all icons in the index file
6+
7+
Use `@suid/vite-plugin` for optimizing the imports.

packages/icons-material/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
!lib
22
lib/*
33
!lib/*.jsx
4+
lib/index.jsx
45
!lib/utils

packages/icons-material/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"material design",
1212
"icons"
1313
],
14+
"main": "index.jsx",
1415
"scripts": {
1516
"build": "tsc --build",
1617
"clean": "tsc --build --clean",

scripts/actions/genIconsMaterialSource.ts

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import { format } from "prettier";
1010

1111
const outPath = join(packagesPath, "icons-material/lib");
1212

13+
export async function writeIndexFile(iconNames: string[]) {
14+
const contents = iconNames
15+
.map((v) => `export { default as ${v} } from "./${v}";`)
16+
.join("\n");
17+
await writeFile(join(outPath, "index.jsx"), contents);
18+
}
19+
1320
async function renderComponentContents(name: string, children: string[]) {
1421
return format(
1522
`
@@ -44,6 +51,7 @@ async function genIconsMaterialSource(options: { version: string }) {
4451
const iconNames = new Set<string>();
4552

4653
for (const fileName of fileNames) {
54+
if (fileName === "index.jsx") continue;
4755
progressLog.add();
4856
const [name] = normalizeFileName(fileName).split(".");
4957
if (iconNames.has(name)) throw new Error(`Duplicated icon name: ${name}`);
@@ -66,6 +74,7 @@ async function genIconsMaterialSource(options: { version: string }) {
6674
const componentSource = await renderComponentContents(name, svgChildren);
6775
await writeFile(componentPath, componentSource);
6876
}
77+
await writeIndexFile(Array.from(iconNames));
6978
progressLog.stop();
7079
}
7180

scripts/actions/genIconsMaterialTyping.ts

+38-14
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
1-
import { createProgressLog } from "./../util/cli";
1+
import { createProgressLog, ProgressLog } from "./../util/cli";
22
import { readOptions } from "./../util/prettier";
33
import { packagesPath } from "./../util/workspace";
4+
import { writeIndexFile } from "./genIconsMaterialSource";
45
import { readdir, writeFile } from "fs/promises";
56
import pLimit from "p-limit";
67
import { join } from "path";
78
import { format } from "prettier";
89

910
const outPath = join(packagesPath, "icons-material/lib");
1011

11-
async function genIconsMaterialTyping() {
12-
const { name } = genIconsMaterialTyping;
12+
async function writeIndexTypingFile(
13+
iconNames: string[],
14+
progressLog: ProgressLog
15+
) {
16+
const contents = [
17+
`import SvgIcon from "@suid/material/SvgIcon";`,
18+
`type C = typeof SvgIcon;`,
19+
...iconNames.map((v) => `export const ${v}: C;`),
20+
].join("\n");
21+
progressLog.add();
22+
await writeFile(join(outPath, "index.d.ts"), contents);
23+
}
1324

14-
const fileNames = (await readdir(outPath)).filter((v) => v.endsWith(".jsx"));
15-
const progressLog = createProgressLog({
16-
name,
17-
total: fileNames.length,
18-
});
25+
async function writeComponentTypingFiles(
26+
iconNames: string[],
27+
progressLog: ProgressLog
28+
) {
1929
const contents = format(`export { default } from "@suid/material/SvgIcon"`, {
2030
...(await readOptions()),
2131
parser: "typescript",
2232
});
2333

2434
const limit = pLimit(10);
25-
const paths = fileNames.map((f) =>
26-
join(outPath, f.replace(/.jsx$/, ".d.ts"))
27-
);
35+
const paths = iconNames.map((v) => join(outPath, `${v}.d.ts`));
2836

29-
const promises = paths.map((path) =>
37+
return paths.map((path) =>
3038
limit(() => {
31-
progressLog.add();
39+
progressLog?.add();
3240
return writeFile(path, contents);
3341
})
3442
);
43+
}
44+
async function genIconsMaterialTyping() {
45+
const { name } = genIconsMaterialTyping;
46+
47+
const iconNames = (await readdir(outPath))
48+
.filter((v) => v !== "index.jsx" && v.endsWith(".jsx"))
49+
.map((v) => v.replace(/.jsx$/, ""));
50+
51+
const progressLog = createProgressLog({
52+
name,
53+
total: iconNames.length + 1,
54+
});
3555

36-
await Promise.all(promises);
56+
await Promise.all([
57+
...(await writeComponentTypingFiles(iconNames, progressLog)),
58+
writeIndexTypingFile(iconNames, progressLog),
59+
writeIndexFile(iconNames),
60+
]);
3761
progressLog.stop();
3862
}
3963

scripts/util/cli.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
export type ProgressLog = {
2+
stop: () => void;
3+
add: () => void;
4+
};
5+
16
export function createProgressLog(data: { name: string; total: number }) {
27
let current = 0;
38
console.time(data.name);

0 commit comments

Comments
 (0)