svg-sprite-loader vs svg-sprite vs gulp-svg-sprite
SVG Sprite Management Libraries Comparison
1 Year
svg-sprite-loadersvg-spritegulp-svg-spriteSimilar Packages:
What's SVG Sprite Management Libraries?

SVG sprite management libraries are tools designed to optimize the use of SVG graphics in web development. They allow developers to combine multiple SVG files into a single sprite sheet, reducing the number of HTTP requests and improving loading times. These libraries provide various features for creating, managing, and loading SVG sprites, making it easier to work with scalable vector graphics in web applications.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
svg-sprite-loader206,8912,022-1483 years agoMIT
svg-sprite139,0851,949213 kB58a year agoMIT
gulp-svg-sprite34,20565013.4 kB42 years agoMIT
Feature Comparison: svg-sprite-loader vs svg-sprite vs gulp-svg-sprite

Integration with Build Tools

  • svg-sprite-loader:

    Designed specifically for Webpack, enabling you to import SVGs as modules in your JavaScript files. This integration allows for a more modern development approach, leveraging Webpack's capabilities for handling assets.

  • svg-sprite:

    Functions as a standalone tool that can be run from the command line, making it versatile for different environments. It does not require a specific build tool, allowing for greater flexibility in integration into existing workflows.

  • gulp-svg-sprite:

    Integrates directly with Gulp, allowing you to define tasks for sprite generation, making it easy to automate the process as part of your build workflow. It supports various Gulp plugins for additional processing, such as minification and optimization.

Customization Options

  • svg-sprite-loader:

    Allows customization through Webpack configuration, enabling you to define how SVGs are processed and how they are exposed to your application. This includes options for setting the prefix for sprite IDs and controlling how SVGs are rendered.

  • svg-sprite:

    Provides a range of command-line options for customizing the sprite generation process, including output directory, sprite naming, and CSS output formats. This allows for a high degree of control over the final output.

  • gulp-svg-sprite:

    Offers extensive customization options through Gulp configuration, allowing you to specify how sprites are generated, including options for dimensions, naming conventions, and output formats. This flexibility helps tailor the sprite creation process to your project's needs.

Performance Optimization

  • svg-sprite-loader:

    Utilizes Webpack's code-splitting features to load SVG sprites on demand, which can improve initial load times by only loading the necessary SVGs when required. This lazy loading capability enhances performance in larger applications.

  • svg-sprite:

    Generates a single sprite file from multiple SVGs, significantly reducing the number of requests made to the server. This optimization is crucial for improving page load speeds, especially when using many SVG icons.

  • gulp-svg-sprite:

    Optimizes SVG files during the sprite generation process, reducing file sizes and improving performance. It can also combine multiple SVGs into a single file, which decreases the number of HTTP requests and enhances loading times.

Ease of Use

  • svg-sprite-loader:

    Integrates seamlessly with modern JavaScript frameworks, allowing developers to use SVGs as components directly in their code. This approach simplifies the process of using SVGs in applications, especially for those familiar with Webpack.

  • svg-sprite:

    Easy to use with a simple command-line interface, making it accessible for developers who prefer not to use a build tool. It is suitable for quick setups and small projects where a full build system may not be necessary.

  • gulp-svg-sprite:

    Requires some familiarity with Gulp, but once set up, it provides a straightforward way to manage SVG sprites as part of your build process. The Gulp ecosystem offers many plugins that can enhance its usability further.

Community and Support

  • svg-sprite-loader:

    Part of the Webpack ecosystem, which has a large and active community. Users can find extensive documentation, tutorials, and community support, making it easier to troubleshoot issues and learn best practices.

  • svg-sprite:

    As a standalone tool, it has a dedicated user base and documentation, making it easy to find support and examples for usage. Its simplicity contributes to a lower barrier for entry for new users.

  • gulp-svg-sprite:

    Has a strong community due to its integration with Gulp, which is widely used in the web development ecosystem. This package benefits from community contributions and a wealth of resources available for Gulp users.

