Files
svg-sprites/docs/en/guides/react-webpack.md

133 lines
3.3 KiB
Markdown
Raw Normal View History

# SVG Sprite for React with Webpack 5
2026-07-14 16:11:39 +03:00
A quick guide to creating an SVG sprite in a React application built with Webpack 5.
2026-07-14 16:11:39 +03:00
## Generate the sprite
2026-07-14 16:11:39 +03:00
Choose a directory for the future SVG sprite, for example `assets/app-icons`, and create `svg-sprite.config.json` inside it. In `input`, specify the path to existing SVG files relative to the configuration file. There is no need to move or copy the icons.
2026-07-14 16:11:39 +03:00
Example configuration:
2026-07-14 16:11:39 +03:00
```json
{
"mode": "react@webpack",
"name": "app",
"input": "../svg-icons/**/*.svg"
2026-07-14 16:11:39 +03:00
}
```
The package does not need to be a project dependency: generation runs through `npx`.
2026-07-14 16:11:39 +03:00
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:
2026-07-14 16:11:39 +03:00
```json
{
"scripts": {
"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"
2026-07-14 16:11:39 +03:00
}
}
```
## Use the sprite
2026-07-14 16:11:39 +03:00
The value `name: "app"` creates the React component `AppIcon`.
2026-07-14 16:11:39 +03:00
Create the entry point `assets/app-icons/index.ts`:
2026-07-14 16:11:39 +03:00
```ts
export * from './.svg-sprite/index.js'
```
Use the component in your application:
2026-07-14 16:11:39 +03:00
```tsx
import { AppIcon } from '../assets/app-icons'
2026-07-14 16:11:39 +03:00
export function SaveIcon() {
2026-07-14 16:11:39 +03:00
return (
<AppIcon
icon="icon-name"
width={24}
height={24}
role="img"
aria-label="Done"
style={{
color: '#334155',
'--icon-color-2': '#f59e0b',
}}
/>
2026-07-14 16:11:39 +03:00
)
}
```
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`.
2026-07-14 16:11:39 +03:00
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`:
2026-07-14 16:11:39 +03:00
```js
{
test: /\.module\.css$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: { modules: { namedExport: false } },
},
],
2026-07-14 16:11:39 +03:00
}
```
Webpack 5 automatically adds `sprite.svg` to the production build.
## Debug and preview
2026-07-14 16:11:39 +03:00
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:
2026-07-14 16:11:39 +03:00
```bash
npm install --save-dev @gromlab/svg-sprites
```
Create the entry `src/svg-sprite-debug.tsx`:
2026-07-14 16:11:39 +03:00
```tsx
import { createRoot } from 'react-dom/client'
2026-07-14 16:11:39 +03:00
import { SpriteViewer } from '@gromlab/svg-sprites/react'
const sources = [
() => import('../assets/app-icons/.svg-sprite/svg-sprite.manifest.js'),
] as const
2026-07-14 16:11:39 +03:00
const container = document.createElement('div')
document.body.append(container)
2026-07-14 16:11:39 +03:00
createRoot(container).render(
<SpriteViewer sources={sources} title="Project icons" />,
)
```
2026-07-14 16:11:39 +03:00
Add the script to the main entry only in development mode. Keep the rest of your `webpack.config.js` settings:
2026-07-14 16:11:39 +03:00
```js
export default (_env, argv) => ({
// Other Webpack settings.
entry: [
'./src/main.tsx',
...(argv.mode === 'development' ? ['./src/svg-sprite-debug.tsx'] : []),
],
2026-07-14 16:11:39 +03:00
})
```
Run `npm run dev`. Viewer appears on the application's main page and is not included in the production build.