# babel-plugin-rn-extract-style

> Babel plugin for extracting react native inline styles to StyleSheet.create({})

Latest version **0.0.9** (published 2020-11-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-rn-extract-style
pnpm add babel-plugin-rn-extract-style
yarn add babel-plugin-rn-extract-style
bun add babel-plugin-rn-extract-style
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2020-11-11 |
| First published | 2020-11-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Teodor Druta |
| Maintainers | teod |
| Keywords | babel, plugin, rn, react, native, style, styles, extract, inline, stylesheet |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-rn-extract-style
- Repository: https://github.com/teod/babel-plugin-rn-extract-style
- Homepage: https://github.com/teod/babel-plugin-rn-extract-style#readme
- Issues: https://github.com/teod/babel-plugin-rn-extract-style/issues
- npm.io page: https://npm.io/package/babel-plugin-rn-extract-style

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.9 (latest) — 2020-11-11
- 0.0.8 — 2020-11-09
- 0.0.7 — 2020-11-06
- 0.0.6 — 2020-11-06
- 0.0.5 — 2020-11-06
- 0.0.4 — 2020-11-06
- 0.0.3 — 2020-11-06
- 0.0.2 — 2020-11-05
- 0.0.1 — 2020-11-05

## README

# Babel plugin for extracting react native inline styles to StyleSheet.create

[![npm version](https://img.shields.io/npm/v/babel-plugin-rn-extract-style.svg?style=flat-square)](https://www.npmjs.com/package/babel-plugin-rn-extract-style)

[Description](#description) | [Install](#install) | [Usage](#usage) | [How it works ?](#how-it-works)

## Description

This babel plugin will extract any inline styles to the `StyleSheet.create` object in a react native application.

## Why it's important to have styles inside `StyleSheet.create` ?

The React Native `StyleSheet` creates immutable stylesheet references.
The style objects passed into the `create()` method are freezed and an ID is assigned to each one of them.
This will avoid creating a new style objecct every render pass.
It also allows to send the style only once through the bridge.

## Install

<i>npm:</i>

```sh
npm install --save-dev babel-plugin-rn-extract-style
```

<i>yarn:</i>

```sh
yarn add babel-plugin-rn-extract-style --dev
```

## Usage

<b>babel.config.js</b>
```js
module.exports = {
  plugins: [
    'rn-extract-style'
  ]
}
```

### Include `node_modules`

By default files from the `node_module` are ignored if, you wish to include those you have to specify the `includeNodeModules: true` option:

<b>babel.config.js</b>
```js
module.exports = {
  plugins: [
    [
      './plugin.js',
      {
        includeNodeModules: true,
      },
    ],
  ]
}
```

## How it works

The plugin parses the contents of each JSX Element and extract the inline styles, generates an uniquie uid for it and appends it to the `StyleSheet.create` object found in the file. In case of a missing `StyleSheet.create` instance it generates a new one.

<b>in:</b>
```javascript
const Component = () => (
  <View
    style={{
      height: 500,
      width: '100%',
      backgroundColor: '#6c5b7b',
    }}>
    <View style={{ height: 200 }}>
      <Text style={{ fontSize: 12 }}>Lorem ipsum</Text>
    </View>
  </View>
)
```
<b>out:</b>
```javascript
const Component = () => (
  <View
    style={babelGeneratedStyles.View_style}>
    <View style={babelGeneratedStyles.View_style1}>
      <Text style={babelGeneratedStyles.Text_style2}>Lorem ipsum</Text>
    </View>
  </View>
)

const babelGeneratedStyles = StyleSheet.create({
  View_style: {
    height: 500,
    width: '100%',
    backgroundColor: '#6c5b7b',
  },
  View_style1: {
    height: 200,
  },
  Text_style1: {
    fontSize: 12,
  },
})
```

---
_Source: https://npm.io/package/babel-plugin-rn-extract-style · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
