Repository navigation
Add quickstart guide #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AnnaSemenec
wants to merge
7
commits into
svgforge:main
Choose a base branch
from
AnnaSemenec:docs/quickstart
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f78763d
Add quickstart guide
AnnaSemenec b470b78
Add CLI quickstart and unify quickstart step formatting
AnnaSemenec 5669f9b
Update CLI quickstart and JS quickstart
AnnaSemenec 43f1b88
fix text
AnnaSemenec 102dc68
add draft: true
AnnaSemenec be44898
Improve Node.js Quickstart instructions
AnnaSemenec 4a23e1c
docs: improve CLI quickstart project setup and usage
AnnaSemenec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| --- | ||
|
|
||
| title: "Quickstart: Get started with the svgforge CLI in 5 minutes" | ||
| description: A quick introduction to the svgforge CLI that helps you install the tool, create your first sprite, and see how to use icons from the sprite in your own product. | ||
| pubDate: 2026-11-07 | ||
| draft: true | ||
| tags: | ||
|
|
||
| - tutorial | ||
| - svg | ||
|
|
||
| --- | ||
|
|
||
| In this guide, we'll build a **symbol sprite** using the command line and test its icons on a page via `<use>`. | ||
|
|
||
| svgforge also supports other sprite modes: `view`, `stack`, and `defs`. For details, see [Specific mode properties](https://github.com/svgforge/svgforge/blob/main/docs/configuration.md#specific-mode-properties). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Node.js 24 or later | ||
|
|
||
| ## Setup | ||
|
|
||
| **1.** Create a project folder to work in. Let's call it `svgforge-test`: | ||
|
|
||
| ```bash | ||
| mkdir svgforge-test | ||
| cd svgforge-test | ||
| ``` | ||
|
|
||
| **2.** Install the svgforge CLI | ||
|
|
||
| ```bash | ||
| npm install --global @svgforge/svgforge-cli | ||
| ``` | ||
|
|
||
| You should see output similar to this: | ||
|
|
||
| ```text | ||
| added 54 packages in 48s | ||
| ... | ||
| ``` | ||
|
|
||
| **3.** Verify the installation: | ||
|
|
||
| ```bash | ||
| svgforge --help | ||
| ``` | ||
|
|
||
| If you see the list of commands, the CLI is installed correctly. | ||
|
|
||
| ## Building the sprite | ||
|
|
||
| **1.** Add a few SVG icons to the `svgforge-test` folder. If you want to use icons from another folder, specify its path in the build command below. | ||
|
|
||
| **2.** Run the following command in the terminal: | ||
|
|
||
| ```bash | ||
| svgforge --symbol --symbol-render-css --symbol-example --dest=out *.svg | ||
| ``` | ||
|
|
||
| This command: | ||
|
|
||
| - `--symbol` — selects the `symbol` mode | ||
| - `--symbol-render-css` — creates a CSS file | ||
| - `--symbol-example` — creates an HTML page with an example | ||
| - `--dest=out` — sets the main output directory | ||
| - `*.svg` — takes all SVG files from the current folder | ||
|
|
||
| To adapt the command to your project, you only need to change two parameters: | ||
|
|
||
| - `--dest=out` | ||
|
|
||
| In this example, svgforge writes the generated files to the out folder. | ||
|
|
||
| If you're using svgforge in an existing project, set `--dest` to the directory where you want the generated files to be written. | ||
|
|
||
| For example, this could be `dist` or `public`, depending on your project's structure and build setup. | ||
|
|
||
| `<OUTPUT_DIRECTORY>` is the directory you set in `--dest`. The generated files for the symbol mode are placed in the symbol subdirectory. | ||
|
|
||
| - `*.svg`. | ||
|
|
||
| If you want to work with icons in an existing project, replace `*.svg` with a glob pattern that matches the location of your own icons. | ||
|
|
||
| For example, `src/assets/icons/**/*.svg` matches SVG files in the src/assets/icons directory and all its subdirectories. | ||
|
|
||
| ```bash | ||
| svgforge --symbol --symbol-render-css --symbol-example --dest=<OUTPUT_DIRECTORY> 'src/assets/icons/**/*.svg' | ||
| ``` | ||
|
|
||
| If the output folder contains the generated files, everything is working — you've just built your first sprite. If you specified a directory other than out in --dest, look for the generated files there. | ||
|
|
||
| ## Checking the sprite | ||
|
|
||
| **1.** Open the `out` folder or another folder you set in `--dest`. Inside, you should find the following files: | ||
|
|
||
| - `symbol/svg/sprite.symbol.svg` | ||
| - `symbol/sprite.css` | ||
| - `symbol/sprite.symbol.html` | ||
|
|
||
| **2.** Open `sprite.symbol.html` by double-clicking it, or right-click → **Open with** → your browser. A page with the icons from the sprite should open. | ||
|
|
||
| If you can see the icons, everything is working perfectly and you can move on. | ||
|
|
||
| If the page is empty or the icons are not displayed, your browser may be blocking external SVG references when the page is opened directly. In that case, serve the folder locally: | ||
|
|
||
| ```bash | ||
| npx serve <OUTPUT_DIRECTORY>/symbol | ||
| ``` | ||
|
|
||
| In this example, the symbol output folder is `out/symbol`. | ||
|
|
||
| Now open the link shown in the terminal in your browser. It will look something like this: | ||
|
|
||
| ```text | ||
| http://localhost:3000/sprite.symbol.html | ||
| ``` | ||
|
|
||
| ## Using the sprite | ||
|
|
||
| Now the icons from the sprite can be used on any HTML page via `<use>`. | ||
|
|
||
| **1.** Create a file named `test-use.html` inside the `out/symbol` folder or another symbol output folder you set in `--dest`. | ||
|
|
||
| **2.** Paste the following code into the file: | ||
|
|
||
| ```html | ||
| <svg width="32" height="32"> | ||
| <use href="svg/sprite.symbol.svg#home"></use> | ||
| </svg> | ||
| ``` | ||
|
|
||
| `#home` is the icon's identifier in the symbol sprite. By default, it matches the original SVG file name without the extension: `home.svg` → `home`. | ||
|
|
||
| Replace `home` with the ID of the icon you want to use. | ||
|
|
||
| **To find your icon's identifier:** | ||
|
|
||
| - Open the preview page: `http://localhost:3000/sprite.symbol.html` | ||
| - Find the icon you need. | ||
| - Click the **Copy ID** button next to it — the identifier will be copied to your clipboard. | ||
| - Paste it in place of `home` in `<use href="svg/sprite.symbol.svg#...">`. | ||
|
|
||
| **3.** If the local server is not running, restart it. Then open the `test-use.html` page in your browser. It will be available at a URL like: | ||
|
|
||
| ```text | ||
| http://localhost:3000/test-use.html | ||
| ``` | ||
|
|
||
| The browser should display the icon whose identifier you specified in `<use>`. | ||
|
|
||
| ## Learn more | ||
|
|
||
| - [SVG sprite modes](https://github.com/svgforge/svgforge#output-modes) | ||
| - [Command-line reference](https://github.com/svgforge/svgforge-cli/blob/main/docs/command-line.md) | ||
| - [Configuration reference](https://github.com/svgforge/svgforge/blob/main/docs/configuration.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| --- | ||
|
|
||
| title: "Quickstart: Build your first sprite in 5 minutes" | ||
| description: A quick introduction to svgforge that helps you install the tool, create your first sprite, and see how to use icons from the sprite in your own product. | ||
| pubDate: 2026-11-07 | ||
| draft: true | ||
| tags: | ||
|
|
||
| - tutorial | ||
| - svg | ||
|
|
||
| --- | ||
|
|
||
| In this guide, we'll build a **symbol sprite** and test its icons on a page using `<use>`. | ||
|
|
||
| svgforge also supports other sprite modes: `view`, `stack`, and `defs`. For details, see [Specific mode properties](https://github.com/svgforge/svgforge/blob/main/docs/configuration.md#specific-mode-properties). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Node.js 24 or later | ||
|
|
||
| ## Setup | ||
|
|
||
| **1.** Create a project folder to work in. Let's call it `svgforge-project`. | ||
|
|
||
| **2.** Download a few SVG icons into that folder. | ||
|
|
||
| **3.** Install the svgforge: | ||
|
|
||
| ```bash | ||
| npm install @svgforge/svgforge | ||
| ``` | ||
|
|
||
| You should see output similar to this in the terminal: | ||
|
|
||
| ``` | ||
| added 38 packages in 1m | ||
|
|
||
| 12 packages are looking for funding | ||
| run `npm fund` for details | ||
| ``` | ||
|
|
||
| ## Building the sprite | ||
|
|
||
| **1.** Create an `.mjs` file in the project folder (for example, `build-sprite.mjs`) and paste in the following code: | ||
|
|
||
| ```js | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import SVGSpriter from "@svgforge/svgforge"; | ||
|
|
||
| const config = { | ||
| dest: "out-lib", | ||
| mode: { | ||
| symbol: { | ||
| example: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const spriter = new SVGSpriter(config); | ||
|
|
||
| for (const file of fs.globSync("*.svg")) { | ||
| spriter.add(file, null, fs.readFileSync(file, "utf8")); | ||
| } | ||
|
|
||
| const { result } = await spriter.compileAsync(); | ||
|
|
||
| for (const mode of Object.values(result)) { | ||
| for (const resource of Object.values(mode)) { | ||
| fs.mkdirSync(path.dirname(resource.path), { recursive: true }); | ||
| fs.writeFileSync(resource.path, resource.contents); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This script uses svgforge to combine SVG icons from your project into a single **symbol sprite** and saves the generated files to the `out-lib` directory. | ||
|
|
||
| - `dest: "out-lib"` sets the output directory where the generated files are written. | ||
|
|
||
| In this example, the generated files are written to the `out-lib` directory in the root of your `svgforge-project` folder. | ||
|
|
||
| If you're using svgforge in an existing project, set `dest` to the directory where you want the generated files to be written. For example, this could be `dist` or `public`, depending on your project's structure and build setup. | ||
|
|
||
| `<OUTPUT_DIRECTORY>` is the directory you set in `dest`. | ||
|
|
||
| - `fs.globSync("*.svg")` finds SVG files matching the specified glob pattern. | ||
| Replace `*.svg` with the path to your icon directory. For example, `src/assets/icons/**/*.svg` matches SVG files in the `src/assets/icons` directory and all its subdirectories. | ||
|
|
||
| Save the file. | ||
|
|
||
| **2.** Run the `.mjs` file in the terminal: | ||
|
|
||
| ```bash | ||
| node build-sprite.mjs | ||
| ``` | ||
|
|
||
| If you named the file differently in the previous step, replace `build-sprite.mjs` with your file name. For example, if you created `icons.mjs`, run: | ||
|
|
||
| ```bash | ||
| node icons.mjs | ||
| ``` | ||
|
|
||
| Two files will appear in the `<OUTPUT_DIRECTORY>/symbol/` folder: | ||
|
|
||
| - `sprite.symbol.svg` — the sprite itself | ||
| - `sprite.symbol.html` — a preview page (generated because of `example: true`) | ||
|
|
||
| If you set `dest: "out-lib"`, the files will appear in the `out-lib/symbol/` folder. | ||
|
|
||
| ## Checking the sprite | ||
|
|
||
| **1.** Start a local server: | ||
|
|
||
| ```bash | ||
| npx serve <OUTPUT_DIRECTORY> | ||
| ``` | ||
| If you set `dest: "out-lib"`, run: | ||
|
|
||
| ```bash | ||
| npx serve out-lib | ||
| ``` | ||
|
|
||
| **2.** Open in your browser: | ||
|
|
||
| ``` | ||
| http://localhost:3000/symbol/sprite.symbol.html | ||
| ``` | ||
|
|
||
| If the terminal shows a different port, use that port in the URL. | ||
|
|
||
| You should see a confirmation that the sprite is ready. Now the icons from the sprite can be used on any page via `<use>`. | ||
|
|
||
| ## Using the sprite | ||
|
|
||
| **1.** Create an HTML file inside the `<OUTPUT_DIRECTORY>/symbol/` folder. In this example — `out-lib/symbol/`. | ||
|
|
||
| Name it `test-use.html`. | ||
|
|
||
| **2.** Paste the following code into the file: | ||
|
|
||
| ```html | ||
| <svg width="32" height="32"> | ||
| <use href="svg/sprite.symbol.svg#home"></use> | ||
| </svg> | ||
| ``` | ||
|
|
||
| `#home` is the icon's identifier in the symbol sprite. By default, it matches the original SVG file name without the extension: `home.svg` → `home`. | ||
|
|
||
| Replace `home` with the ID of the icon you want to use. | ||
|
|
||
| **How to find your icon's identifier:** | ||
|
|
||
| - Open the preview page: `http://localhost:3000/symbol/sprite.symbol.html` | ||
| - Find the icon you need. | ||
| - Click the **Copy ID** button next to it — the identifier will be copied to your clipboard. | ||
| - Paste it in place of `home` in `<use href="svg/sprite.symbol.svg#...">`. | ||
|
|
||
| Save the file. | ||
|
|
||
| **3.** If the local server is not running, restart it. Then open in your browser: | ||
|
|
||
| ``` | ||
| http://localhost:3000/symbol/test-use.html | ||
| ``` | ||
|
|
||
| The browser should display the icon whose identifier you specified in `<use>`. | ||
|
|
||
| ## Learn more | ||
|
|
||
| - [Using symbol sprites via `<use>`](https://github.com/svgforge/svgforge/blob/main/docs/configuration.md#defs--symbol-mode) | ||
| - [Other sprite modes: `view`, `stack`, `defs`](https://github.com/svgforge/svgforge/blob/main/docs/configuration.md#specific-mode-properties) | ||
| - [Full sprite configuration documentation](https://github.com/svgforge/svgforge/blob/main/docs/configuration.md) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.