How to Choose: svg-sprite-loader vs svg-sprite vs gulp-svg-sprite
  • svg-sprite-loader:

    Opt for svg-sprite-loader if you are using Webpack and want to import SVG files as React components or Vue components directly in your JavaScript code. This loader allows for dynamic loading of SVG sprites, making it suitable for modern front-end frameworks and applications.

  • svg-sprite:

    Select svg-sprite if you need a standalone solution that provides a command-line interface for generating SVG sprites. This package is ideal for projects where you want to manage SVG sprites without a build tool dependency, offering flexibility in how you integrate it into your workflow.

  • gulp-svg-sprite:

    Choose gulp-svg-sprite if you are already using Gulp as your build system and need a straightforward way to automate the creation of SVG sprites during your build process. It integrates seamlessly with Gulp tasks and offers a range of customization options for sprite generation.

README for svg-sprite-loader

SVG sprite loader

NPM version Build status Documentation score Dependencies status Dev dependencies status NPM downloads

Webpack loader for creating SVG sprites.

:tada: 2.0 is out, please read the migration guide & overview.

:warning: For old v0.x versions see the README in the v0 branch.

Table of contents

Why it's cool

  • Minimum initial configuration. Most of the options are configured automatically.
  • Runtime for browser. Sprites are rendered and injected in pages automatically, you just refer to images via <svg><use xlink:href="#id"></use></svg>.
  • Isomorphic runtime for node/browser. Can render sprites on server or in browser manually.
  • Customizable. Write/extend runtime module to implement custom sprite behaviour. Write/extend runtime generator to produce your own runtime, e.g. React component configured with imported symbol.
  • External sprite file is generated for images imported from css/scss/sass/less/styl/html (SVG stacking technique).

Installation

npm install svg-sprite-loader -D
# via yarn
yarn add svg-sprite-loader -D

Configuration

// webpack 1
{
  test: /\.svg$/,
  loader: 'svg-sprite-loader',
  query: { ... }
}

// webpack 1 multiple loaders
{
  test: /\.svg$/,
  loaders: [
    `svg-sprite-loader?${JSON.stringify({ ... })}`,
    'svg-transform-loader',
    'svgo-loader'
  ]
}

// webpack >= 2
{
  test: /\.svg$/,
  loader: 'svg-sprite-loader',
  options: { ... }
}

// webpack >= 2 multiple loaders
{
  test: /\.svg$/,
  use: [
    { loader: 'svg-sprite-loader', options: { ... } },
    'svg-transform-loader',
    'svgo-loader'
  ]
}

symbolId (string | function(path, query), default [name])

How <symbol> id attribute should be named. All patterns from loader-utils#interpolateName are supported. Also can be a function which accepts 2 args - file path and query string and return symbol id:

{
  symbolId: filePath => path.basename(filePath)
}

symbolRegExp (default '')

Passed to the symbolId interpolator to support the [N] pattern in the loader-utils name interpolator

esModule (default true, autoconfigured)

Generated export format:

  • when true loader will produce export default ....
  • when false the result is module.exports = ....

By default depends on used webpack version: true for webpack >= 2, false otherwise.

Runtime configuration

When you require an image, loader transforms it to SVG <symbol>, adds it to the special sprite storage and returns class instance that represents symbol. It contains id, viewBox and content (id, viewBox and url in extract mode) fields and can later be used for referencing the sprite image, e.g:

import twitterLogo from './logos/twitter.svg';
// twitterLogo === SpriteSymbol<id: string, viewBox: string, content: string>
// Extract mode: SpriteSymbol<id: string, viewBox: string, url: string, toString: Function>

const rendered = `
<svg viewBox="${twitterLogo.viewBox}">
  <use xlink:href="#${twitterLogo.id}" />
</svg>`;

