style: Синхронизировать английскую документацию

This commit is contained in:
2026-07-15 13:41:24 +03:00
parent 44afec7cdb
commit e54ab4991c
43 changed files with 1081 additions and 1436 deletions

View File

@@ -1,153 +1,108 @@
# Next.js App Router Turbopack SVG Sprite Quick Start
# SVG Sprite for Next.js App Router with Turbopack
This guide targets the exact mode key `next@app/turbopack`: a generated typed React icon for the Next.js App Router and Turbopack.
A quick guide to creating an SVG sprite in a Next.js application using App Router and Turbopack.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`.
Choose a folder for the sprite. This example uses `assets/app-icons`, with source SVG files, including the `check.svg` used below, in `assets/svg-icons`.
Keep the config and source icons together:
Create `assets/app-icons/svg-sprite.config.json`:
```text
src/ui/icons/
├── icons/
├── check.svg
└── folder.svg
└── svg-sprite.config.ts
```
Use a plain default object export with no package import:
```ts
// src/ui/icons/svg-sprite.config.ts
export default {
mode: 'next@app/turbopack',
name: 'icons',
```json
{
"mode": "next@app/turbopack",
"name": "app",
"input": "../svg-icons/**/*.svg"
}
```
When `input` is omitted, SVG files are read from `./icons` relative to the config. A `.js` config with a default export and a `.json` config are also supported. Generate directly with:
The `input` path is relative to the config folder.
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts
```
Generate once per invocation and keep the exact Turbopack flags on both commands:
Add generation commands to `package.json`. Generated files are excluded from Git by default, so `predev` and `prebuild` rebuild the sprite before every start and build:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts",
"dev": "npm run sprites && next dev --turbopack",
"build": "npm run sprites && next build --turbopack"
"sprites": "npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json",
"predev": "npm run sprites",
"dev": "next dev --turbopack",
"prebuild": "npm run sprites",
"build": "next build --turbopack"
}
}
```
Do not add `predev` or `prebuild` hooks to these scripts; that would run generation twice. In CI, replace `latest` with an exact package version.
## Use the sprite
Generation creates a local `.gitignore`; commit that file once, but do not commit `.svg-sprite/`. Generated declarations are self-contained and do not require the package.
The value `name: "app"` creates the React component `AppIcon`.
### Production usage
Create the entry point `assets/app-icons/index.ts`:
The generated icon has no `'use client'` directive and is Server Component-compatible. Import it directly in an App Router page or layout:
```ts
export * from './.svg-sprite/index.js'
```
Use the component in a Server Component:
```tsx
// src/app/page.tsx
import {
IconsIcon,
iconsIconNames,
} from '../ui/icons/.svg-sprite/index.js'
// app/page.tsx
import { AppIcon } from '../assets/app-icons'
export default function Page() {
return (
<main>
<IconsIcon icon="folder" width={24} height={24} aria-label="Files" />
<p>{iconsIconNames.length} icons available</p>
</main>
<AppIcon
icon="check"
width={24}
height={24}
role="img"
aria-label="Done"
style={{
color: '#334155',
'--icon-color-2': '#f59e0b',
}}
/>
)
}
```
Turbopack resolves the generated `new URL('../sprite.svg', import.meta.url)` and CSS Module, emitting a separate SVG asset. Keep the mode and the `--turbopack` dev/build flags aligned.
`AppIcon` does not need `'use client'`. Turbopack automatically adds `sprite.svg` to the production build, so you do not need to move it to `public`.
## 2. Debug and preview
## Debug and preview
This section is optional. Only users who need the Viewer or icon previews should install:
Viewer displays all icons on one page so you can check their rendering, change colors, and inspect the related CSS variables. It is only needed for development and is installed separately:
```bash
npm install --save-dev @gromlab/svg-sprites
```
Viewer is interactive, so place the React bridge in a separate Client Component:
Create the Client Component `app/svg-sprite/SvgSpriteViewer.tsx`:
```tsx
// src/app/icon-debug/IconsViewer.tsx
'use client'
import { SpriteViewer } from '@gromlab/svg-sprites/react'
const sources = [
() => import('../../ui/icons/.svg-sprite/svg-sprite.manifest.js'),
]
() => import('../../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'),
] as const
export function IconsViewer() {
export function SvgSpriteViewer() {
return <SpriteViewer sources={sources} title="Project icons" />
}
```
Render it from the route's Server Component:
Create the route `app/svg-sprite/page.tsx`:
```tsx
// src/app/icon-debug/page.tsx
import { IconsViewer } from './IconsViewer'
import { notFound } from 'next/navigation'
export default function IconDebugPage() {
return <IconsViewer />
import { SvgSpriteViewer } from './SvgSpriteViewer'
export default function SvgSpritePage() {
if (process.env.NODE_ENV !== 'development') notFound()
return <SvgSpriteViewer />
}
```
Keep the route internal or development-only. Viewer is not part of the production icon runtime.
## 3. Type the config
Choose one of these two paths.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'next@app/turbopack',
name: 'icons',
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
### Without the package
Copy a mode-specific type directly into the config:
```ts
type LocalSpriteConfig = {
mode: 'next@app/turbopack'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'next@app/turbopack',
name: 'icons',
} satisfies LocalSpriteConfig
```
Run `npm run dev` and open `/svg-sprite`. In production, the route returns 404.

View File

@@ -1,153 +1,108 @@
# Next.js App Router Webpack SVG Sprite Quick Start
# SVG Sprite for Next.js App Router with Webpack
This guide targets the exact mode key `next@app/webpack`: a generated typed React icon for the Next.js App Router and Webpack 5.
A quick guide to creating an SVG sprite in a Next.js application using App Router and Webpack.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`.
Choose a folder for the sprite. This example uses `assets/app-icons`, with source SVG files, including the `check.svg` used below, in `assets/svg-icons`.
Keep the config adjacent to its source icons:
Create `assets/app-icons/svg-sprite.config.json`:
```text
src/ui/icons/
├── icons/
├── check.svg
└── folder.svg
└── svg-sprite.config.ts
```
Use a plain default object export with no package import:
```ts
// src/ui/icons/svg-sprite.config.ts
export default {
mode: 'next@app/webpack',
name: 'icons',
```json
{
"mode": "next@app/webpack",
"name": "app",
"input": "../svg-icons/**/*.svg"
}
```
When `input` is omitted, SVG files are read from `./icons` relative to the config. A `.js` config with a default export and a `.json` config are also supported. Generate directly with:
The `input` path is relative to the config folder.
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts
```
Generate once per invocation and keep the exact Webpack flags on both commands:
Add generation commands to `package.json`. Generated files are excluded from Git by default, so `predev` and `prebuild` rebuild the sprite before every start and build:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts",
"dev": "npm run sprites && next dev --webpack",
"build": "npm run sprites && next build --webpack"
"sprites": "npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json",
"predev": "npm run sprites",
"dev": "next dev --webpack",
"prebuild": "npm run sprites",
"build": "next build --webpack"
}
}
```
Do not add `predev` or `prebuild` hooks to these scripts; that would run generation twice. In CI, replace `latest` with an exact package version.
## Use the sprite
Generation creates a local `.gitignore`; commit that file once, but do not commit `.svg-sprite/`. Generated declarations are self-contained and do not require the package.
The value `name: "app"` creates the React component `AppIcon`.
### Production usage
Create the entry point `assets/app-icons/index.ts`:
The generated icon has no `'use client'` directive and is Server Component-compatible. Import it directly in an App Router page or layout:
```ts
export * from './.svg-sprite/index.js'
```
Use the component in a Server Component:
```tsx
// src/app/page.tsx
import {
IconsIcon,
iconsIconNames,
} from '../ui/icons/.svg-sprite/index.js'
// app/page.tsx
import { AppIcon } from '../assets/app-icons'
export default function Page() {
return (
<main>
<IconsIcon icon="folder" width={24} height={24} aria-label="Files" />
<p>{iconsIconNames.length} icons available</p>
</main>
<AppIcon
icon="check"
width={24}
height={24}
role="img"
aria-label="Done"
style={{
color: '#334155',
'--icon-color-2': '#f59e0b',
}}
/>
)
}
```
Webpack resolves the generated `new URL('../sprite.svg', import.meta.url)` and CSS Module, emitting a separate SVG asset. Keep the mode and the `--webpack` dev/build flags aligned. If custom Next.js webpack rules process SVG through SVGR, exclude `.svg-sprite/sprite.svg` from those rules.
`AppIcon` does not need `'use client'`. Next.js automatically adds `sprite.svg` to the production build, so you do not need to move it to `public`.
## 2. Debug and preview
## Debug and preview
This section is optional. Only users who need the Viewer or icon previews should install:
Viewer displays all icons on one page so you can check their rendering, change colors, and inspect the related CSS variables. It is only needed for development and is installed separately:
```bash
npm install --save-dev @gromlab/svg-sprites
```
Viewer is interactive, so place the React bridge in a separate Client Component:
Create the Client Component `app/svg-sprite/SvgSpriteViewer.tsx`:
```tsx
// src/app/icon-debug/IconsViewer.tsx
'use client'
import { SpriteViewer } from '@gromlab/svg-sprites/react'
const sources = [
() => import('../../ui/icons/.svg-sprite/svg-sprite.manifest.js'),
]
() => import('../../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'),
] as const
export function IconsViewer() {
export function SvgSpriteViewer() {
return <SpriteViewer sources={sources} title="Project icons" />
}
```
Render it from the route's Server Component:
Create the route `app/svg-sprite/page.tsx`:
```tsx
// src/app/icon-debug/page.tsx
import { IconsViewer } from './IconsViewer'
import { notFound } from 'next/navigation'
export default function IconDebugPage() {
return <IconsViewer />
import { SvgSpriteViewer } from './SvgSpriteViewer'
export default function SvgSpritePage() {
if (process.env.NODE_ENV !== 'development') notFound()
return <SvgSpriteViewer />
}
```
Keep the route internal or development-only. Viewer is not part of the production icon runtime.
## 3. Type the config
Choose one of these two paths.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'next@app/webpack',
name: 'icons',
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
### Without the package
Copy a mode-specific type directly into the config:
```ts
type LocalSpriteConfig = {
mode: 'next@app/webpack'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'next@app/webpack',
name: 'icons',
} satisfies LocalSpriteConfig
```
Run `npm run dev` and open `/svg-sprite`. In production, the route returns 404.

View File

@@ -1,140 +1,98 @@
# Next.js Pages Router Turbopack SVG Sprite Quick Start
# SVG Sprite for Next.js Pages Router with Turbopack
This guide targets the exact mode key `next@pages/turbopack`: a generated typed React icon for the Next.js Pages Router and Turbopack.
A quick guide to creating an SVG sprite in a Next.js application using Pages Router and Turbopack.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`.
Choose a folder for the sprite. This example uses `assets/app-icons`, with source SVG files, including the `check.svg` used below, in `assets/svg-icons`.
Keep the config and source icons together:
Create `assets/app-icons/svg-sprite.config.json`:
```text
src/ui/icons/
├── icons/
├── check.svg
└── folder.svg
└── svg-sprite.config.ts
```
Use a plain default object export with no package import:
```ts
// src/ui/icons/svg-sprite.config.ts
export default {
mode: 'next@pages/turbopack',
name: 'icons',
```json
{
"mode": "next@pages/turbopack",
"name": "app",
"input": "../svg-icons/**/*.svg"
}
```
When `input` is omitted, SVG files are read from `./icons` relative to the config. A `.js` config with a default export and a `.json` config are also supported. Generate directly with:
The `input` path is relative to the config folder.
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts
```
Generate once per invocation and keep the exact Turbopack flags on both commands:
Add generation commands to `package.json`. Generated files are excluded from Git by default, so `predev` and `prebuild` rebuild the sprite before every start and build:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts",
"dev": "npm run sprites && next dev --turbopack",
"build": "npm run sprites && next build --turbopack"
"sprites": "npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json",
"predev": "npm run sprites",
"dev": "next dev --turbopack",
"prebuild": "npm run sprites",
"build": "next build --turbopack"
}
}
```
Do not add `predev` or `prebuild` hooks to these scripts; that would run generation twice. In CI, replace `latest` with an exact package version.
## Use the sprite
Generation creates a local `.gitignore`; commit that file once, but do not commit `.svg-sprite/`. Generated declarations are self-contained and do not require the package.
The value `name: "app"` creates the React component `AppIcon`.
### Production usage
Create the entry point `assets/app-icons/index.ts`:
Import the generated component and icon-name list into a Pages Router page:
```ts
export * from './.svg-sprite/index.js'
```
Use the component on a page:
```tsx
// src/pages/index.tsx
import {
IconsIcon,
iconsIconNames,
} from '../ui/icons/.svg-sprite/index.js'
// pages/index.tsx
import { AppIcon } from '../assets/app-icons'
export default function HomePage() {
export default function Page() {
return (
<main>
<IconsIcon icon="folder" width={24} height={24} aria-label="Files" />
<p>{iconsIconNames.length} icons available</p>
</main>
<AppIcon
icon="check"
width={24}
height={24}
role="img"
aria-label="Done"
style={{
color: '#334155',
'--icon-color-2': '#f59e0b',
}}
/>
)
}
```
The component works with SSR, SSG, and client-side navigation. Turbopack resolves the generated SVG URL and CSS Module and emits a separate asset. Keep the mode and the `--turbopack` dev/build flags aligned.
The component works with SSR, SSG, and client-side navigation. Turbopack automatically adds `sprite.svg` to the production build, so you do not need to move it to `public`.
## 2. Debug and preview
## Debug and preview
This section is optional. Only users who need the Viewer or icon previews should install:
Viewer displays all icons on one page so you can check their rendering, change colors, and inspect the related CSS variables. It is only needed for development and is installed separately:
```bash
npm install --save-dev @gromlab/svg-sprites
```
Pages Router does not require an App Router Client Component boundary. Use the React bridge directly in a page with a static loader array:
Create the page `pages/svg-sprite.tsx`:
```tsx
// src/pages/icon-debug.tsx
import type { GetStaticProps } from 'next'
import { SpriteViewer } from '@gromlab/svg-sprites/react'
const sources = [
() => import('../ui/icons/.svg-sprite/svg-sprite.manifest.js'),
]
() => import('../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'),
] as const
export default function IconDebugPage() {
export default function SvgSpritePage() {
return <SpriteViewer sources={sources} title="Project icons" />
}
export const getStaticProps: GetStaticProps = () =>
process.env.NODE_ENV === 'development'
? { props: {} }
: { notFound: true }
```
Keep the page internal or development-only. Viewer is not part of the production icon runtime.
## 3. Type the config
Choose one of these two paths.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'next@pages/turbopack',
name: 'icons',
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
### Without the package
Copy a mode-specific type directly into the config:
```ts
type LocalSpriteConfig = {
mode: 'next@pages/turbopack'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'next@pages/turbopack',
name: 'icons',
} satisfies LocalSpriteConfig
```
Run `npm run dev` and open `/svg-sprite`. In production, the route returns 404.

View File

@@ -1,140 +1,98 @@
# Next.js Pages Router Webpack SVG Sprite Quick Start
# SVG Sprite for Next.js Pages Router with Webpack
This guide targets the exact mode key `next@pages/webpack`: a generated typed React icon for the Next.js Pages Router and Webpack 5.
A quick guide to creating an SVG sprite in a Next.js application using Pages Router and Webpack.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`.
Choose a folder for the sprite. This example uses `assets/app-icons`, with source SVG files, including the `check.svg` used below, in `assets/svg-icons`.
Keep the config adjacent to its source icons:
Create `assets/app-icons/svg-sprite.config.json`:
```text
src/ui/icons/
├── icons/
├── check.svg
└── folder.svg
└── svg-sprite.config.ts
```
Use a plain default object export with no package import:
```ts
// src/ui/icons/svg-sprite.config.ts
export default {
mode: 'next@pages/webpack',
name: 'icons',
```json
{
"mode": "next@pages/webpack",
"name": "app",
"input": "../svg-icons/**/*.svg"
}
```
When `input` is omitted, SVG files are read from `./icons` relative to the config. A `.js` config with a default export and a `.json` config are also supported. Generate directly with:
The `input` path is relative to the config folder.
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts
```
Generate once per invocation and keep the exact Webpack flags on both commands:
Add generation commands to `package.json`. Generated files are excluded from Git by default, so `predev` and `prebuild` rebuild the sprite before every start and build:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts",
"dev": "npm run sprites && next dev --webpack",
"build": "npm run sprites && next build --webpack"
"sprites": "npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json",
"predev": "npm run sprites",
"dev": "next dev --webpack",
"prebuild": "npm run sprites",
"build": "next build --webpack"
}
}
```
Do not add `predev` or `prebuild` hooks to these scripts; that would run generation twice. In CI, replace `latest` with an exact package version.
## Use the sprite
Generation creates a local `.gitignore`; commit that file once, but do not commit `.svg-sprite/`. Generated declarations are self-contained and do not require the package.
The value `name: "app"` creates the React component `AppIcon`.
### Production usage
Create the entry point `assets/app-icons/index.ts`:
Import the generated component and icon-name list into a Pages Router page:
```ts
export * from './.svg-sprite/index.js'
```
Use the component on a page:
```tsx
// src/pages/index.tsx
import {
IconsIcon,
iconsIconNames,
} from '../ui/icons/.svg-sprite/index.js'
// pages/index.tsx
import { AppIcon } from '../assets/app-icons'
export default function HomePage() {
export default function Page() {
return (
<main>
<IconsIcon icon="folder" width={24} height={24} aria-label="Files" />
<p>{iconsIconNames.length} icons available</p>
</main>
<AppIcon
icon="check"
width={24}
height={24}
role="img"
aria-label="Done"
style={{
color: '#334155',
'--icon-color-2': '#f59e0b',
}}
/>
)
}
```
The component works with SSR, SSG, and client-side navigation. Webpack resolves the generated SVG URL and CSS Module and emits a separate asset. Keep the mode and the `--webpack` dev/build flags aligned. If custom Next.js webpack rules process SVG through SVGR, exclude `.svg-sprite/sprite.svg` from those rules.
The component works with SSR, SSG, and client-side navigation. Next.js automatically adds `sprite.svg` to the production build, so you do not need to move it to `public`.
## 2. Debug and preview
## Debug and preview
This section is optional. Only users who need the Viewer or icon previews should install:
Viewer displays all icons on one page so you can check their rendering, change colors, and inspect the related CSS variables. It is only needed for development and is installed separately:
```bash
npm install --save-dev @gromlab/svg-sprites
```
Pages Router does not require an App Router Client Component boundary. Use the React bridge directly in a page with a static loader array:
Create the page `pages/svg-sprite.tsx`:
```tsx
// src/pages/icon-debug.tsx
import type { GetStaticProps } from 'next'
import { SpriteViewer } from '@gromlab/svg-sprites/react'
const sources = [
() => import('../ui/icons/.svg-sprite/svg-sprite.manifest.js'),
]
() => import('../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'),
] as const
export default function IconDebugPage() {
export default function SvgSpritePage() {
return <SpriteViewer sources={sources} title="Project icons" />
}
export const getStaticProps: GetStaticProps = () =>
process.env.NODE_ENV === 'development'
? { props: {} }
: { notFound: true }
```
Keep the page internal or development-only. Viewer is not part of the production icon runtime.
## 3. Type the config
Choose one of these two paths.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'next@pages/webpack',
name: 'icons',
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
### Without the package
Copy a mode-specific type directly into the config:
```ts
type LocalSpriteConfig = {
mode: 'next@pages/webpack'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'next@pages/webpack',
name: 'icons',
} satisfies LocalSpriteConfig
```
Run `npm run dev` and open `/svg-sprite`. In production, the route returns 404.

View File

@@ -1,148 +1,115 @@
# React Vite SVG Sprite Quick Start
# SVG Sprite for React with Vite
This guide targets the exact mode key `react@vite`: a generated typed React component with a Vite-managed SVG asset.
A quick guide to creating an SVG sprite in a React application built with Vite.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`.
Choose a folder for the sprite. This example uses `assets/app-icons`, with source SVG files, including the `check.svg` used below, in `assets/svg-icons`.
Keep the config and source icons together:
Create `assets/app-icons/svg-sprite.config.json`:
```text
src/ui/icons/
├── icons/
├── check.svg
└── folder.svg
└── svg-sprite.config.ts
```
Use a plain default object export with no package import:
```ts
// src/ui/icons/svg-sprite.config.ts
export default {
mode: 'react@vite',
name: 'icons',
```json
{
"mode": "react@vite",
"name": "app",
"input": "../svg-icons/**/*.svg"
}
```
When `input` is omitted, SVG files are read from `./icons` relative to the config. A `.js` config with a default export and a `.json` config are also supported. Generate directly with:
The `input` path is relative to the config folder.
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts
```
Use the exact Vite dev/build commands and generate once per invocation:
Add generation commands to `package.json`. Generated files are excluded from Git by default, so `predev` and `prebuild` rebuild the sprite before every start and build:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts",
"dev": "npm run sprites && vite",
"build": "npm run sprites && tsc --noEmit && vite build"
"sprites": "npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json",
"predev": "npm run sprites",
"dev": "vite",
"prebuild": "npm run sprites",
"build": "tsc --noEmit && vite build"
}
}
```
Do not add `predev` or `prebuild` hooks to these scripts; that would run generation twice. In CI, replace `latest` with an exact package version.
## Use the sprite
Generation creates a local `.gitignore`; commit that file once, but do not commit `.svg-sprite/`. Generated declarations are self-contained and do not require the package.
The value `name: "app"` creates the React component `AppIcon`.
### Production usage
Create the entry point `assets/app-icons/index.ts`:
Import the generated component and icon-name list directly:
```ts
export * from './.svg-sprite/index.js'
```
Use the component in your application:
```tsx
// src/App.tsx
import {
IconsIcon,
iconsIconNames,
} from './ui/icons/.svg-sprite/index.js'
import { AppIcon } from '../assets/app-icons'
export function App() {
export function SaveIcon() {
return (
<main>
<IconsIcon icon="check" width={24} height={24} aria-label="Complete" />
<small>{iconsIconNames.length} icons available</small>
</main>
<AppIcon
icon="check"
width={24}
height={24}
role="img"
aria-label="Done"
style={{
color: '#334155',
'--icon-color-2': '#f59e0b',
}}
/>
)
}
```
`icon` is a generated union of SVG file names. Vite automatically handles the generated CSS Module and the `sprite.svg?no-inline` import, emitting the sprite as a separate asset. If your own TypeScript source imports Vite query assets, include Vite's ambient types:
The `icon` prop accepts source SVG file names without the extension. A monochrome icon inherits `color`, while colors in a multicolor icon are overridden with `--icon-color-N`.
```json
{
"compilerOptions": {
"types": ["vite/client"]
}
}
```
Vite automatically includes the component styles and adds `sprite.svg` to the production build.
## 2. Debug and preview
## Debug and preview
This section is optional. Only users who need the Viewer or icon previews should install:
Viewer displays all icons on one page so you can check their rendering, change colors, and inspect the related CSS variables. It is only needed for development and is installed separately:
```bash
npm install --save-dev @gromlab/svg-sprites
```
Use the React `SpriteViewer` bridge with a static loader array. Keep every `import()` path a string literal:
Create `svg-sprite.html` in the project root:
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project icons</title>
</head>
<body>
<!-- React root for debugging and previewing the SVG sprite in Viewer -->
<div id="svg-sprite-viewer"></div>
<!-- Load the debug script created below -->
<script type="module" src="/src/svg-sprite-debug.tsx"></script>
</body>
</html>
```
Create `src/svg-sprite-debug.tsx`:
```tsx
// src/IconsDebugPage.tsx
import { createRoot } from 'react-dom/client'
import { SpriteViewer } from '@gromlab/svg-sprites/react'
const sources = [
() => import('./ui/icons/.svg-sprite/svg-sprite.manifest.js'),
]
() => import('../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'),
] as const
export function IconsDebugPage() {
return <SpriteViewer sources={sources} title="Project icons" />
}
createRoot(document.getElementById('svg-sprite-viewer')!).render(
<SpriteViewer sources={sources} title="Project icons" />,
)
```
Keep this component on a debug route or in an internal tool. Viewer is not part of the production icon runtime.
Run `npm run dev` and open `/svg-sprite.html`.
## 3. Type the config
Choose one of these two paths.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'react@vite',
name: 'icons',
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
### Without the package
Copy a mode-specific type directly into the config:
```ts
type LocalSpriteConfig = {
mode: 'react@vite'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'react@vite',
name: 'icons',
} satisfies LocalSpriteConfig
```
The standard Vite production build uses only `index.html` and does not include the Viewer page.

View File

@@ -1,156 +1,132 @@
# React Webpack SVG Sprite Quick Start
# SVG Sprite for React with Webpack 5
This guide targets the exact mode key `react@webpack`: a generated typed React component using Webpack 5 Asset Modules and CSS Modules.
A quick guide to creating an SVG sprite in a React application built with Webpack 5.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`.
Choose a folder for the sprite. This example uses `assets/app-icons`, with source SVG files, including the `check.svg` used below, in `assets/svg-icons`.
Keep the config adjacent to its source icons:
Create `assets/app-icons/svg-sprite.config.json`:
```text
src/ui/icons/
├── icons/
├── check.svg
└── folder.svg
└── svg-sprite.config.ts
```
Use a plain default object export with no package import:
```ts
// src/ui/icons/svg-sprite.config.ts
export default {
mode: 'react@webpack',
name: 'icons',
```json
{
"mode": "react@webpack",
"name": "app",
"input": "../svg-icons/**/*.svg"
}
```
When `input` is omitted, SVG files are read from `./icons` relative to the config. A `.js` config with a default export and a `.json` config are also supported. Generate directly with:
The `input` path is relative to the config folder.
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts
```
Use the exact Webpack 5 flags and generate once per invocation:
Add generation commands to `package.json`. Generated files are excluded from Git by default, so `predev` and `prebuild` rebuild the sprite before every start and build:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts",
"dev": "npm run sprites && webpack serve --mode development",
"build": "npm run sprites && webpack --mode production"
"sprites": "npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json",
"predev": "npm run sprites",
"dev": "webpack serve --mode development",
"prebuild": "npm run sprites",
"build": "webpack --mode production"
}
}
```
Do not add `predev` or `prebuild` hooks to these scripts; that would run generation twice. In CI, replace `latest` with an exact package version.
## Use the sprite
Generation creates a local `.gitignore`; commit that file once, but do not commit `.svg-sprite/`. Generated declarations are self-contained and do not require the package.
The value `name: "app"` creates the React component `AppIcon`.
### Production usage
Create the entry point `assets/app-icons/index.ts`:
Import the generated component and icon-name list directly:
```ts
export * from './.svg-sprite/index.js'
```
Use the component in your application:
```tsx
// src/App.tsx
import {
IconsIcon,
iconsIconNames,
} from './ui/icons/.svg-sprite/index.js'
import { AppIcon } from '../assets/app-icons'
export function App() {
export function SaveIcon() {
return (
<main>
<IconsIcon icon="folder" width={24} height={24} aria-label="Files" />
<small>{iconsIconNames.length} icons available</small>
</main>
<AppIcon
icon="check"
width={24}
height={24}
role="img"
aria-label="Done"
style={{
color: '#334155',
'--icon-color-2': '#f59e0b',
}}
/>
)
}
```
The generated component uses `new URL('../sprite.svg', import.meta.url)`, which Webpack 5 processes through Asset Modules and emits as a separate SVG asset. Exclude `.svg-sprite/sprite.svg` from SVG component or SVGR rules so they do not intercept that URL dependency.
The `icon` prop accepts source SVG file names without the extension. A monochrome icon inherits `color`, while colors in a multicolor icon are overridden with `--icon-color-N`.
The generated component also imports `react-component.module.css`. Configure `.module.css` through `css-loader` with modules enabled, plus `style-loader` or `MiniCssExtractPlugin`:
The component uses CSS Modules. If the project does not process them yet, install the loaders:
```bash
npm install --save-dev style-loader css-loader
```
Then add a rule with a default export to `webpack.config.js`:
```js
// webpack.config.js (relevant rule)
export default {
module: {
rules: [
{
test: /\.module\.css$/i,
use: ['style-loader', { loader: 'css-loader', options: { modules: true } }],
},
],
},
{
test: /\.module\.css$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: { modules: { namedExport: false } },
},
],
}
```
## 2. Debug and preview
Webpack 5 automatically adds `sprite.svg` to the production build.
This section is optional. Only users who need the Viewer or icon previews should install:
## Debug and preview
Viewer displays all icons on one page so you can check their rendering, change colors, and inspect the related CSS variables. It is only needed for development.
Install Viewer:
```bash
npm install --save-dev @gromlab/svg-sprites
```
Use the React `SpriteViewer` bridge with a static loader array. Keep every `import()` path a string literal so Webpack can create the chunk:
Create the entry `src/svg-sprite-debug.tsx`:
```tsx
// src/IconsDebugPage.tsx
import { createRoot } from 'react-dom/client'
import { SpriteViewer } from '@gromlab/svg-sprites/react'
const sources = [
() => import('./ui/icons/.svg-sprite/svg-sprite.manifest.js'),
]
() => import('../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'),
] as const
export function IconsDebugPage() {
return <SpriteViewer sources={sources} title="Project icons" />
}
const container = document.createElement('div')
document.body.append(container)
createRoot(container).render(
<SpriteViewer sources={sources} title="Project icons" />,
)
```
Keep this component on a debug route or in an internal tool. Viewer is not part of the production icon runtime.
Add the script to the main entry only in development mode. Keep the rest of your `webpack.config.js` settings:
## 3. Type the config
Choose one of these two paths.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'react@webpack',
name: 'icons',
```js
export default (_env, argv) => ({
// Other Webpack settings.
entry: [
'./src/main.tsx',
...(argv.mode === 'development' ? ['./src/svg-sprite-debug.tsx'] : []),
],
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
### Without the package
Copy a mode-specific type directly into the config:
```ts
type LocalSpriteConfig = {
mode: 'react@webpack'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'react@webpack',
name: 'icons',
} satisfies LocalSpriteConfig
```
Run `npm run dev`. Viewer appears on the application's main page and is not included in the production build.

View File

@@ -1,156 +1,114 @@
# Standalone Vite SVG Sprite Quick Start
# SVG Sprite for Vite Without a Framework
This guide targets the exact mode key `standalone@vite`: a native generated Web Component with Vite-managed SVG assets.
A quick guide to creating an SVG sprite in a Vite application without a framework.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`.
Choose a folder for the sprite. This example uses `assets/app-icons`, with source SVG files, including the `check.svg` used below, in `assets/svg-icons`.
Keep the config and source icons together:
Create `assets/app-icons/svg-sprite.config.json`:
```text
src/ui/icons/
├── icons/
├── check.svg
└── folder.svg
└── svg-sprite.config.ts
```
Use a plain default object export with no package import:
```ts
// src/ui/icons/svg-sprite.config.ts
export default {
mode: 'standalone@vite',
name: 'icons',
```json
{
"mode": "standalone@vite",
"name": "app",
"input": "../svg-icons/**/*.svg"
}
```
When `input` is omitted, SVG files are read from `./icons` relative to the config. A `.js` config with a default export and a `.json` config are also supported. Generate once directly with:
The `input` path is relative to the config folder.
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts
```
Use the exact Vite dev/build commands and generate once per invocation:
Add generation commands to `package.json`. Generated files are excluded from Git by default, so `predev` and `prebuild` rebuild the sprite before every start and build:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts",
"dev": "npm run sprites && vite",
"build": "npm run sprites && vite build"
"sprites": "npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json",
"predev": "npm run sprites",
"dev": "vite",
"prebuild": "npm run sprites",
"build": "vite build"
}
}
```
Do not add `predev` or `prebuild` hooks to these scripts; that would run generation twice. In CI, replace `latest` with an exact package version.
## Use the sprite
Generation creates a local `.gitignore`; commit that file once, but do not commit `.svg-sprite/`. The generated JavaScript and declarations live together, and the declarations are self-contained: they do not require `@gromlab/svg-sprites`.
The value `name: "app"` creates the `<app-icon>` element.
### Production usage
Register the generated element once, then use `<icons-icon>`:
Create the entry point `assets/app-icons/index.ts`:
```ts
// src/main.ts
import {
defineIconsIconElement,
iconsIconNames,
} from './ui/icons/.svg-sprite/index.js'
defineIconsIconElement()
console.log('Available icons:', iconsIconNames)
export * from './.svg-sprite/index.js'
```
Register the element in `src/main.ts`:
```ts
import { defineAppIconElement } from '../assets/app-icons'
import './style.css'
defineAppIconElement()
```
Use the icon in HTML:
```html
<icons-icon icon="check" role="img" aria-label="Complete"></icons-icon>
<app-icon icon="check" role="img" aria-label="Done"></app-icon>
```
The host is `1em` by `1em`, so `font-size` controls its default size. Transformed colors use `currentColor` and custom properties such as `--icon-color-1`:
The file `check.svg` is available as `icon="check"`. Set its size and colors with CSS:
```css
icons-icon {
app-icon {
font-size: 24px;
color: #2563eb;
--icon-color-2: #dbeafe;
color: #334155;
--icon-color-2: #f59e0b;
}
```
Vite handles the generated `sprite.svg?no-inline` import automatically and emits a separate asset. If your own TypeScript source imports Vite query assets, include Vite's ambient types:
A monochrome icon inherits `color`, while colors in a multicolor icon are overridden with `--icon-color-N`. Viewer shows the variables you need.
```json
{
"compilerOptions": {
"types": ["vite/client"]
}
}
```
Vite automatically adds `sprite.svg` to the production build. You do not need to copy it to `public`.
## 2. Debug and preview
## Debug and preview
This section is optional. Only users who need the Viewer or icon previews should install:
Viewer displays all icons on one page so you can check their rendering, change colors, and inspect the related CSS variables. It is only needed for development and is installed separately:
```bash
npm install --save-dev @gromlab/svg-sprites
```
Register the Viewer element, import its type, and assign the generated JavaScript manifest to `sources`:
Create `svg-sprite.html` in the project root:
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project icons</title>
</head>
<body>
<!-- Viewer component for debugging and previewing the SVG sprite -->
<gromlab-sprite-viewer viewer-title="Project icons"></gromlab-sprite-viewer>
<!-- Load the debug script created below -->
<script type="module" src="/src/svg-sprite-debug.ts"></script>
</body>
</html>
```
Create `src/svg-sprite-debug.ts`:
```ts
import '@gromlab/svg-sprites/viewer/element'
import type { SpriteViewerElement } from '@gromlab/svg-sprites/viewer'
import spriteManifest from './ui/icons/.svg-sprite/svg-sprite.manifest.js'
document.querySelector<HTMLDivElement>('#debug')!.innerHTML = `
<gromlab-sprite-viewer viewer-title="Project icons"></gromlab-sprite-viewer>
`
import spriteManifest from '../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'
const viewer = document.querySelector<SpriteViewerElement>('gromlab-sprite-viewer')!
viewer.sources = [spriteManifest]
```
Keep this code on a debug route or in an internal tool. Viewer is not part of the production icon runtime.
Run `npm run dev` and open `/svg-sprite.html`.
## 3. Type the config
Choose one of these two paths.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'standalone@vite',
name: 'icons',
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
### Without the package
Copy a mode-specific type directly into the config:
```ts
type LocalSpriteConfig = {
mode: 'standalone@vite'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'standalone@vite',
name: 'icons',
} satisfies LocalSpriteConfig
```
Viewer is not required for `<app-icon>` and is not imported by the application's main code.

View File

@@ -1,143 +1,111 @@
# Standalone Webpack SVG Sprite Quick Start
# SVG Sprite for Webpack 5 Without a Framework
This guide targets the exact mode key `standalone@webpack`: a native generated Web Component using Webpack 5 Asset Modules.
A quick guide to creating an SVG sprite in a Webpack 5 application without a framework.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`.
Choose a folder for the sprite. This example uses `assets/app-icons`, with source SVG files, including the `check.svg` used below, in `assets/svg-icons`.
Keep the config adjacent to its source icons:
Create `assets/app-icons/svg-sprite.config.json`:
```text
src/ui/icons/
├── icons/
├── check.svg
└── folder.svg
└── svg-sprite.config.ts
```
Use a plain default object export with no package import:
```ts
// src/ui/icons/svg-sprite.config.ts
export default {
mode: 'standalone@webpack',
name: 'icons',
```json
{
"mode": "standalone@webpack",
"name": "app",
"input": "../svg-icons/**/*.svg"
}
```
When `input` is omitted, SVG files are read from `./icons` relative to the config. A `.js` config with a default export and a `.json` config are also supported. Generate directly with:
The `input` path is relative to the config folder.
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts
```
Use the exact Webpack 5 flags and generate once per invocation:
Add generation commands to `package.json`. Generated files are excluded from Git by default, so `predev` and `prebuild` rebuild the sprite before every start and build:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.ts",
"dev": "npm run sprites && webpack serve --mode development",
"build": "npm run sprites && webpack --mode production"
"sprites": "npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json",
"predev": "npm run sprites",
"dev": "webpack serve --mode development",
"prebuild": "npm run sprites",
"build": "webpack --mode production"
}
}
```
Do not add `predev` or `prebuild` hooks to these scripts; that would run generation twice. In CI, replace `latest` with an exact package version.
## Use the sprite
Generation creates a local `.gitignore`; commit that file once, but do not commit `.svg-sprite/`. Generated declarations are self-contained and do not require the package.
The value `name: "app"` creates the `<app-icon>` element.
### Production usage
Register the generated element once, then use `<icons-icon>`:
Create the entry point `assets/app-icons/index.ts`:
```ts
// src/main.ts
import {
defineIconsIconElement,
iconsIconNames,
} from './ui/icons/.svg-sprite/index.js'
defineIconsIconElement()
console.log('Available icons:', iconsIconNames)
export * from './.svg-sprite/index.js'
```
Register the element in the application's main entry:
```ts
import { defineAppIconElement } from '../assets/app-icons'
import './style.css'
defineAppIconElement()
```
Use the icon in HTML:
```html
<icons-icon
icon="check"
role="img"
aria-label="Complete"
style="font-size:24px;color:#16a34a;--icon-color-1:#16a34a"
></icons-icon>
<app-icon icon="check" role="img" aria-label="Done"></app-icon>
```
The generated facade uses `new URL('./sprite.svg', import.meta.url)`, which Webpack 5 processes through Asset Modules and emits as a separate asset. If the project has SVG-to-component or SVGR rules, exclude `.svg-sprite/sprite.svg` from them so they do not intercept this URL dependency. Check `output.publicPath` if the emitted URL is wrong.
The file `check.svg` is available as `icon="check"`. Set its size and colors with CSS:
## 2. Debug and preview
```css
app-icon {
font-size: 24px;
color: #334155;
--icon-color-2: #f59e0b;
}
```
This section is optional. Only users who need the Viewer or icon previews should install:
A monochrome icon inherits `color`, while colors in a multicolor icon are overridden with `--icon-color-N`. Viewer shows the variables you need.
Webpack 5 automatically adds `sprite.svg` to the production build.
## Debug and preview
Viewer displays all icons on one page so you can check their rendering, change colors, and inspect the related CSS variables. It is only needed for development.
Install Viewer:
```bash
npm install --save-dev @gromlab/svg-sprites
```
Register the Viewer element, import its type, and assign the generated JavaScript manifest to `sources`:
Create the entry `src/svg-sprite-debug.ts`:
```ts
import '@gromlab/svg-sprites/viewer/element'
import type { SpriteViewerElement } from '@gromlab/svg-sprites/viewer'
import spriteManifest from './ui/icons/.svg-sprite/svg-sprite.manifest.js'
import spriteManifest from '../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'
document.querySelector<HTMLDivElement>('#debug')!.innerHTML = `
<gromlab-sprite-viewer viewer-title="Project icons"></gromlab-sprite-viewer>
`
const viewer = document.querySelector<SpriteViewerElement>('gromlab-sprite-viewer')!
const viewer = document.createElement('gromlab-sprite-viewer') as SpriteViewerElement
viewer.viewerTitle = 'Project icons'
viewer.sources = [spriteManifest]
document.body.append(viewer)
```
Keep this code on a debug route or in an internal tool. Viewer is not part of the production icon runtime.
Add the script to the main entry only in development mode. Keep the rest of your `webpack.config.js` settings:
## 3. Type the config
Choose one of these two paths.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'standalone@webpack',
name: 'icons',
```js
export default (_env, argv) => ({
// Other Webpack settings.
entry: [
'./src/main.ts',
...(argv.mode === 'development' ? ['./src/svg-sprite-debug.ts'] : []),
],
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
Run `npm run dev`. Viewer appears on the application's main page.
### Without the package
Copy a mode-specific type directly into the config:
```ts
type LocalSpriteConfig = {
mode: 'standalone@webpack'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'standalone@webpack',
name: 'icons',
} satisfies LocalSpriteConfig
```
Viewer is only added to the development build and is not included in production.

View File

@@ -1,22 +1,26 @@
# Standalone SVG Sprite Quick Start
# SVG Sprite for a Site Without a Bundler
This guide targets the exact mode key `standalone`: static HTML or a custom asset pipeline with no generated JavaScript facade.
Combine SVG icons into one file and use them on an HTML page.
## 1. Generate the sprite
## Generate the sprite
No package installation and no `package.json` dependency are needed. `npx` downloads the CLI temporarily, and generated runtime does not import `@gromlab/svg-sprites`; bare `standalone` does not generate a JavaScript runtime at all.
You do not need to install the package in your project.
Keep the config next to its source icons:
This guide uses the following project structure:
```text
src/ui/icons/
├── icons/
│ ├── check.svg
── folder.svg
└── svg-sprite.config.json
/
├── index.html
└── assets/
── app-icons/
└── svg-icons/
├── check.svg
└── warning.svg
```
Create a JSON config:
### 1. Create the sprite config
Choose a folder for the sprite. This example uses `assets/app-icons`. Create `svg-sprite.config.json` inside it:
```json
{
@@ -25,147 +29,80 @@ Create a JSON config:
}
```
When `input` is omitted, the generator reads `./icons` next to the config.
### 2. Set the icon source
To include SVG files from nested directories, set one glob pattern:
`input` can point to a folder, an individual SVG file, or a glob pattern. To use multiple sources, provide an array with any combination of these values:
```json
{
"mode": "standalone",
"name": "icons",
"input": "../assets/icons/**/*.svg"
"input": "../svg-icons/**/*.svg"
}
```
An array can mix folders, individual SVG files, and glob patterns:
### 3. Generate the sprite
```json
{
"mode": "standalone",
"name": "icons",
"input": [
"../assets/icons",
"../features/profile/user.svg",
"../features/admin/*.svg"
]
}
```
All relative paths start at the config directory. Pass the config path explicitly:
Pass the config path to the command:
```bash
npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.json
npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json
```
For repeatable local commands, the same temporary CLI can be used from a script:
The package collects the icons in a `.svg-sprite` directory next to the config:
```json
{
"scripts": {
"sprites": "npx --yes --package=@gromlab/svg-sprites@latest svg-sprites src/ui/icons/svg-sprite.config.json"
}
}
```text
assets/app-icons/.svg-sprite/
├── sprite.svg
└── svg-sprite.manifest.json
```
Run that script once from your existing dev/build pipeline. If you use a `predev` or `prebuild` hook instead, do not also call `npm run sprites` inside the corresponding script. Bare `standalone` has no bundler-specific dev or build flag. In CI, replace `latest` with an exact package version.
- `sprite.svg` is the finished sprite for use on the site.
- `svg-sprite.manifest.json` contains icon data for Viewer.
Bare `standalone` does not create or modify `.gitignore`; the application decides whether `.svg-sprite/` is committed or ignored. It emits no declarations, facade, or component. Generated declarations in typed modes are self-contained and do not require the package.
The `.svg-sprite` directory is created automatically and fully replaced on every generation. Do not edit its contents manually.
### Production usage
### 4. Use an icon
The application owns the public URL, versioning, and cache policy. Copy both generated assets into the deploy output:
```bash
cp src/ui/icons/.svg-sprite/sprite.svg public/assets/icons.svg
cp src/ui/icons/.svg-sprite/svg-sprite.manifest.json public/assets/icons.manifest.json
```
Use the published URL manually:
In `index.html`, point to the generated `sprite.svg`. After `#`, add the icon file name without the `.svg` extension:
```html
<svg width="24" height="24" role="img" aria-label="Complete">
<use href="/assets/icons.svg#check"></use>
<svg
width="24"
height="24"
aria-label="Done"
>
<use href="./assets/app-icons/.svg-sprite/sprite.svg#check"></use>
</svg>
```
Safe icon names normally match their fragment IDs. Names containing spaces or other SVG-ID-unsafe characters receive a generated ID; read that icon's `id` from `icons.manifest.json` instead of constructing the fragment yourself.
The icon from `check.svg` is available as `#check`.
## 2. Debug and preview
## Debug and preview
This section is optional. Only install the package locally if you need the debug Viewer or an icon preview:
`sprite.svg` is a technical file, not an icon gallery. Opening it does not provide a convenient view of the whole set. Gradients, masks, filters, and references to internal `id` values may also render with artifacts.
```bash
npm install --save-dev @gromlab/svg-sprites
```
Use the official Viewer for visual checks. It displays every icon in the sprite and helps you verify its colors and rendering.
Without a bundler, self-host the browser bundle by copying it from `node_modules` into the deploy output:
Viewer is optional and intended only for development. You do not need to install the package through npm.
```bash
cp node_modules/@gromlab/svg-sprites/dist/viewer-element.js public/debug/viewer-element.js
```
Viewer works directly with files from `.svg-sprite`. Nothing needs to be copied.
Then provide both public asset URLs through HTML attributes:
### Add Viewer to the page
```html
<script type="module" src="/debug/viewer-element.js"></script>
<gromlab-sprite-viewer
viewer-title="Project icons"
manifest-url="/assets/icons.manifest.json"
sprite-url="/assets/icons.svg"
></gromlab-sprite-viewer>
```
For a quick preview, a pinned CDN file can replace the self-hosted script:
Add a module script to `index.html` and provide paths to the generated manifest and sprite:
```html
<script
type="module"
src="https://unpkg.com/@gromlab/svg-sprites@1.1.5/dist/viewer-element.js"
src="https://cdn.jsdelivr.net/npm/@gromlab/svg-sprites/dist/viewer-element.js"
></script>
<gromlab-sprite-viewer
viewer-title="Project icons"
manifest-url="./assets/app-icons/.svg-sprite/svg-sprite.manifest.json"
sprite-url="./assets/app-icons/.svg-sprite/sprite.svg"
></gromlab-sprite-viewer>
```
Self-host the file for controlled environments and pin the CDN version if you use the alternative. Viewer is optional tooling, not part of the production icon runtime.
## 3. Type the config
This guide uses a JSON config, which works without TypeScript types. If config autocomplete is needed, replace `svg-sprite.config.json` with `svg-sprite.config.ts` and choose one of these approaches.
### With a local package installation
After installing the package locally, use the helper:
```ts
import { defineSpriteConfig } from '@gromlab/svg-sprites'
export default defineSpriteConfig({
mode: 'standalone',
name: 'icons',
})
```
You can alternatively import `type SpriteConfig` and apply `satisfies SpriteConfig`.
### Without the package
Keep a narrow local type directly in the config:
```ts
type LocalSpriteConfig = {
mode: 'standalone'
name?: string
description?: string
input?: string | string[]
transform?: {
removeSize?: boolean
replaceColors?: boolean
addTransition?: boolean
}
generatedNotice?: boolean
}
export default {
mode: 'standalone',
name: 'icons',
} satisfies LocalSpriteConfig
```
You can move Viewer to a separate HTML file in the site root used only for development and icon checks.