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-get-asset

v1.0.0

Published

Copy asset to file system, or read asset as base64 data url

Downloads

43

Readme

react-native-get-asset

npm version npm downloads npm licence Platform

Copy asset to file system, or read asset as base64 data url.

Install

npm install react-native-get-asset

Note: peerDependencies are expo-asset and react-native-fs.

Bundle asset

Use environment.dds as an example, it's very simple to bundle environment.dds file into APP as an asset automatically at bundle time:

src below support Android, iOS and Web

Cause appHtml: resolveApp('public/index.html') in node_modules/react-scripts/config/paths.js which works with react-native-web and react-app-rewired, so it's better to put assets files into APP/public/ thus Web can use them too, that's why e.g. src: Platform.OS !== 'web' ? require('../public/textures/environment.dds') : 'textures/environment.dds' below.

PS: The usage of react-app-rewired can ref to my blog.

downloadUrlOrRequireId2Storage() to copy asset to file system

e.g. you can modify

const MODEL_URL =
  'https://github.com/facebookresearch/playtorch/releases/download/v0.2.0/yolov5s.ptl';
...
    const filePath = await MobileModel.download(MODEL_URL);

into

import GetAsset from 'react-native-get-asset';
...
const MODEL_URL =
  Platform.OS !== 'web'
    ? require('../../public/pytorch/yolov5s.ptl')
    : 'pytorch/yolov5s.ptl';
...
      const filePath = await GetAsset.downloadUrlOrRequireId2Storage({
        src: MODEL_URL,
        dst:
          Platform.OS !== 'web' &&
          Platform.select({
            android: `${RNFS.ExternalDirectoryPath}/detectObjects.ptl`,
            ios: `${RNFS.DocumentDirectoryPath}/detectObjects.ptl`,
          }),
        iosReleaseRequireIdAvoidCopy: true, // default is false
      });

Unlike asset on Android zipped in release.apk so must unzip copy, asset on iOS release is in bundle folder so that can avoid copy to access it directly, so can iosReleaseRequireIdAvoidCopy: true here.

PS: If react-native-get-asset does not satisfy your APP situation, you can try react-native-filereader or react-native-blob-util.

downloadUrlOrRequireId2DataURL() to read asset as base64 data url

e.g. https://github.com/flyskywhy/GCanvasRNExamples/blob/master/src/nonDeclarative.js:

import GetAsset from 'react-native-get-asset';
...
  const url = await GetAsset.downloadUrlOrRequireId2DataURL({
    // src: 'https://raw.githubusercontent.com/brianzinn/react-babylonjs/v3.1.3/packages/storybook/storyboard-site/assets/textures/environment.dds';
    src: Platform.OS !== 'web' ? require('../public/textures/environment.dds') : 'textures/environment.dds',
    urlAvoidDataURL: true, // default is false
  });

If your other code can deal with http url, you can urlAvoidDataURL: true here to return url instead of data url.

PS: data-uri.macro can convert files to data-uris at bundle time to avoid these asset matter.

Donate

To support my work, please consider donate.

  • ETH: 0xd02fa2738dcbba988904b5a9ef123f7a957dbb3e