Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(postcss-reduce-initial): added ignore options #929

Merged
merged 4 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 20 additions & 12 deletions packages/postcss-reduce-initial/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# [postcss][postcss]-reduce-initial

> Reduce `initial` definitions to the *actual* initial value, where possible.

> Reduce `initial` definitions to the _actual_ initial value, where possible.

## Install

Expand All @@ -11,7 +10,6 @@ With [npm](https://npmjs.org/package/postcss-reduce-initial) do:
npm install postcss-reduce-initial --save
```


## Examples

See the [data](data) for more conversions. This data is courtesy
Expand All @@ -26,19 +24,18 @@ be converted:

```css
h1 {
min-width: initial;
min-width: initial;
}
```

#### Output

```css
h1 {
min-width: 0;
min-width: 0;
}
```


### Convert values back to `initial`

When the `initial` value is smaller than the property value, it will
Expand All @@ -48,39 +45,50 @@ be converted:

```css
h1 {
transform-box: border-box;
transform-box: border-box;
}
```

#### Output

```css
h1 {
transform-box: initial;
transform-box: initial;
}
```

This conversion is only applied when you supply a browsers list that all support
the `initial` keyword; it's worth noting that Internet Explorer has no support.

## API

### reduceInitial([options])

#### options

##### ignore

Type: `Array<String>`
Default: `undefined`

It contains the Array of properties that will be ignored while reducing its value to initial.
Example : `{ ignore : ["min-height"] }`

## Usage

See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for
examples for your environment.


## Contributors

See [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).


## License

[Template:CSSData] by Mozilla Contributors is licensed under [CC-BY-SA 2.5].

[Template:CSSData]: https://developer.mozilla.org/en-US/docs/Template:CSSData
[CC-BY-SA 2.5]: http://creativecommons.org/licenses/by-sa/2.5/
[template:cssdata]: https://developer.mozilla.org/en-US/docs/Template:CSSData
[cc-by-sa 2.5]: http://creativecommons.org/licenses/by-sa/2.5/

MIT © [Ben Briggs](http://beneb.info)

Expand Down
27 changes: 27 additions & 0 deletions packages/postcss-reduce-initial/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,30 @@ test(
'min-height auto tests',
processCSS('h1{min-height:initial}', 'h1{min-height:auto}')
);

test(
'should ignore the data present in the ignore options',
passthroughCSS('h1{min-height:initial}', { ignore: ['min-height'] })
);

test(
'should ignore the data present in the ignore options #2',
processCSS(
'h1{ writing-mode: sideways-rl;}',
'h1{ writing-mode: sideways-rl;}',
{ ignore: ['writing-mode'] }
)
);

test(
'should ignore the data present in the ignore options #3',
processCSS(
'h1{ writing-mode: vertical-lr;}',
'h1{ writing-mode: vertical-lr;}',
{ ignore: [] }
)
);
test(
'should ignore the data present in the ignore options , toInitial #3',
passthroughCSS('WRITING-MODE:horizontal-tb', { ignore: ['writing-mode'] })
);
8 changes: 8 additions & 0 deletions packages/postcss-reduce-initial/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import toInitial from '../data/toInitial.json';

const initial = 'initial';

// In most of the browser including chrome the initial for `writing-mode` is not `horizontal-tb`. Ref https://github.com/cssnano/cssnano/pull/905
const defaultIgnoreProps = ['writing-mode'];
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

export default plugin('postcss-reduce-initial', () => {
return (css, result) => {
const resultOpts = result.opts || {};
Expand All @@ -20,6 +23,11 @@ export default plugin('postcss-reduce-initial', () => {

css.walkDecls((decl) => {
const lowerCasedProp = decl.prop.toLowerCase();
const ignoreProp = defaultIgnoreProps.concat(resultOpts.ignore || []);

if (ignoreProp.includes(lowerCasedProp)) {
return;
}

if (
initialSupport &&
Expand Down