-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add lazy load variation for each icon (#2469)
- Loading branch information
1 parent
861d521
commit 1f6300c
Showing
4 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
src/react | ||
src/lazy | ||
src/svg/index.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
const REACT_ICONS_FOLDER = path.resolve("./src/react"); | ||
const LAZY_EXPORTS_FOLDER = path.resolve("./src/lazy"); | ||
|
||
if (!fs.existsSync(LAZY_EXPORTS_FOLDER)) { | ||
fs.mkdirSync(LAZY_EXPORTS_FOLDER); | ||
} | ||
|
||
const iconFiles = fs.readdirSync(REACT_ICONS_FOLDER).filter(file => file.endsWith(".tsx")); | ||
|
||
iconFiles.forEach(file => { | ||
const fileNameWithoutExtension = path.basename(file, ".tsx"); | ||
const lazyComponentCode = `import React, { lazy, Suspense } from 'react'; | ||
const ${fileNameWithoutExtension}Icon = lazy(() => import('../react/${fileNameWithoutExtension}')); | ||
const ${fileNameWithoutExtension} = ({ fallback = <div />, ...props }) => ( | ||
<Suspense fallback={fallback}> | ||
<${fileNameWithoutExtension}Icon {...props} /> | ||
</Suspense> | ||
); | ||
export default ${fileNameWithoutExtension}; | ||
`; | ||
|
||
fs.writeFileSync(path.join(LAZY_EXPORTS_FOLDER, `${fileNameWithoutExtension}.tsx`), lazyComponentCode); | ||
}); | ||
|
||
// Create an index.js file that exports all lazy-loaded components | ||
const indexContent = iconFiles | ||
.map(file => { | ||
const fileNameWithoutExtension = path.basename(file, ".tsx"); | ||
return `export { default as Lazy${fileNameWithoutExtension}Icon } from './${fileNameWithoutExtension}';`; | ||
}) | ||
.join("\n"); | ||
|
||
fs.writeFileSync(path.join(LAZY_EXPORTS_FOLDER, "index.ts"), indexContent); | ||
|
||
console.log(`Generated lazy components and index.ts for ${iconFiles.length} icons.`); |