-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] SVG Sprite 빌드 시스템 설정 #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0557a45
chore(ui): svg-sprite, svgo, tsx 패키지 추가 (#47)
ehye1 c7abdc8
feat(ui): SVG Sprite 빌드 스크립트 및 Icon 컴포넌트 구현 (#47)
ehye1 f550d8a
feat(web): SvgSprite 인라인 주입 및 layout.tsx 연결 (#47)
ehye1 26a8ca6
fix(web): sprite.svg 미존재 시 SvgSprite 크래시 방지 (#47)
ehye1 5d8c78b
fix(web): .gitignore sprite.svg 경로 수정 (#47)
ehye1 6cec4fe
perf(web): SvgSprite 파일 읽기 모듈 레벨 캐싱 추가 (#47)
ehye1 ff64da8
fix(ui): iconNames.ts 생성 시 아이콘 이름 알파벳 순 정렬 추가 (#47)
ehye1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -34,3 +34,6 @@ yarn-error.log* | |
| # typescript | ||
| *.tsbuildinfo | ||
| next-env.d.ts | ||
|
|
||
| #icons | ||
| /public/sprite.svg | ||
This file contains hidden or 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
Empty file.
This file contains hidden or 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,22 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
|
|
||
| const spritePath = path.resolve(process.cwd(), "public/sprite.svg"); | ||
| let cachedSprite: string | null | undefined; | ||
|
|
||
| export const SvgSprite = () => { | ||
| if (cachedSprite === undefined) { | ||
| cachedSprite = fs.existsSync(spritePath) | ||
| ? fs.readFileSync(spritePath, "utf-8") | ||
| : null; | ||
| } | ||
|
|
||
| if (!cachedSprite) return null; | ||
|
|
||
| return ( | ||
| <div | ||
| dangerouslySetInnerHTML={{ __html: cachedSprite }} | ||
| style={{ display: "none" }} | ||
| /> | ||
| ); | ||
| }; |
This file contains hidden or 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
Empty file.
This file contains hidden or 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,46 @@ | ||
| import type { IconName } from "./iconNames"; | ||
|
|
||
| interface IconProps extends React.SVGProps<SVGSVGElement> { | ||
| name: IconName; | ||
| size?: number | string; | ||
| width?: number | string; | ||
| height?: number | string; | ||
| className?: string; | ||
| rotate?: 90 | 180 | 270; | ||
| ariaHidden?: boolean; | ||
| } | ||
|
|
||
| export const Icon = ({ | ||
| name, | ||
| size, | ||
| width, | ||
| height, | ||
| className, | ||
| rotate, | ||
| ariaHidden = true, | ||
| ...rest | ||
| }: IconProps) => { | ||
| const w = width ?? size ?? 20; | ||
| const h = height ?? size ?? 20; | ||
|
|
||
| const rotateClass = | ||
| rotate === 90 | ||
| ? "rotate-90" | ||
| : rotate === 180 | ||
| ? "rotate-180" | ||
| : rotate === 270 | ||
| ? "rotate-[270deg]" | ||
| : ""; | ||
|
|
||
| return ( | ||
| <svg | ||
| width={typeof w === "number" ? `${w}px` : w} | ||
| height={typeof h === "number" ? `${h}px` : h} | ||
| className={`inline-block ${rotateClass} ${className ?? ""}`} | ||
| aria-hidden={ariaHidden} | ||
| {...rest} | ||
| > | ||
| <use href={`#icon-${name}`} /> | ||
| </svg> | ||
| ); | ||
| }; | ||
17 changes: 17 additions & 0 deletions
17
packages/timo-design-system/src/icons/generate-icon-names.ts
This file contains hidden or 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,17 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
|
|
||
| const sourceDir = path.resolve(process.cwd(), "src/icons/source"); | ||
| const outputFile = path.resolve(process.cwd(), "src/icons/iconNames.ts"); | ||
|
|
||
| const names = fs | ||
| .readdirSync(sourceDir) | ||
| .filter((f) => f.endsWith(".svg")) | ||
| .map((f) => path.basename(f, ".svg")) | ||
| .sort(); | ||
|
|
||
| const content = `// auto-generated | ||
| export type IconName =\n | ${names.map((n) => `'${n}'`).join("\n | ")};\n`; | ||
|
|
||
| fs.writeFileSync(outputFile, content); | ||
|
ehye1 marked this conversation as resolved.
|
||
| console.log("✅ iconNames.ts 생성 완료"); | ||
This file contains hidden or 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,45 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
|
|
||
| import SVGSpriter from "svg-sprite"; | ||
|
|
||
| const sourceDir = path.resolve(process.cwd(), "src/icons/source"); | ||
| const outputDir = path.resolve(process.cwd(), "../../apps/timo-web/public"); | ||
|
|
||
| const spriter = new SVGSpriter({ | ||
| dest: outputDir, | ||
| mode: { | ||
| symbol: { | ||
| dest: ".", | ||
| sprite: "sprite.svg", | ||
| }, | ||
| }, | ||
| shape: { | ||
| id: { | ||
| separator: "", | ||
| generator: (name: string) => `icon-${path.basename(name, ".svg")}`, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const files = fs.readdirSync(sourceDir).filter((f) => f.endsWith(".svg")); | ||
|
|
||
| for (const file of files) { | ||
| const filePath = path.join(sourceDir, file); | ||
| spriter.add(filePath, file, fs.readFileSync(filePath, "utf-8")); | ||
| } | ||
|
|
||
| spriter.compile((error, result) => { | ||
| if (error) throw error; | ||
|
|
||
| for (const mode of Object.values(result)) { | ||
| for (const resource of Object.values( | ||
| mode as Record<string, { path: string; contents: Buffer }>, | ||
| )) { | ||
| fs.mkdirSync(path.dirname(resource.path), { recursive: true }); | ||
| fs.writeFileSync(resource.path, resource.contents); | ||
| } | ||
| } | ||
|
|
||
| console.log("✅ sprite.svg 생성 완료"); | ||
| }); |
This file contains hidden or 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,2 @@ | ||
| // auto-generated | ||
| export type IconName = never; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or 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 +1,2 @@ | ||
| export {}; | ||
| export { Icon } from "./Icon"; | ||
| export type { IconName } from "./iconNames"; |
This file contains hidden or 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,14 @@ | ||
| export default { | ||
| multipass: true, | ||
| plugins: [ | ||
| { | ||
| name: "preset-default", | ||
| params: { | ||
| overrides: { | ||
| removeViewBox: false, | ||
| }, | ||
| }, | ||
| }, | ||
| "removeDimensions", | ||
| ], | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.