2026-07-11 09:26:43 +03:00
# Programmatic API
[← Back to home ](../../README.md )
2026-07-13 20:07:42 +03:00
The package is ESM-only and provides one Node.js generation API. The React runtime with `SpriteViewer` is available from the separate `@gromlab/svg-sprites/react` entry point.
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
## `generateSprite`
2026-07-11 09:26:43 +03:00
```ts
2026-07-13 20:07:42 +03:00
import { generateSprite } from '@gromlab/svg -sprites'
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
const result = await generateSprite(
'src/ui/file-manager/svg-sprite/svg-sprite.config.ts',
2026-07-11 09:26:43 +03:00
)
```
2026-07-14 08:34:45 +03:00
For static standalone mode, use `result.spritePath` in a build script to publish the
SVG under an application URL:
```ts
import { copyFile } from 'node:fs/promises'
const result = await generateSprite('src/sprite/svg-sprite.config.ts', {
mode: 'standalone',
})
await copyFile(result.spritePath, 'dist/app-icons/sprite.svg')
```
`spritePath` is a filesystem path, not a browser URL. A deployment-neutral JSON
manifest is available through `result.manifestPath` and is copied independently.
2026-07-13 20:07:42 +03:00
The first argument accepts the full path to an explicitly selected `.ts` , `.js` , or `.json` config file with any name. Passing a directory enables config-less mode and uses that directory as the sprite module root.
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
The second argument contains optional overrides and always takes precedence over the config:
2026-07-11 09:26:43 +03:00
```ts
2026-07-13 20:07:42 +03:00
await generateSprite('src/ui/file-manager/svg-sprite/custom-config.json', {
mode: 'react@webpack ',
name: 'documents',
inputFolder: './assets',
inputFiles: ['../../shared/search.svg'],
transform: {
addTransition: false,
},
generatedNotice: false,
})
2026-07-11 09:26:43 +03:00
```
2026-07-13 20:07:42 +03:00
Configuration is resolved in this order:
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
```text
defaults → config → API overrides
2026-07-11 09:26:43 +03:00
```
2026-07-13 20:07:42 +03:00
For fully programmatic generation, pass a directory and provide the required settings as overrides:
2026-07-11 09:26:43 +03:00
```ts
2026-07-13 20:07:42 +03:00
await generateSprite('src/ui/file-manager/svg-sprite', {
mode: 'react@vite ',
name: 'file-manager',
inputFiles: [
'../../shared/search.svg',
'../../shared/settings.svg',
],
})
2026-07-11 09:26:43 +03:00
```
2026-07-13 20:07:42 +03:00
## Configuration
2026-07-11 09:26:43 +03:00
```ts
2026-07-13 20:07:42 +03:00
import { defineSpriteConfig } from '@gromlab/svg -sprites'
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
export default defineSpriteConfig({
mode: 'react@vite ',
2026-07-11 09:26:43 +03:00
name: 'file-manager',
description: 'File manager icons',
inputFolder: './icons',
2026-07-13 20:07:42 +03:00
inputFiles: ['../../shared/check.svg'],
2026-07-11 09:26:43 +03:00
transform: {
removeSize: true,
replaceColors: true,
addTransition: true,
},
generatedNotice: true,
})
```
2026-07-13 20:07:42 +03:00
`defineSpriteConfig` is an identity helper for TypeScript autocomplete. JavaScript can export the same object with `export default` , while JSON contains the object directly.
## Specialized wrappers
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
The specialized functions are available as wrappers around `generateSprite` :
2026-07-11 09:26:43 +03:00
```ts
2026-07-13 20:07:42 +03:00
import { generateNextSprite, generateReactSprite } from '@gromlab/svg -sprites'
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
await generateReactSprite('path/to/config.ts', 'vite')
await generateNextSprite('path/to/config.ts', {
router: 'app',
bundler: 'turbopack',
2026-07-11 09:26:43 +03:00
})
```
2026-07-13 20:07:42 +03:00
An explicitly supplied target overrides `mode` from the file. Prefer `generateSprite` in new code.
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
## Config API
2026-07-11 09:26:43 +03:00
```ts
2026-07-13 20:07:42 +03:00
import {
loadSpriteConfig,
resolveSpriteConfig,
validateSpriteConfig,
} from '@gromlab/svg -sprites'
2026-07-11 09:26:43 +03:00
```
2026-07-13 20:07:42 +03:00
- `loadSpriteConfig(file)` loads an explicitly selected `.ts` , `.js` , or `.json` file.
- `validateSpriteConfig(value)` performs runtime validation.
- `resolveSpriteConfig(root, config, overrides)` merges values, applies defaults, and resolves paths relative to `root` .
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
## Low-level compiler
2026-07-11 09:26:43 +03:00
```ts
import {
compileSprite,
compileSpriteContent,
createShapeTransform,
} from '@gromlab/svg -sprites'
```
2026-07-13 20:07:42 +03:00
These functions are intended for custom orchestration. Standard generation should use `generateSprite` .
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
## React runtime
2026-07-11 09:26:43 +03:00
```tsx
import { SpriteViewer } from '@gromlab/svg -sprites/react'
```
2026-07-13 20:07:42 +03:00
`SpriteViewer` accepts generated manifests, lazy loaders, or an `import.meta.glob` result. This entry point contains `'use client'` and is intended for debug tools; production components are imported from local sprite modules.