npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-svg-asset-plugin

v0.5.0

Published

Asset plugin for importing SVG images in React Native

Downloads

18,290

Readme

react-native-svg-asset-plugin

Build Status

Asset plugin for React Native which enables using SVGs with Image components. Works by generating PNGs during compile time, and passing them to the metro transformer.

:iphone: If you also want to use SVG images for your application launcher icons, you might want to check out react-native-svg-app-icon.

Installation

npm

npm install --save-dev react-native-svg-asset-plugin

No dependencies outside of NPM. Uses sharp for SVG rasterization.

Requires React Native version 0.57 or later to work. Expo not supported, instead you might want to use react-native-svg-transformer.

metro

Add 'react-native-svg-asset-plugin' to the list of assetPlugins in your metro.config.js file under the transformer section.

For example;

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
    assetPlugins: ['react-native-svg-asset-plugin'],
  },
};

Usage

Just require your SVG files directly into React Native Image and ImageBackground components. For example:

<Image source={require('./assets/image.svg')} />

Scaled PNGs will be generated under the subdirectory .png-cache alongside the SVG files, so you might want to add a .gitignore entry to exclude the cache directory from your code repo.

Configuration

You can configure the plugin behaviour through the optional svgAssetPlugin field in your metro.config.js file under the transformer section.

For example;

module.exports = {
  transformer: {
    // ...
    assetPlugins: ['react-native-svg-asset-plugin'],
    svgAssetPlugin: {
      pngCacheDir: '.png-cache',
      scales: [1, 2, 3],
      output: {
        compressionLevel: 9,
      },
    },
  },
};

Where the possible configuration values are:

| Field | Type | Default | Description | |---------------|----------|----------------|-----------------------------------| | cacheDir | string | '.png-cache' | Name of directory to store cached PNGs. | | scales | number[] | [1, 2, 3] | PNG image scales to generate for different screen densities. | | output | object | {} | Sharp PNG output options. | | ignoreRegex | RegExp | null | Regex that will be matched against the source file's full path, if there's a match the file will be ignored. |

You will need to reset the bundler cache with react-native start --reset-cache for configuration changes to take effect for already generated images.

Comparison with react-native-svg

Most alternative ways of displaying SVG content in React Native apps are based on the react-native-svg library, which provides runtime rendering of SVG images. react-native-svg-asset-plugin works differently by rasetrizing the vector images to PNGs during compile time, and using the native image rendering APIs.

| | react-native-svg | react-native-svg-asset-plugin | |----------------------|:------------------:|:-------------------------------:| | Rasterization | Runtime | Compile time | | Runtime dependencies | JS + Native | None | | Image compatibility | No | Yes | | Remote assets | Yes | No | | App size | Smaller | Larger |

Technical details

The plugin works by intercepting loaded SVG assets, and transforming them to PNGs before they are loaded by the metro transformer. After being loaded by the transformer, they work as any other PNG file in React Native, meaning you can use and style them freely in Image components.

Each SVG file produces three PNG files in 1x, 2x and 3x scales. The size of the PNG images are defined by the width and height attributes of the SVG images.

SVGs are rasterized to PNGs using the sharp Node.js library, which is based on libvips C library, which includes the librsvg library that renders the SVG images.