# @ltipton/babel-plugin-restyle-css

> Babel plugin to convert reStyle css strings into JS objects durning build

Latest version **0.1.0** (published 2020-12-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ltipton/babel-plugin-restyle-css
pnpm add @ltipton/babel-plugin-restyle-css
yarn add @ltipton/babel-plugin-restyle-css
bun add @ltipton/babel-plugin-restyle-css
```

## 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.1.0 |
| Published | 2020-12-29 |
| First published | 2020-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Lance Tipton |
| Maintainers | lktipton |
| Keywords | reStyle, restyle, theme, css, keg, re-theme, retheme, babel-plugin |

## Links

- npm: https://www.npmjs.com/package/@ltipton/babel-plugin-restyle-css
- Repository: git@github.com:lancetipton/babel-plugin-restyle-css
- Homepage: https://github.com/lancetipton/babel-plugin-restyle-css
- Issues: https://github.com/lancetipton/babel-plugin-restyle-css/issues
- npm.io page: https://npm.io/package/@ltipton/babel-plugin-restyle-css

## Dependencies (3)

- [postcss](https://npm.io/package/postcss.md) 7.0.14
- [camelcase](https://npm.io/package/camelcase.md) 5.0.0
- [postcss-safe-parser](https://npm.io/package/postcss-safe-parser.md) 4.0.1

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2020-12-29
- 0.0.2 — 2020-12-29
- 0.0.1 — 2020-12-29

## README

# babel-plugin-restyle-css

Babel plugin to convert reStyle css strings into JS objects durning build.
<br/>
Code originally taken from [babel-plugin-css-to-js](https://github.com/jakecoxon/babel-plugin-css-to-js).
<br/>
It was then modified to meet the needs of this library.
  * `css` prop was changed to `reStyle`
  * All dependencies were updated to the latest versions


## Install

**NPM**
```sh
npm install babel-plugin-restyle-css --save-dev
```

**Yarn**
```sh
yarn add babel-plugin-restyle-css --dev
```

## Setup

Add it to your babel config ( `.babelrc` || `babel.config.js` )
```js
{
  "plugins": [["restyle-css"]]
}
```

**Post CSS Plugins**
It uses postCSS under the hood to do the CSS conversion. Which means any you can pass any postCSS plugins you want by adding them to the babel config

```js
{
  "plugins": [["restyle-css", {
    // Add PostCss Plugins ( optional )
    "plugins": ["autoprefixer"]
  }]]
}
```

**method**
By default the `reStyle` method will be used in transposed code. To modify the name of the method, use the method property in the babel config.
```js
{
  "plugins": [["restyle-css", {
    "method": 'customMethod'
  }]]
}
```
Setting the `method` property to `false` will omit the method call entirely. Use this to get just the converted style object. See example below for more info


## Examples

### Example 1
Basic setup with the `reStyle` method

**Babel config**
```js
{
  "plugins": [["restyle-css"]]
}
```

**In App**
*  Import the `reStyle` method at the top of your file, then use it when building your styles

```javascript
import { reStyle } from '@keg-hub/retheme/reStyle'

const rule = props => reStyle`
  font-size: ${props.fontSize}px;
  margin-top: ${props.margin ? '15px' : 0};
  color: red;
  line-height: 1.4;
  :hover {
    color: blue;
    fontSize: ${props.fontSize + 2}px;
  }
  @media (min-height: 300px) {
    background-color: gray;
    :hover {
      color: black;
    }
  }
`
```

**Build Output**
```javascript
const rule = props => reStyle({
  fontSize: props.fontSize + 'px',
  marginTop: props.margin ? '15px' : 0,
  color: 'red',
  lineHeight: 1.4,
  ':hover': {
    color: 'blue',
    fontSize: props.fontSize + 2 + 'px'
  },
  '@media (min-height: 300px)': {
    backgroundColor: 'gray',
    ':hover': {
      color: 'black'
    }
  }
})
```

### Example 2
With a custom method, which will overwrite the `reStyle` method in the build output

**Babel config**
```js
{
  "plugins": [["restyle-css", {
    "method": 'customMethod'
  }]]
}
```

**In App**
*  Import your `customMethod` method at the top of your file. You can use `reStyle` or `customMethod` when building your styles

```javascript
import { customMethod } from './path/to/custom/method'

// reStyle will be replaced by customMethod
const rule = props => reStyle`
  font-size: ${props.fontSize}px;
`
// Or use customMethod here directly
const rule = props => customMethod`
  font-size: ${props.fontSize}px;
`
```

**Build Output**
```javascript
const rule = props => customMethod({
  fontSize: props.fontSize + 'px'
})
```


### Example 3
With the `method` property set to `false` in the babel config.
> Works the same as the [babel-plugin-css-to-js](https://github.com/jakecoxon/babel-plugin-css-to-js) plugin

**Babel config**
```js
{
  "plugins": [["restyle-css", {
    "method": false
  }]]
}
```

**In App**
*  Still use `reStyle` when building your styles

```javascript
// reStyle will be removed by the plugin
const rule = props => reStyle`
  font-size: ${props.fontSize}px;
`
```

**Build Output**
```javascript
// The converted styles object will be returned, without a method call
const rule = props => ({
  fontSize: props.fontSize + 'px'
})
```

## Notes
You may have a need to convert styles inline of a `reStyle` definition. A helper tag has been added to allow this functionality. Similar to settng the `method` property to false. It will return the converted styles as an object.

**In App**
```js
const rule = props => reStyle`
  .inline-w-helper {
    composes: ${props.foo ? reInline`border: 1px` : undefined};
  }
`
```

**Build Output**
```js
const rule = props => reStyle({
  '.inline-w-helper': {
    composes: props.foo ? { border: "1px" } : undefined,
  }
})
```

---
_Source: https://npm.io/package/@ltipton/babel-plugin-restyle-css · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
