Skip to content

Commit dad438a

Browse files
authored
Merge pull request #4672 from easyops-cn/steve/v2-image-icons
feat(): support image icons
2 parents 6eb873d + f2688aa commit dad438a

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = [
2929
},
3030
{
3131
path: "packages/brick-container/dist/icons--*.js",
32-
limit: "882 KB",
32+
limit: "890 KB",
3333
},
3434
{
3535
path: "packages/brick-container/dist/polyfill.*.js",

declarations/global.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ declare module "*.svg" {
2222
}
2323

2424
declare module "*.png" {
25-
const value: any;
25+
const value: string;
26+
export = value;
27+
}
28+
29+
declare module "*.gif" {
30+
const value: string;
2631
export = value;
2732
}
2833

packages/brick-container/webpack/common.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ module.exports = () => {
158158
)}`,
159159
to: "assets/illustrations",
160160
},
161+
{
162+
from: `${path.resolve(
163+
require.resolve("@next-core/brick-icons/package.json"),
164+
"../dist/image-icons"
165+
)}`,
166+
to: "assets/image-icons",
167+
},
161168
],
162169
}),
163170
new HtmlWebpackPlugin({

packages/brick-icons/rollup.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { rollupFactory, rollupPlugins } from "@next-core/rollup-config-factory";
23
import svgr from "@svgr/rollup";
34

@@ -48,5 +49,12 @@ export default rollupFactory({
4849
},
4950
],
5051
}),
52+
rollupPlugins.url({
53+
include: ["**/*.png", "**/*.gif"],
54+
fileName: "[dirname][name].[hash][extname]",
55+
destDir: "dist/image-icons",
56+
limit: 0,
57+
sourceDir: path.join(__dirname, "src/icons/image"),
58+
}),
5159
],
5260
});

packages/brick-icons/scripts/pre-build.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ const flattenIcons = klawSync(iconsDir, {
3131
return { category, relativePath, basename };
3232
});
3333

34+
const imageIconsDir = path.join(iconsDir, "image");
35+
const flattenImageIcons = klawSync(imageIconsDir, {
36+
depthLimit: 1,
37+
nodir: true,
38+
filter: (item) => item.path.endsWith(".png") || item.path.endsWith(".gif"),
39+
traverseAll: true,
40+
}).map((item) => {
41+
const filename = path.basename(item.path);
42+
const relativePath = `image/${filename}`;
43+
const basename = filename.replace(/\.(png|gif)$/, "-$1");
44+
return { category: "image", relativePath, basename };
45+
});
46+
47+
flattenIcons.push(...flattenImageIcons);
48+
3449
const groupedIcons = Object.entries(_.groupBy(flattenIcons, "category"));
3550

3651
for (const [category, icons] of groupedIcons) {
@@ -42,20 +57,26 @@ for (const [category, icons] of groupedIcons) {
4257
);
4358
const exports = `export const ${changeCase.camelCase(category)}Category = {
4459
${icons
45-
.map(
46-
(icon) =>
47-
` "${changeCase.paramCase(icon.basename)}": ${changeCase.pascalCase(
48-
category
49-
)}${changeCase.pascalCase(icon.basename)},`
50-
)
60+
.map((icon) => {
61+
const specifier = `${changeCase.pascalCase(
62+
category
63+
)}${changeCase.pascalCase(icon.basename)}`;
64+
return ` "${changeCase.paramCase(icon.basename)}": ${
65+
category === "image"
66+
? `() => (<img src={\`\${__webpack_public_path__}assets/image-icons/\${${specifier}}\`} style={{ width: "1em", height: "1em", verticalAlign: "baseline" }} />)`
67+
: specifier
68+
},`;
69+
})
5170
.join(os.EOL)}
5271
};`;
53-
const content = imports.concat(exports).join(os.EOL);
72+
const content = ['import React from "react";']
73+
.concat(imports, exports)
74+
.join(os.EOL);
5475

5576
const categoryTsPath = path.join(
5677
process.cwd(),
5778
"src/generated/icons",
58-
`${category}.ts`
79+
`${category}.tsx`
5980
);
6081
fs.outputFileSync(
6182
categoryTsPath,

0 commit comments

Comments
 (0)