@tinloof/typed-svg-sprite 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,13 +1,19 @@
1
- # @tinloof/typed-svg-sprite
2
-
3
- ## 0.0.1
4
-
5
- ### Patch Changes
6
-
7
- - Initial release of @tinloof/typed-svg-sprite
8
- - Generate optimized SVG sprites with full TypeScript support
9
- - CLI tool for generating sprites from SVG files
10
- - Next.js integration with `withSpriteLoader`
11
- - Auto-generated TypeScript definitions
12
- - Auto-generated React Icon component
13
- - Watch mode for development
1
+ # @tinloof/typed-svg-sprite
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - b2234dd: Added astro integration
8
+
9
+ ## 0.0.1
10
+
11
+ ### Patch Changes
12
+
13
+ - Initial release of @tinloof/typed-svg-sprite
14
+ - Generate optimized SVG sprites with full TypeScript support
15
+ - CLI tool for generating sprites from SVG files
16
+ - Next.js integration with `withSpriteLoader`
17
+ - Auto-generated TypeScript definitions
18
+ - Auto-generated React Icon component
19
+ - Watch mode for development
package/README.md CHANGED
@@ -1,190 +1,230 @@
1
- # @tinloof/typed-svg-sprite
2
-
3
- > Generate optimized SVG sprites with full TypeScript support
4
-
5
- Automatically generates SVG sprite files with type-safe TypeScript definitions and a ready-to-use React component.
6
-
7
- ## Installation
8
-
9
- ```bash
10
- npm install @tinloof/typed-svg-sprite
11
- ```
12
-
13
- ## Quick Start
14
-
15
- ### CLI
16
-
17
- ```bash
18
- # Generate sprite
19
- typed-svg-sprite --input public/icons --output public/sprite.svg
20
-
21
- # Watch mode
22
- typed-svg-sprite -i public/icons -o public/sprite.svg --watch
23
- ```
24
-
25
- ### Next.js
26
-
27
- ```typescript
28
- // next.config.ts
29
- import { withSpriteLoader } from "@tinloof/typed-svg-sprite/next";
30
-
31
- export default withSpriteLoader({});
32
- ```
33
-
34
- Place SVGs in `public/icons/` and use:
35
-
36
- ```typescript
37
- import { HOME, SETTINGS } from "@/generated/icons";
38
- import { Icon } from "@/generated/Icon";
39
-
40
- function MyComponent() {
41
- return (
42
- <>
43
- <Icon href={HOME} />
44
- <Icon href={SETTINGS} size={32} />
45
- </>
46
- );
47
- }
48
- ```
49
-
50
- ## Usage
51
-
52
- ### CLI Options
53
-
54
- ```bash
55
- typed-svg-sprite --input <dir> --output <file> [options]
56
-
57
- Options:
58
- -i, --input <dir> Directory containing SVG files
59
- -o, --output <file> Output sprite file path
60
- -w, --watch Watch for changes and regenerate
61
- -h, --help Show help message
62
- ```
63
-
64
- ### Next.js Configuration
65
-
66
- ```typescript
67
- export default withSpriteLoader(
68
- {
69
- // your existing Next.js config
70
- },
71
- {
72
- inputDir?: string; // default: "public/icons"
73
- outputFile?: string; // default: "public/sprite.svg"
74
- url?: string; // default: "/"
75
- filename?: string; // default: "sprite.svg"
76
- typesOutputFile?: string; // default: "generated/icons.ts"
77
- generateIconComponent?: boolean; // default: true
78
- iconComponentOutputFile?: string; // default: "generated/Icon.tsx"
79
- }
80
- );
81
- ```
82
-
83
- ## Generated Files
84
-
85
- ### 1. Sprite (`public/sprite.svg`)
86
-
87
- ```xml
88
- <svg xmlns="http://www.w3.org/2000/svg" style="display:none">
89
- <symbol id="a" viewBox="0 0 24 24"><!-- icon content --></symbol>
90
- <symbol id="b" viewBox="0 0 24 24"><!-- icon content --></symbol>
91
- </svg>
92
- ```
93
-
94
- ### 2. TypeScript Types (`generated/icons.ts`)
95
-
96
- ```typescript
97
- export enum IconId {
98
- HOME = "a",
99
- SETTINGS = "b",
100
- }
101
-
102
- export type IconHref = `/sprite.svg#${IconId}`;
103
-
104
- export const HOME: IconHref = "/sprite.svg#a";
105
- export const SETTINGS: IconHref = "/sprite.svg#b";
106
-
107
- export function getIconHref(iconId: IconId): IconHref;
108
- export const allIcons: IconId[];
109
- ```
110
-
111
- ### 3. React Component (`generated/Icon.tsx`)
112
-
113
- ```typescript
114
- import { IconHref } from "./icons";
115
-
116
- export interface IconProps extends React.SVGProps<SVGSVGElement> {
117
- href: IconHref;
118
- size?: number | string;
119
- }
120
-
121
- export function Icon({ href, size = 24, ...props }: IconProps) {
122
- // ...
123
- }
124
- ```
125
-
126
- ## Examples
127
-
128
- ### Basic Usage
129
-
130
- ```typescript
131
- import { HOME, SEARCH, SETTINGS } from "@/generated/icons";
132
- import { Icon } from "@/generated/Icon";
133
-
134
- <Icon href={HOME} />
135
- <Icon href={SEARCH} size={20} />
136
- <Icon href={SETTINGS} className="text-blue-500" />
137
- ```
138
-
139
- ### Dynamic Icons
140
-
141
- ```typescript
142
- import { IconId, getIconHref } from "@/generated/icons";
143
-
144
- function DynamicIcon({ iconId }: { iconId: IconId }) {
145
- return <Icon href={getIconHref(iconId)} />;
146
- }
147
- ```
148
-
149
- ### Build Script Integration
150
-
151
- ```json
152
- {
153
- "scripts": {
154
- "generate:icons": "typed-svg-sprite --input public/icons --output public/sprite.svg",
155
- "build": "npm run generate:icons && next build"
156
- }
157
- }
158
- ```
159
-
160
- ### Without React
161
-
162
- ```bash
163
- # Generate sprite and types
164
- typed-svg-sprite --input src/icons --output public/sprite.svg
165
-
166
- # Use generated types
167
- import { HOME, SETTINGS } from "./generated/icons";
168
-
169
- // In your HTML/JS
170
- <svg><use href={HOME} /></svg>
171
- ```
172
-
173
- ## How It Works
174
-
175
- 1. Scans directory for `.svg` files
176
- 2. Extracts and optimizes SVG content
177
- 3. Generates compact base-52 IDs (`a`, `b`, `aa`, etc.)
178
- 4. Combines into single sprite file
179
- 5. Generates TypeScript types with file-based names
180
- 6. Generates React component (optional)
181
-
182
- **Symbol IDs**: Short IDs (`a`, `b`) in sprite, original names (`HOME`, `SETTINGS`) in TypeScript exports.
183
-
184
- ## Roadmap
185
-
186
- - [ ] Integrate SVGO for advanced SVG optimization
187
-
188
- ## License
189
-
190
- MIT
1
+ # @tinloof/typed-svg-sprite
2
+
3
+ > Generate optimized SVG sprites with full TypeScript support
4
+
5
+ Automatically generates SVG sprite files with type-safe TypeScript definitions and a ready-to-use React component. Works with Next.js, Astro, or standalone via CLI.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @tinloof/typed-svg-sprite
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ### CLI
16
+
17
+ ```bash
18
+ # Generate sprite
19
+ typed-svg-sprite --input public/icons --output public/sprite.svg
20
+
21
+ # Watch mode
22
+ typed-svg-sprite -i public/icons -o public/sprite.svg --watch
23
+ ```
24
+
25
+ ### Next.js
26
+
27
+ ```typescript
28
+ // next.config.ts
29
+ import { withSpriteLoader } from "@tinloof/typed-svg-sprite/next";
30
+
31
+ export default withSpriteLoader({});
32
+ ```
33
+
34
+ Place SVGs in `public/icons/` and use:
35
+
36
+ ```typescript
37
+ import { HOME, SETTINGS } from "@/generated/icons";
38
+ import { Icon } from "@/generated/Icon";
39
+
40
+ function MyComponent() {
41
+ return (
42
+ <>
43
+ <Icon href={HOME} />
44
+ <Icon href={SETTINGS} size={32} />
45
+ </>
46
+ );
47
+ }
48
+ ```
49
+
50
+ ### Astro
51
+
52
+ ```typescript
53
+ // astro.config.mjs
54
+ import { defineConfig } from "astro/config";
55
+ import { svgSprite } from "@tinloof/typed-svg-sprite/astro";
56
+
57
+ export default defineConfig({
58
+ integrations: [svgSprite()],
59
+ });
60
+ ```
61
+
62
+ Place SVGs in `public/icons/` and use:
63
+
64
+ ```typescript
65
+ import { HOME, SETTINGS } from "../generated/icons";
66
+ import { Icon } from "../generated/Icon";
67
+
68
+ <Icon href={HOME} />
69
+ <Icon href={SETTINGS} size={32} />
70
+ ```
71
+
72
+ ## Usage
73
+
74
+ ### CLI Options
75
+
76
+ ```bash
77
+ typed-svg-sprite --input <dir> --output <file> [options]
78
+
79
+ Options:
80
+ -i, --input <dir> Directory containing SVG files
81
+ -o, --output <file> Output sprite file path
82
+ -w, --watch Watch for changes and regenerate
83
+ -h, --help Show help message
84
+ ```
85
+
86
+ ### Next.js Configuration
87
+
88
+ ```typescript
89
+ export default withSpriteLoader(
90
+ {
91
+ // your existing Next.js config
92
+ },
93
+ {
94
+ inputDir?: string; // default: "public/icons"
95
+ outputFile?: string; // default: "public/sprite.svg"
96
+ url?: string; // default: "/"
97
+ filename?: string; // default: "sprite.svg"
98
+ typesOutputFile?: string; // default: "generated/icons.ts"
99
+ generateIconComponent?: boolean; // default: true
100
+ iconComponentOutputFile?: string; // default: "generated/Icon.tsx"
101
+ }
102
+ );
103
+ ```
104
+
105
+ ### Astro Configuration
106
+
107
+ ```typescript
108
+ export default defineConfig({
109
+ integrations: [
110
+ svgSprite({
111
+ inputDir?: string; // default: "public/icons"
112
+ outputFile?: string; // default: "public/sprite.svg"
113
+ url?: string; // default: "/"
114
+ filename?: string; // default: "sprite.svg"
115
+ typesOutputFile?: string; // default: "src/generated/icons.ts"
116
+ generateIconComponent?: boolean; // default: true
117
+ iconComponentOutputFile?: string; // default: "src/generated/Icon.tsx"
118
+ }),
119
+ ],
120
+ });
121
+ ```
122
+
123
+ ## Generated Files
124
+
125
+ ### 1. Sprite (`public/sprite.svg`)
126
+
127
+ ```xml
128
+ <svg xmlns="http://www.w3.org/2000/svg" style="display:none">
129
+ <symbol id="a" viewBox="0 0 24 24"><!-- icon content --></symbol>
130
+ <symbol id="b" viewBox="0 0 24 24"><!-- icon content --></symbol>
131
+ </svg>
132
+ ```
133
+
134
+ ### 2. TypeScript Types (`generated/icons.ts`)
135
+
136
+ ```typescript
137
+ export enum IconId {
138
+ HOME = "a",
139
+ SETTINGS = "b",
140
+ }
141
+
142
+ export type IconHref = `/sprite.svg#${IconId}`;
143
+
144
+ export const HOME: IconHref = "/sprite.svg#a";
145
+ export const SETTINGS: IconHref = "/sprite.svg#b";
146
+
147
+ export function getIconHref(iconId: IconId): IconHref;
148
+ export const allIcons: IconId[];
149
+ ```
150
+
151
+ ### 3. React Component (`generated/Icon.tsx`)
152
+
153
+ ```typescript
154
+ import { IconHref } from "./icons";
155
+
156
+ export interface IconProps extends React.SVGProps<SVGSVGElement> {
157
+ href: IconHref;
158
+ size?: number | string;
159
+ }
160
+
161
+ export function Icon({ href, size = 24, ...props }: IconProps) {
162
+ // ...
163
+ }
164
+ ```
165
+
166
+ ## Examples
167
+
168
+ ### Basic Usage
169
+
170
+ ```typescript
171
+ import { HOME, SEARCH, SETTINGS } from "@/generated/icons";
172
+ import { Icon } from "@/generated/Icon";
173
+
174
+ <Icon href={HOME} />
175
+ <Icon href={SEARCH} size={20} />
176
+ <Icon href={SETTINGS} className="text-blue-500" />
177
+ ```
178
+
179
+ ### Dynamic Icons
180
+
181
+ ```typescript
182
+ import { IconId, getIconHref } from "@/generated/icons";
183
+
184
+ function DynamicIcon({ iconId }: { iconId: IconId }) {
185
+ return <Icon href={getIconHref(iconId)} />;
186
+ }
187
+ ```
188
+
189
+ ### Build Script Integration
190
+
191
+ ```json
192
+ {
193
+ "scripts": {
194
+ "generate:icons": "typed-svg-sprite --input public/icons --output public/sprite.svg",
195
+ "build": "npm run generate:icons && next build"
196
+ }
197
+ }
198
+ ```
199
+
200
+ ### Without React
201
+
202
+ ```bash
203
+ # Generate sprite and types
204
+ typed-svg-sprite --input src/icons --output public/sprite.svg
205
+
206
+ # Use generated types
207
+ import { HOME, SETTINGS } from "./generated/icons";
208
+
209
+ // In your HTML/JS
210
+ <svg><use href={HOME} /></svg>
211
+ ```
212
+
213
+ ## How It Works
214
+
215
+ 1. Scans directory for `.svg` files
216
+ 2. Extracts and optimizes SVG content
217
+ 3. Generates compact base-52 IDs (`a`, `b`, `aa`, etc.)
218
+ 4. Combines into single sprite file
219
+ 5. Generates TypeScript types with file-based names
220
+ 6. Generates React component (optional)
221
+
222
+ **Symbol IDs**: Short IDs (`a`, `b`) in sprite, original names (`HOME`, `SETTINGS`) in TypeScript exports.
223
+
224
+ ## Roadmap
225
+
226
+ - [ ] Integrate SVGO for advanced SVG optimization
227
+
228
+ ## License
229
+
230
+ MIT
@@ -0,0 +1,63 @@
1
+ import type { AstroIntegration } from "astro";
2
+ export interface SpriteIntegrationOptions {
3
+ /**
4
+ * URL path where the sprite file will be served from.
5
+ * @default "/"
6
+ */
7
+ url?: string;
8
+ /**
9
+ * Filename for the sprite file.
10
+ * @default "sprite.svg"
11
+ */
12
+ filename?: string;
13
+ /**
14
+ * Directory containing SVG files to include in the sprite.
15
+ * Relative to project root.
16
+ * @default "public/icons"
17
+ */
18
+ inputDir?: string;
19
+ /**
20
+ * Output path for the sprite file.
21
+ * Relative to project root.
22
+ * @default "public/sprite.svg"
23
+ */
24
+ outputFile?: string;
25
+ /**
26
+ * Output path for the TypeScript types file.
27
+ * Relative to project root.
28
+ * @default "src/generated/icons.ts"
29
+ */
30
+ typesOutputFile?: string;
31
+ /**
32
+ * Whether to generate the Icon component.
33
+ * - `true`: generates both Astro and React components
34
+ * - `false`: generates no component
35
+ * - `{ astro?: boolean, react?: boolean }`: choose which to generate
36
+ * @default { astro: true }
37
+ */
38
+ generateIconComponent?: boolean | {
39
+ astro?: boolean;
40
+ react?: boolean;
41
+ };
42
+ /**
43
+ * Output path for the Icon component file (without extension).
44
+ * Extension will be added based on component type (.astro, .tsx).
45
+ * Relative to project root.
46
+ * @default "src/generated/Icon"
47
+ */
48
+ iconComponentOutputFile?: string;
49
+ }
50
+ /**
51
+ * Astro integration for generating SVG sprites with TypeScript types
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * // astro.config.mjs
56
+ * import { svgSprite } from '@tinloof/typed-svg-sprite/astro';
57
+ *
58
+ * export default defineConfig({
59
+ * integrations: [svgSprite()]
60
+ * });
61
+ * ```
62
+ */
63
+ export declare function svgSprite(options?: SpriteIntegrationOptions): AstroIntegration;
package/dist/astro.js ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.svgSprite = svgSprite;
4
+ const shared_js_1 = require("./shared.js");
5
+ /**
6
+ * Astro integration for generating SVG sprites with TypeScript types
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // astro.config.mjs
11
+ * import { svgSprite } from '@tinloof/typed-svg-sprite/astro';
12
+ *
13
+ * export default defineConfig({
14
+ * integrations: [svgSprite()]
15
+ * });
16
+ * ```
17
+ */
18
+ function svgSprite(options) {
19
+ const inputDir = options?.inputDir ?? "public/icons";
20
+ const outputFile = options?.outputFile ?? "public/sprite.svg";
21
+ const spriteUrl = options?.url ?? "/";
22
+ const spriteFilename = options?.filename ?? "sprite.svg";
23
+ const typesOutputFile = options?.typesOutputFile ?? "src/generated/icons.ts";
24
+ const generateIcon = options?.generateIconComponent === false
25
+ ? false
26
+ : options?.generateIconComponent === true
27
+ ? true
28
+ : options?.generateIconComponent ?? { astro: true };
29
+ const iconComponentOutputFile = options?.iconComponentOutputFile ?? "src/generated/Icon";
30
+ return {
31
+ name: "typed-svg-sprite",
32
+ hooks: {
33
+ "astro:config:setup": ({ command }) => {
34
+ // Generate on startup
35
+ (0, shared_js_1.generateSpriteAndTypes)(inputDir, outputFile, spriteUrl, spriteFilename, typesOutputFile, generateIcon, iconComponentOutputFile);
36
+ // Watch in dev mode
37
+ if (command === "dev") {
38
+ (0, shared_js_1.startWatcher)(inputDir, outputFile, spriteUrl, spriteFilename, typesOutputFile, generateIcon, iconComponentOutputFile);
39
+ }
40
+ },
41
+ "astro:build:start": () => {
42
+ // Regenerate before build
43
+ (0, shared_js_1.generateSpriteAndTypes)(inputDir, outputFile, spriteUrl, spriteFilename, typesOutputFile, generateIcon, iconComponentOutputFile);
44
+ },
45
+ },
46
+ };
47
+ }
package/dist/cli.js CHANGED
@@ -68,21 +68,21 @@ function parseArgs() {
68
68
  return options;
69
69
  }
