# postcss-relaxed-unit

> PostCSS plugin Make write csss easier with custom unit

Latest version **1.1.1** (published 2019-10-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install postcss-relaxed-unit
pnpm add postcss-relaxed-unit
yarn add postcss-relaxed-unit
bun add postcss-relaxed-unit
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2019-10-16 |
| First published | 2019-09-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 45 |
| Author | HaoDaWang |
| Maintainers | younccat |
| Keywords | postcss, css, postcss-plugin, unit |

## Links

- npm: https://www.npmjs.com/package/postcss-relaxed-unit
- Repository: https://github.com/youncccat/postcss-relaxed-unit
- Issues: https://github.com/youncccat/postcss-relaxed-unit/issues
- npm.io page: https://npm.io/package/postcss-relaxed-unit

## Dependencies (2)

- [postcss](https://npm.io/package/postcss.md) ^7.0.6
- [bignumber.js](https://npm.io/package/bignumber.js.md) ^9.0.0

## 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

- 1.1.1 (latest) — 2019-10-16
- 1.0.7 — 2019-10-08
- 1.0.5 — 2019-09-29
- 1.0.4 — 2019-09-29
- 1.0.3 — 2019-09-29
- 1.0.2 — 2019-09-28
- 1.0.1 — 2019-09-28
- 1.0.0 — 2019-09-28

## README

# Postcss-relaxed-unit 🍮

[postcss]: https://github.com/postcss/postcss
[ci-img]: https://travis-ci.org/youncccat/postcss-relaxed-unit.svg
[ci]: https://travis-ci.org/youncccat/postcss-relaxed-unit

English doc | [中文文档](https://github.com/youncccat/postcss-relaxed-unit/blob/master/doc/readme-zh.md)

Postcss-relaxed-unit is a postcss plugin for unit tranformation and make write css easier with custom unit.

You can define a rule to determine the mapping relationship between custom unit and target unit, and you can use some operators to calculate the target value (like `add`, `sub`, `mul`, `div`, `unit`), so you can write style relaxed without care about calculate unit.

## Motivation

Sometimes, we need to care about calculating the style values, for example, `px` to `rem`， `rem` to `vw` , `px` to whatever that mobile side need and we don't need many plugins. so, `postcss-relaxed-unit` can help you forget that, you just need define a rule that describes the custom unit to target unit mapping! that's all! :tada:

## Precision overflow 👌

You don't have to care about precision overflow, because `postcss-relaxed-unit` wraps `bignumber.js`.

## Install

`postcss-relaxed-unit` is publish to `npm`，so you can install it using `npm` or `yarn`

```
npm i postcss-relaxed-unit -D
```

or

```
yarn add postcss-relaxed-unit -D
```

because `postcss-relaxed-unit` is depends on [PostCSS], you need to install postcss.

## Usage

You only need to define rule to get start~

**postcss.config.js**

```javascript
const RelaxedUnit = require("postcss-relaxed-unit");

module.exports = {
  plugins: [
    RelaxedUnit({
      rules: { rx: "add(1).sub(2).mul(3).div(9).unit(rem)" }
    })
  ]
};
```

### Multiple rules

You can define more rules :)

```javascript
const RelaxedUnit = require("postcss-relaxed-unit");

module.exports = {
  plugins: [
    RelaxedUnit({
      rules: {
        rx: "add(1).sub(2).mul(3).div(9).unit(rem)",
        ex: "div(100).unit(rem)"
      }
    })
  ]
};
```

### Nuxt

In `Nuxt.js`, you need to define the config in `nuxt.config.js`

**nuxt.config.js**

```js
module.exports = {
 	build: {
    extractCSS: true,
    postcss: {
      plugins: {
        'postcss-relaxed-unit': {
          rules: { rx: 'div(100).unit(rem)' },
        },
    },
  },
}
```

## Options

- `rules {[custom unit name]: 'operators'}` custom unit to target unit mapping container

- `add Operator` target value plus `+`

- `sub Operator` target value subtraction `-`

- `mul Operator` target value multiplication `*`

- `div Operator` target value divition `/`

- `unit Operator` unit of output

custom unit will output does not change missing `unit` Operator, `e.g.`

```json
{
  "postcss-relaxed-unit": {
    "rules": { "rx": "add(10).sub(2)" }
  }
}
```

**origin style**

```css
.hello-relaxed-unit {
  width: 10rx;
}
```

**output style**

```css
.hello-relaxed-unit {
  width: 18rx;
}
```

The signature of operator function like

```typescript
type OperatorFunction = (arg: number | string) => string;
```

so, if you call the operator function like `add(aas)` , it will compile passing, the `aas` wiil be convert to `0`.

```javascript
{"rx": "add(aas).unit(px)"} => {"rx": "add(0).unit(px)"}
```

## Example

run `yarn example` or `npm run example` if you want to see the results of `postcss-relaxed-unit` working.⚙️

## LICENSE

MIT.

---
_Source: https://npm.io/package/postcss-relaxed-unit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
