@svgt/cli
A CLI to transform SVG files into frontend components.
Installation
It requires Node.js v22.13 or later.
Run it without installing:
npx @svgt/cli arrow-left.svg --framework reactOr add it globally to your system:
npm i -g @svgt/cliUsage
svgt <files or folders...> --framework <framework> [options]
cat icon.svg | svgt --framework <framework> [options]- Convert a single SVG file. This writes
ArrowLeft.tsxto the current folder:
svgt arrow-left.svg --framework react- Write it to another folder:
svgt arrow-left.svg --framework react --out src/components- Convert every SVG file in a folder, including its subfolders. Hidden folders,
node_modules,distandbuildare skipped:
svgt icons/ --framework svelte --out src/lib/icons- Pick the files from a list. When you pass no files or folders and stdin is a terminal, the CLI lists every SVG under the current folder:
svgt --framework react| Key | Action |
|---|---|
| Space | Select or clear a file |
| a | Select all |
| i | Invert the selection |
| Enter | Convert the selection |
| Esc | Cancel |
- Read the SVG from stdin. The component is named
SvgIconunless you pass--name:
cat icon.svg | svgt --framework vue --name ArrowLeftOptions
| Option | Description |
|---|---|
-f, --framework <name> | Required. react, svelte, vue, angular, astro or web-components. |
-o, --out <folder> | Folder to write the components to. Defaults to the current folder. |
-n, --name <name> | Component name, for a single SVG only. Defaults to the file name in PascalCase, or SvgIcon from stdin. |
--js | Output JavaScript instead of TypeScript. Fails with Vue and Angular, which have no TypeScript option. |
--no-dimensions | Remove width and height from the root <svg>. |
--svg-prop <name=value> | Set an attribute on the root <svg>. Repeat it for more attributes. A bare name removes the attribute. |
--expand-props <pos> | Where to spread the props on the root <svg>: start, end or none. Defaults to end. |
--export-type <type> | default or directly (export const / export class). Defaults to default. Fails with any framework other than React and Web Components. |
--force | Overwrite existing files without asking. |
-h, --help | Show the help. |
-v, --version | Show the version. |
Each option maps to a transform() option, documented in the @svgt/core README:
svgt icon.svg -f react --svg-prop aria-hidden=true --svg-prop class --expand-props startOutput files
Files are named after each framework's convention:
| Framework | File |
|---|---|
| React | ArrowLeft.tsx / .jsx |
| Svelte | ArrowLeft.svelte |
| Vue | ArrowLeft.vue |
| Angular | arrow-left.component.ts |
| Astro | ArrowLeft.astro |
| Web Components | arrow-left.ts / .js |
- When a file already exists, the CLI asks before overwriting it:
yfor yes,nfor no (the default when you press Enter),ato overwrite it and every file after it. Without a terminal to ask in, or when the SVG comes from stdin, existing files are skipped unless you pass--force. - If an SVG fails to parse, that SVG is skipped. If two SVGs would write the same file, such as
arrow-left.svgandArrowLeft.svg, the first is written and the second is skipped. In both cases the other SVGs are still written and the command exits with code 1.
License
MIT