svg-url-loader
Converts SVG file to utf-8 encoded data-uri string
Last updated a month ago by bhovhannes .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install svg-url-loader 
SYNC missed versions from official npm registry.

svg-url-loader

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

A webpack loader which loads SVG file as utf-8 encoded DataUrl string.

Existing url-loader always does Base64 encoding for data-uri. As SVG content is a human-readable xml string, using base64 encoding is not mandatory. Instead, one may only escape unsafe characters and replace " with ' as described in this article.

There are some benefits for choosing utf-8 encoding over base64.

  1. Resulting string is shorter (can be ~2 times shorter for 2K-sized icons);
  2. Resulting string will be compressed better when using gzip compression;
  3. Browser parses utf-8 encoded string faster than its base64 equivalent.

Supported parameters

Parameters can be passed both in an url or from webpack config file. See Loaders section in webpack documentation for more details.

The loader supports the following parameters:

limit

If given, loader will not encode the source file if its content is greater than this limit. Defaults to no limit. If the file is greater than the limit the file-loader is used, receiving all query parameters set for svg-url-loader.

require('svg-url-loader?limit=1024!./file.svg');
// => DataUrl if "file.png" is smaller that 1kb

require('svg-url-loader?prefix=img/!./file.svg');
// => Parameters for the file-loader are valid too
//    They are passed to the file-loader if used.

stripdeclarations

This option is true by default. It will be removed in the next major release.
See this issue for more context about this decision.

If given will tell the loader to strip out any XML declaration, e.g. <?xml version="1.0" encoding="UTF-8"?> at the beginning of imported SVGs. Internet Explorer (tested in Edge 14) cannot handle XML declarations in CSS data URLs (content: url("data:image/svg...")).

require('svg-url-loader?stripdeclarations!./file.svg');

iesafe

This option falls back to the file-loader if the file contains a style-element and the encoded size is above 4kB no matter the limit specified.

Internet Explorer (including IE11) stops parsing style-elements in SVG data-URIs longer than 4kB. This results in black fill-color for all styled shapes.

require('svg-url-loader?iesafe!./file.svg');

encoding

This option controls which encoding to use when constructing a data-URI for an SVG. When set to a non-"none" value, loader does not apply quotes to the resulting data-URI.

Possible values are "base64" and "none". Defaults to "none".

require('svg-url-loader?encoding=base64!./file.svg');

Usage

Documentation: Loaders

In JS:

require('svg-url-loader!./file.svg');
// => DataUrl for file.svg

In CSS (with webpack.config.js below):

.icon {
  background: url('../images/file.svg');
}
module.exports = {
  //...
  module: {
    rules: [
      {
        test: /\.svg/,
        use: {
          loader: 'svg-url-loader',
          options: {}
        }
      }
    ]
  },
  //...
};

Benefits of using data-uri

  1. Browser won't make an extra http call to download the image
  2. Some folks mentioned that even if image is in browser cache images with data url appear on screen faster.

License

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

Current Tags

  • 6.0.0                                ...           latest (a month ago)

25 Versions

  • 6.0.0                                ...           a month ago
  • 5.0.1                                ...           a month ago
  • 5.0.0                                ...           4 months ago
  • 4.0.0                                ...           5 months ago
  • 3.0.3                                ...           8 months ago
  • 3.0.2                                ...           10 months ago
  • 3.0.1                                ...           10 months ago
  • 3.0.0                                ...           a year ago
  • 2.3.3                                ...           a year ago
  • 2.3.2                                ...           2 years ago
  • 2.3.1                                ...           3 years ago
  • 2.3.0                                ...           3 years ago
  • 2.2.1                                ...           3 years ago
  • 2.2.0                                ...           3 years ago
  • 2.1.1                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 35
This Week 1,106
This Month 2,168
Last Day 345
Last Week 1,704
Last Month 5,951
Dependencies (2)
Dev Dependencies (8)
Dependents (160)

Copyright 2014 - 2017 © taobao.org |