All Projects → bhovhannes → Svg Url Loader

bhovhannes / Svg Url Loader

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Svg Url Loader

Svgr
Transform SVGs into React components 🦁
Stars: ✭ 8,263 (+3707.83%)
Mutual labels:  webpack-loader, svg
Svg Inline Loader
Inline SVG loader with cleaning-up functionality
Stars: ✭ 490 (+125.81%)
Mutual labels:  webpack-loader, svg
Svg Sprite Loader
Webpack loader for creating SVG sprites.
Stars: ✭ 1,822 (+739.63%)
Mutual labels:  webpack-loader, svg
React Svg Loader
A loader for webpack, rollup, babel that loads svg as a React Component
Stars: ✭ 570 (+162.67%)
Mutual labels:  webpack-loader, svg
Svg Fill Loader
DEPRECATED, use https://github.com/kisenka/svg-mixer/tree/master/packages/svg-transform-loader instead
Stars: ✭ 74 (-65.9%)
Mutual labels:  webpack-loader, svg
Vue Svg Inline Loader
Webpack loader used for inline replacement of SVG images with actual content of SVG files in Vue projects.
Stars: ✭ 105 (-51.61%)
Mutual labels:  webpack-loader, svg
Webfonts Loader
Make an icon font from SVGs!
Stars: ✭ 153 (-29.49%)
Mutual labels:  webpack-loader, svg
Svg Text Animate
A Javascript library for convert texts to SVG stroke animations in the browser.
Stars: ✭ 197 (-9.22%)
Mutual labels:  svg
Picasso
Picasso is a high quality 2D vector graphic rendering library. It support path , matrix , gradient , pattern , image and truetype font.
Stars: ✭ 205 (-5.53%)
Mutual labels:  svg
Vue Awesome
Awesome SVG icon component for Vue.js, built-in with Font Awesome icons.
Stars: ✭ 2,302 (+960.83%)
Mutual labels:  svg
Svelte Awesome
Awesome SVG icon component for Svelte JS, built with Font Awesome icons. Based on Justineo/vue-awesome
Stars: ✭ 193 (-11.06%)
Mutual labels:  svg
Svg Patterns
SVG patterns for Data Visualization.
Stars: ✭ 201 (-7.37%)
Mutual labels:  svg
Svgbob
Convert your ascii diagram scribbles into happy little SVG
Stars: ✭ 3,031 (+1296.77%)
Mutual labels:  svg
Virtual Dom
The foundation of HTML and SVG in Elm.
Stars: ✭ 196 (-9.68%)
Mutual labels:  svg
Ng Inline Svg
[Inactive] Angular directive for inserting an SVG file inline within an element.
Stars: ✭ 211 (-2.76%)
Mutual labels:  svg
Animating Vue Workshop
Animated Interfaces with Vue.js Workshop Materials
Stars: ✭ 195 (-10.14%)
Mutual labels:  svg
React Sigma
Lightweight React library for drawing network graphs built on top of SigmaJS
Stars: ✭ 217 (+0%)
Mutual labels:  svg
Contrast Swatch
🅰️ Image microservice for color contrast information
Stars: ✭ 210 (-3.23%)
Mutual labels:  svg
Small N Flat
svg icons on a 24px grid
Stars: ✭ 205 (-5.53%)
Mutual labels:  svg
String Replace Loader
Replace loader for Webpack
Stars: ✭ 205 (-5.53%)
Mutual labels:  webpack-loader

svg-url-loader

NPM version NPM downloads codecov MIT License

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.

Passing parameters using resourceQuery is also supported:

.selector {
  background-image: url(../assets/foo.svg?encoding=base64);
}

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 tells loader to fall back to the file-loader if the file contains a style-element and the encoded size is above 4kB no matter the limit specified.

This option was introduced because 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.

You can either specify iesafe option in the query:

// apply `iesafe` option only for 'foo.svg'
require("svg-url-loader?iesafe!./foo.svg");

Or, you can set this option globally in you webpack config:

module.exports = {
  //...
  module: {
    rules: [
      {
        test: /\.svg/,
        use: {
          loader: "svg-url-loader",
          options: {
            // make all svg images to work in IE
            iesafe: true,
          },
        },
      },
    ],
  },
  //...
};

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".

Normally, setting encoding option to base64 should not be required, as using base64 encoding defeats the original purpose of this loader - embed svg with minimal size overhead. However, because of incompatibility with some tools, we support this mode, too.

You can either specify base64 option in the query:

// apply `base64` option only for 'foo.svg'
require("svg-url-loader?encoding=base64!./foo.svg");

Or, you can set this option globally in you webpack config:

module.exports = {
  //...
  module: {
    rules: [
      {
        test: /\.svg/,
        use: {
          loader: "svg-url-loader",
          options: {
            // make loader to behave like url-loader, for all svg files
            encoding: "base64",
          },
        },
      },
    ],
  },
  //...
};

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)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].