Skip to content

Commit

Permalink
change inline export structure
Browse files Browse the repository at this point in the history
  • Loading branch information
alexasselin008 committed Feb 19, 2025
1 parent 4cf6942 commit aa66600
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
6 changes: 5 additions & 1 deletion packages/svg-icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
],
"exports": {
"./icons/*.svg": "./dist/icons/*.svg",
"./icons/inline/*": "./dist/icons/inline/*",
"./icons/inline": {
"types": "./dist/icons/inline/index.d.ts",
"import": "./dist/icons/inline/index.js",
"default": "./dist/icons/inline/index.js"
},
"./rich-icons/*.svg": "./dist/rich-icons/*.svg"
},
"scripts": {
Expand Down
40 changes: 34 additions & 6 deletions packages/svg-icons/scripts/buildInlineIcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import path from "path";

import { IconsInlineDistDirectory, IconsOptimizedDirectory } from "./constants.ts";

//
const optimizedIconsPath = IconsOptimizedDirectory;

console.log("⚙️ Generating inline icons...\n");
/**
* Converts a file path to a file name.
* @example fileNameConverter("C:\Dev\wl-hopper\packages\svg-icons\src\optimized-rich-icons\action-list-24.svg") // ActionListIcon24
*/
function fileNameConverter(filePath: string) {
const fileName = path.basename(filePath, ".svg");

return fileName.split("-").map(s => s.charAt(0).toUpperCase() + s.slice(1)).join("") + "Icon";
}

const files = fs.readdirSync(optimizedIconsPath, { recursive: true, withFileTypes: true });
const svgFiles = files.filter(file => file.isFile() && file.name.endsWith(".svg"));
Expand All @@ -15,11 +22,32 @@ const svgFiles = files.filter(file => file.isFile() && file.name.endsWith(".svg"
fs.rmSync(IconsInlineDistDirectory, { recursive: true, force: true });
fs.mkdirSync(IconsInlineDistDirectory, { recursive: true });

svgFiles.forEach(file => {
const inlinedSvgs = svgFiles.map(file => {
const contents = fs.readFileSync(path.resolve(file.path, file.name), "utf8");
// TODO: fix the /, there is probably a method to join those paths
fs.writeFileSync(`${IconsInlineDistDirectory}/${file.name.replace(".svg", ".js")}`, `export default \`${contents}\``);
fs.writeFileSync(`${IconsInlineDistDirectory}/${file.name.replace(".svg", ".d.ts")}`, "declare const _default: string; export default _default;");
const name = fileNameConverter(file.name);
const fileName = `${name}.js`;
const location = path.resolve(IconsInlineDistDirectory, fileName);
fs.writeFileSync(location, `export default \`${contents}\``);

return {
name,
fileName,
location
};
});

// index barrel file
fs.writeFileSync(path.resolve(IconsInlineDistDirectory, "index.js"), inlinedSvgs.map(file => {
const name = file.name;

return `export { default as ${name} } from "./${file.fileName}";\n`;
}).join(""));

// types barrel file
fs.writeFileSync(path.resolve(IconsInlineDistDirectory, "index.d.ts"), inlinedSvgs.map(file => {
const name = file.name;

return `export const ${name}: string;\n`;
}).join(""));

console.log("✨ The inline icons have been generated!\n");

0 comments on commit aa66600

Please sign in to comment.