1 | CSS MQPacker
|
2 | ============
|
3 |
|
4 | Pack same CSS media query rules into one media query rule.
|
5 |
|
6 | Written with [PostCSS][1].
|
7 |
|
8 |
|
9 | SYNOPSIS
|
10 | --------
|
11 |
|
12 | A CSS file processed with a CSS pre-processor may have same queries that can
|
13 | merge:
|
14 |
|
15 | ```css
|
16 | .foo::before {
|
17 | content: "foo on small";
|
18 | }
|
19 |
|
20 | @media screen and (min-width: 769px) {
|
21 | .foo::before {
|
22 | content: "foo on medium";
|
23 | }
|
24 | }
|
25 |
|
26 | .bar::before {
|
27 | content: "bar on small";
|
28 | }
|
29 |
|
30 | @media screen and (min-width: 769px) {
|
31 | .bar::before {
|
32 | content: "bar on medium";
|
33 | }
|
34 | }
|
35 | ```
|
36 |
|
37 | This PostCSS plugin packs exactly same queries (and optionally sorts) like this:
|
38 |
|
39 | ```css
|
40 | .foo::before {
|
41 | content: "foo on small";
|
42 | }
|
43 |
|
44 | .bar::before {
|
45 | content: "bar on small";
|
46 | }
|
47 |
|
48 | @media screen and (min-width: 769px) {
|
49 | .foo::before {
|
50 | content: "foo on medium";
|
51 | }
|
52 | .bar::before {
|
53 | content: "bar on medium";
|
54 | }
|
55 | }
|
56 | ```
|
57 |
|
58 |
|
59 | INSTALL
|
60 | -------
|
61 |
|
62 | $ npm install css-mqpacker
|
63 |
|
64 |
|
65 | USAGE
|
66 | -----
|
67 |
|
68 | Of course, this package can be used as PostCSS plugin:
|
69 |
|
70 | ```javascript
|
71 | #!/usr/bin/env node
|
72 |
|
73 | "use strict";
|
74 |
|
75 | var fs = require("fs");
|
76 | var postcss = require("postcss");
|
77 |
|
78 | var css = fs.readFileSync("from.css", "utf8");
|
79 | postcss([
|
80 | require("autoprefixer-core")(),
|
81 | require("css-mqpacker")()
|
82 | ]).process(css).then(function (result) {
|
83 | console.log(result.css);
|
84 | });
|
85 | ```
|
86 |
|
87 |
|
88 | ### As standard Node.js package
|
89 |
|
90 | Read `from.css`, process its content, and output processed CSS to STDOUT.
|
91 |
|
92 | ```javascript
|
93 | #!/usr/bin/env node
|
94 |
|
95 | "use strict";
|
96 |
|
97 | var fs = require("fs");
|
98 | var mqpacker = require("css-mqpacker");
|
99 |
|
100 | var original = fs.readFileSync("from.css", "utf8");
|
101 | var processed = mqpacker.pack(original, {
|
102 | from: "from.css",
|
103 | map: {
|
104 | inline: false
|
105 | },
|
106 | to: "to.css"
|
107 | });
|
108 | console.log(processed.css);
|
109 | ```
|
110 |
|
111 |
|
112 | ### As CLI Program
|
113 |
|
114 | This package also installs a command line interface.
|
115 |
|
116 | $ node ./node_modules/.bin/mqpacker --help
|
117 | Usage: mqpacker [options] INPUT [OUTPUT]
|
118 |
|
119 | Description:
|
120 | Pack same CSS media query rules into one media query rule.
|
121 |
|
122 | Options:
|
123 | -s, --sort Sort `min-width` queries.
|
124 | --sourcemap Create source map file.
|
125 | -h, --help Show this message.
|
126 | -v, --version Print version information.
|
127 |
|
128 | Use a single dash for INPUT to read CSS from standard input.
|
129 |
|
130 | When PostCSS failed to parse INPUT, CLI shows a CSS parse error in GNU error
|
131 | format instead of Node.js stack trace.
|
132 |
|
133 | The `--sort` option does not currently support a custom function.
|
134 |
|
135 |
|
136 | OPTIONS
|
137 | -------
|
138 |
|
139 | ### sort
|
140 |
|
141 | By default, CSS MQPacker pack and order media queries as they are defined. If
|
142 | you want to sort queries automatically, pass `sort: true` to this module.
|
143 |
|
144 | ```javascript
|
145 | postcss([
|
146 | mqpacker({
|
147 | sort: true
|
148 | })
|
149 | ]).process(css);
|
150 | ```
|
151 |
|
152 | Currently, this option only supports `min-width` queries with specific units
|
153 | (`ch`, `em`, `ex`, `px`, and `rem`). If you want to do more, you need to create
|
154 | your own sorting function and pass it to this module like this:
|
155 |
|
156 | ```javascript
|
157 | postcss([
|
158 | mqpacker({
|
159 | sort: function (a, b) {
|
160 | return a.localeCompare(b);
|
161 | }
|
162 | })
|
163 | ]).process(css);
|
164 | ```
|
165 |
|
166 | In this example, all your queries will sort by A-Z order.
|
167 |
|
168 | This sorting function directly pass to `Array#sort()` method of an array of all
|
169 | your queries.
|
170 |
|
171 |
|
172 | API
|
173 | ---
|
174 |
|
175 | ### pack(css, [options])
|
176 |
|
177 | Packs media queries in `css`.
|
178 |
|
179 | The second argument is optional. The `options` is same as the second argument of
|
180 | PostCSS’s `process()` method. This is useful for generating Source Map.
|
181 |
|
182 | ```javascript
|
183 | var fs = require("fs");
|
184 | var mqpacker = require("css-mqpacker");
|
185 |
|
186 | var css = fs.readFileSync("from.css", "utf8");
|
187 | var result = mqpacker.pack(css, {
|
188 | from: "from.css",
|
189 | map: {
|
190 | inline: false
|
191 | },
|
192 | to: "to.css"
|
193 | });
|
194 | fs.writeFileSync("to.css", result.css);
|
195 | fs.writeFileSync("to.css.map", result.map);
|
196 | ```
|
197 |
|
198 | See also [PostCSS document][2] for more about this `options`.
|
199 |
|
200 |
|
201 | LICENSE
|
202 | -------
|
203 |
|
204 | MIT: http://hail2u.mit-license.org/2014
|
205 |
|
206 |
|
207 | [1]: https://github.com/postcss/postcss
|
208 | [2]: https://github.com/postcss/postcss#source-map
|