A webpack loader which wraps content of root element of source SVG file inside <symbol> element and returns resulting markup.
JavaScript
Clone or download

README.md

svg-as-symbol-loader

NPM version NPM downloads Dependencies Dev. Dependencies MIT License Build Status

A webpack loader which wraps content of root element of source SVG file inside <symbol> element and returns resulting markup.

What loader does

It takes contents of root element in source SVG markup (usually, root element will be <svg>), wraps it into <symbol> tag and returns resulting markup.
It is possible to use another tag instead of <symbol> using tag loader parameter.
Attributes applied to root element in source SVG file will be preserved and applied to target <symbol> tag.
If source SVG image contains elements with id attribute set, loader will append unique prefix to all ids in order to make them unique in the universe.

Why do I need this?

There may be different usage scenarios for different people. One of them is described below.

Usually data:URI scheme is used to embed icons in SVG file. That results in SVG file containing urls with data:URI scheme.
Although support for such url scheme is pretty good, some SVG parsers do not understand such urls. For example, Apache Batik fails to rasterize SVG markup including urls with data:URI to PDF.
If embedded icon is SVG itself, it can be included by copy/pasting its markup into target SVG file. In order to point to inserted element, it should have an id attribute assigned, which can be assigned using id loader parameter.

Supported parameters

The loader supports the following parameters:

tag

Defaults to symbol. Is used as the name of root tag in generated SVG markup.

id

If given, will be applied to the root tag (symbol by default, see description for tag option) in generated SVG markup.

class

If given, will be applied to the root tag in generated SVG markup.

viewBox

If given, overwrites value of viewBox attribute applied to the svg tag in source SVG file.

height

If given, overwrites value of height attribute applied to the svg tag in source SVG file.

width

If given, overwrites value of width attribute applied to the svg tag in source SVG file.

preserveAspectRatio

If given, overwrites value of preserveAspectRatio attribute applied to the svg tag in source SVG file.

Parameters can be passed both in a url or from webpack config file. See Using loaders section in webpack documentation for more details.

The id and class parameters allow naming templates based on filename interpolation. See loader-utils#interpolatename for full usage details.

Usage

Documentation: Using loaders

License

MIT (http://www.opensource.org/licenses/mit-license.php)