https://github.com/svbutko/react-native-image-resource-generator
CLI tool to generate code-friendly image URI source constants for React Native
https://github.com/svbutko/react-native-image-resource-generator
generator hacktoberfest icons images javascript productivity react-native resources typescript
Last synced: 8 months ago
JSON representation
CLI tool to generate code-friendly image URI source constants for React Native
- Host: GitHub
- URL: https://github.com/svbutko/react-native-image-resource-generator
- Owner: svbutko
- License: mit
- Created: 2021-07-26T10:44:23.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-02-03T16:26:37.000Z (over 1 year ago)
- Last Synced: 2025-03-15T07:39:23.739Z (over 1 year ago)
- Topics: generator, hacktoberfest, icons, images, javascript, productivity, react-native, resources, typescript
- Language: JavaScript
- Homepage:
- Size: 34.2 KB
- Stars: 23
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-native-image-resource-generator
[](https://www.npmjs.com/package/react-native-image-resource-generator)
[](https://www.npmjs.com/package/react-native-image-resource-generator)
[](https://github.com/svbutko)
Generate code-friendly image URI source constants.
To learn about use cases and what issues it solves check [this blog post](https://dev.to/svbutko/react-native-image-resource-generator-m14).
## Quick start
### Installation
#### yarn
```sh
yarn add -D react-native-image-resource-generator
```
#### npm
```sh
npm install --save-dev react-native-image-resource-generator
```
### Usage
1. Create a folder and put all of your images there (_sub-folders are supported too_). Example:
```
project
│ package.json
│ src
│
└───resources
│ │ fonts
│ │ settings
│ │
│ └───images
│ │ arrow_down.png
│ │ arrow_down@2x.png
│ │ arrow_down@3x.png
│ │ arrow_up.png
│ │ ...
```
2. Add script to your `package.json` scripts or type into terminal:
* JavaScript: ```img-res-gen --dir=resources/images --out=src/common/ImageResources.js```
* TypeScript ```img-res-gen --dir=resources/images --out=src/common/ImageResources.g.ts --ts```
3. The result of the previous command will create a file with static image URI sources, which will look something similar to this:
```typescript
/* eslint:disable */
/* tslint:disable */
import {ImageURISource} from "react-native";
export class ImageResources {
static readonly account: ImageURISource = require("../../resources/images/account.png");
static readonly arrow_down: ImageURISource = require("../../resources/images/arrow_down.png");
static readonly arrow_up: ImageURISource = require("../../resources/images/arrow_up.png");
static readonly avatar: ImageURISource = require("../../resources/images/avatar.png");
static readonly back: ImageURISource = require("../../resources/images/back.png");
static readonly bank: ImageURISource = require("../../resources/images/bank.png");
static readonly bell: ImageURISource = require("../../resources/images/bell.png");
}
```
4. After this use it anywhere you need:
```typescript jsx
```
If you added or removed images, simply re-run the script to regenerate the file.
### Options
| Option | Description | Required | Example |
|--------|----------------------------------------------------------------------------|----------|----------------------------------------------------|
| dir | Relative directory path with images | `True` | `img-res-gen --dir=resources/images` |
| out | Output file path | `True` | `img-res-gen --out=src/common/ImageResources.g.ts` |
| read | Read directory path, adds additional path to a file's output required path | `False` | `img-res-gen --read=build/src` |
| ts | Should the output file be generated as a TypeScript file | `False` | `img-res-gen --ts` |