@@ -7,48 +7,47 @@ Pack same CSS media query rules into one using PostCSS
7
7
SYNOPSIS
8
8
--------
9
9
10
- A CSS file processed with a CSS pre-processor may have same queries that can
11
- merge:
10
+ A well componentized CSS file may have same media queries that can merge:
12
11
13
12
``` css
14
- .foo ::before {
15
- content : " foo on small " ;
13
+ .foo {
14
+ width : 240 px ;
16
15
}
17
16
18
- @media screen and (min-width : 769 px ) {
19
- .foo ::before {
20
- content : " foo on medium " ;
17
+ @media screen and (min-width : 768 px ) {
18
+ .foo {
19
+ width : 576 px ;
21
20
}
22
21
}
23
22
24
- .bar ::before {
25
- content : " bar on small " ;
23
+ .bar {
24
+ width : 160 px ;
26
25
}
27
26
28
- @media screen and (min-width : 769 px ) {
29
- .bar ::before {
30
- content : " bar on medium " ;
27
+ @media screen and (min-width : 768 px ) {
28
+ .bar {
29
+ width : 384 px ;
31
30
}
32
31
}
33
32
```
34
33
35
- This PostCSS plugin packs exactly same queries (and optionally sorts) like this :
34
+ This PostCSS plugin packs exactly same media queries :
36
35
37
36
``` css
38
- .foo ::before {
39
- content : " foo on small " ;
37
+ .foo {
38
+ width : 240 px ;
40
39
}
41
40
42
- .bar ::before {
43
- content : " bar on small " ;
41
+ .bar {
42
+ width : 160 px ;
44
43
}
45
44
46
- @media screen and (min-width : 769 px ) {
47
- .foo ::before {
48
- content : " foo on medium " ;
45
+ @media screen and (min-width : 768 px ) {
46
+ .foo {
47
+ width : 576 px ;
49
48
}
50
- .bar ::before {
51
- content : " bar on medium " ;
49
+ .bar {
50
+ width : 384 px ;
52
51
}
53
52
}
54
53
```
@@ -70,40 +69,40 @@ Of course, this package can be used as PostCSS plugin:
70
69
71
70
" use strict" ;
72
71
73
- var fs = require (" fs" );
74
- var postcss = require (" postcss" );
72
+ const fs = require (" fs" );
73
+ cosnt postcss = require (" postcss" );
75
74
76
- var css = fs .readFileSync (" from.css" , " utf8" );
77
75
postcss ([
78
76
require (" autoprefixer-core" )(),
79
77
require (" css-mqpacker" )()
80
- ]).process (css).then (function (result ) {
78
+ ]).process (fs . readFileSync ( " from. css" , " utf8 " ) ).then (function (result ) {
81
79
console .log (result .css );
82
80
});
83
81
```
84
82
83
+ It is a recommended way to use this tool.
84
+
85
85
86
86
### As standard Node.js package
87
87
88
- Read ` from.css ` , process its content, and output processed CSS to STDOUT.
88
+ This package is also a Node.js module. For exmaple, you can read ` from.css ` ,
89
+ process its content, and output processed CSS to STDOUT:
89
90
90
91
``` javascript
91
92
#! / usr/ bin/ env node
92
93
93
94
" use strict" ;
94
95
95
- var fs = require (" fs" );
96
- var mqpacker = require (" css-mqpacker" );
96
+ const fs = require (" fs" );
97
+ const mqpacker = require (" css-mqpacker" );
97
98
98
- var original = fs .readFileSync (" from.css" , " utf8" );
99
- var processed = mqpacker .pack (original, {
99
+ cosole .log (mqpacker .pack (fs .readFileSync (" from.css" , " utf8" ), {
100
100
from: " from.css" ,
101
101
map: {
102
102
inline: false
103
103
},
104
104
to: " to.css"
105
- });
106
- console .log (processed .css );
105
+ }).css );
107
106
```
108
107
109
108
@@ -134,14 +133,17 @@ format instead of Node.js stack trace.
134
133
135
134
The ` --sort ` option does not currently support a custom function.
136
135
136
+ If you install this package in global, CLI will be available somewhere in the
137
+ ` $PATH ` .
138
+
137
139
138
140
OPTIONS
139
141
-------
140
142
141
143
### sort
142
144
143
- By default, CSS MQPacker pack and order media queries as they are defined. See
144
- also [ The "First Win" Algorithm ] [ 1 ] . If you want to sort queries automatically,
145
+ By default, CSS MQPacker pack and order media queries as they are defined ( [ the
146
+ “first win” algorithm ] [ 1 ] ) . If you want to sort media queries automatically,
145
147
pass ` sort: true ` to this module.
146
148
147
149
``` javascript
@@ -166,10 +168,10 @@ postcss([
166
168
]).process (css);
167
169
```
168
170
169
- In this example, all your queries will sort by A-Z order.
171
+ In this example, all your media queries will sort by A-Z order.
170
172
171
- This sorting function directly pass to ` Array#sort() ` method of an array of all
172
- your queries.
173
+ This sorting function is directly passed to ` Array#sort() ` method of an array of
174
+ all your media queries.
173
175
174
176
175
177
API
@@ -187,11 +189,10 @@ The second argument is optional. The `options` are:
187
189
You can specify both at the same time.
188
190
189
191
``` javascript
190
- var fs = require (" fs" );
191
- var mqpacker = require (" css-mqpacker" );
192
+ cosnt fs = require (" fs" );
193
+ const mqpacker = require (" css-mqpacker" );
192
194
193
- var css = fs .readFileSync (" from.css" , " utf8" );
194
- var result = mqpacker .pack (css, {
195
+ const result = mqpacker .pack (fs .readFileSync (" from.css" , " utf8" ), {
195
196
from: " from.css" ,
196
197
map: {
197
198
inline: false
@@ -243,8 +244,8 @@ Becomes:
243
244
}
244
245
```
245
246
246
- ` .foo ` is always ` 400px ` in original CSS, but ` 300px ` if viewport is wider than
247
- `640px' with processed CSS .
247
+ ` .foo ` is always ` 400px ` with original CSS. With processed CSS, however, ` .foo `
248
+ is ` 300px ` if viewport is wider than ` 640px ` .
248
249
249
250
This does not occur on small project. On large project, however, this could
250
251
occur frequently. For example, if you want to override a CSS framework (like
@@ -253,9 +254,9 @@ to above example. To avoid this problem, you should pack only CSS you write, and
253
254
then concaenate with a CSS framework.
254
255
255
256
256
- ### The " First Win" Algorithm
257
+ ### The “ First Win” Algorithm
257
258
258
- CSS MQPacker is implemented with the " first win" algorithm. This means:
259
+ CSS MQPacker is implemented with the “ first win” algorithm. This means:
259
260
260
261
``` css
261
262
.foo {
@@ -322,13 +323,13 @@ I suggest defining a query order on top of your CSS:
322
323
@media (min-width : 640px ) { /* ! Wider than 640px */ }
323
324
```
324
325
325
- If you use only simple ` min-width ` queries, [ the ` sort ` option] [ 4 ] can help.
326
+ If you use simple ` min-width ` queries only , [ the ` sort ` option] [ 4 ] can help.
326
327
327
328
328
329
### Multiple Classes
329
330
330
331
CSS MQPacker works only with CSS. This may break CSS applying order to an
331
- elements that have multiple classes.
332
+ elements that have multiple classes. For example:
332
333
333
334
``` css
334
335
@media (min-width : 320px ) {
0 commit comments