When browser event DOMContentLoaded is fired, sprite will be automatically rendered and injected in the document.body. If custom behaviour is needed (e.g. a different mounting target) default sprite module could be overridden via spriteModule option. Check example below.

spriteModule (autoconfigured)

Path to sprite module that will be compiled and executed at runtime. By default it depends on target webpack config option:

  • svg-sprite-loader/runtime/browser-sprite.build for 'web' target.
  • svg-sprite-loader/runtime/sprite.build for other targets.

If you need custom behavior, use this option to specify a path of your sprite implementation module. Path will be resolved relative to the current webpack build folder, e.g. utils/sprite.js placed in current project dir should be written as ./utils/sprite.

Example of sprite with custom mounting target (copypasted from browser-sprite):

import BrowserSprite from 'svg-baker-runtime/src/browser-sprite';
import domready from 'domready';

const sprite = new BrowserSprite();
domready(() => sprite.mount('#my-custom-mounting-target'));

export default sprite; // don't forget to export!

It's highly recommended to extend default sprite classes:

symbolModule (autoconfigured)

Same as spriteModule, but for sprite symbol. By default also depends on target webpack config option:

  • svg-baker-runtime/browser-symbol for 'web' target.
  • svg-baker-runtime/symbol for other targets.

runtimeGenerator (default generator)

Path to node.js script that generates client runtime. Use this option if you need to produce your own runtime, e.g. React component configured with imported symbol. Example.

runtimeCompat (default false, deprecated)

Should runtime be compatible with earlier v0.x loader versions. This option will be removed in the next major version release.

runtimeOptions

Arbitrary data passed to runtime generator. Reserved for future use when other runtime generators will be created.

Extract configuration

In the extract mode loader should be configured with plugin, otherwise an error is thrown. Example:

// webpack.config.js
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');

...

{
  plugins: [
    new SpriteLoaderPlugin()
  ]
}

extract (default false, autoconfigured)

Switches loader to the extract mode. Enabled automatically for images imported from css/scss/sass/less/styl/html files.

spriteFilename (type string|Function<string>,default sprite.svg)

Filename of extracted sprite. Multiple sprites can be generated by specifying different loader rules restricted with include option or by providing custom function which recieves SVG file absolute path, e.g.:

{
  test: /\.svg$/,
  loader: 'svg-sprite-loader',
  options: {
    extract: true,
    spriteFilename: svgPath => `sprite${svgPath.substr(-4)}`
  }
}

[hash] in sprite filename will be replaced by it's content hash. It is also possible to generate sprite for each chunk by using [chunkname] pattern in spriteFilename option. This is experimental feature, use it with caution!

publicPath (type: string, default: __webpack_public_path__)

Custom public path for sprite file.

{
  test: /\.svg$/,
  loader: 'svg-sprite-loader',
  options: {
    extract: true,
    publicPath: '/'
  }
}

outputPath (type: string, default: null`)

Custom output path for sprite file. By default it will use publicPath. This param is useful if you want to store sprite is a directory with a custom http access.

{
  test: /\.svg$/,
  loader: 'svg-sprite-loader',
  options: {
    extract: true,
    outputPath: 'custom-dir/sprites/'
    publicPath: 'sprites/'
  }
}

Plain sprite

You can render plain sprite in extract mode without styles and usages. Pass plainSprite: true option to plugin constructor:

{
  plugins: [
    new SpriteLoaderPlugin({ plainSprite: true })
  ]
}

Sprite attributes

Sprite <svg> tag attributes can be specified via spriteAttrs plugin option:

{
  plugins: [
    new SpriteLoaderPlugin({
      plainSprite: true,
      spriteAttrs: {
        id: 'my-custom-sprite-id'
      }
    })
  ]
}

Examples

See examples folder.

Contributing guidelines

See CONTRIBUTING.md.

License

See LICENSE

Credits

Huge thanks for all this people.