70
70
  function printHelp() {
71
- console.log(`
72
- SVG Sprite Generator
73
-
74
- Usage:
75
- typed-svg-sprite --input <dir> --output <file> [options]
76
-
77
- Options:
78
- -i, --input <dir> Directory containing SVG files
79
- -o, --output <file> Output sprite file path
80
- -w, --watch Watch for changes and regenerate
81
- -h, --help Show this help message
82
-
83
- Examples:
84
- typed-svg-sprite --input public/icons --output public/sprite.svg
85
- typed-svg-sprite -i ./icons -o ./dist/sprite.svg --watch
71
+ console.log(`
72
+ SVG Sprite Generator
73
+
74
+ Usage:
75
+ typed-svg-sprite --input <dir> --output <file> [options]
76
+
77
+ Options:
78
+ -i, --input <dir> Directory containing SVG files
79
+ -o, --output <file> Output sprite file path
80
+ -w, --watch Watch for changes and regenerate
81
+ -h, --help Show this help message
82
+
83
+ Examples:
84
+ typed-svg-sprite --input public/icons --output public/sprite.svg
85
+ typed-svg-sprite -i ./icons -o ./dist/sprite.svg --watch
86
86
  `);
87
87
  }
88
88
  function runSpriteGeneration(inputDir, outputFile) {
@@ -21,4 +21,8 @@ export interface IconComponentGenerationOptions {
21
21
  /**
22
22
  * Generate React Icon component file
23
23
  */
24
- export declare function generateIconComponent(options: IconComponentGenerationOptions): void;
24
+ export declare function generateReactIconComponent(options: IconComponentGenerationOptions): void;
25
+ /**
26
+ * Generate Astro Icon component file
27
+ */
28
+ export declare function generateAstroIconComponent(options: IconComponentGenerationOptions): void;