UNPKG

3.92 kBMarkdownView Raw
1CSS MQPacker
2============
3
4Pack same CSS media query rules into one media query rule.
5
6Written with [PostCSS][1].
7
8
9SYNOPSIS
10--------
11
12A CSS file processed with a CSS pre-processor may have same queries that can
13merge:
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
37This 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
59INSTALL
60-------
61
62 $ npm install css-mqpacker
63
64
65USAGE
66-----
67
68Of course, this package can be used as PostCSS plugin:
69
70```javascript
71#!/usr/bin/env node
72
73"use strict";
74
75var fs = require("fs");
76var postcss = require("postcss");
77
78var css = fs.readFileSync("from.css", "utf8");
79postcss([
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
90Read `from.css`, process its content, and output processed CSS to STDOUT.
91
92```javascript
93#!/usr/bin/env node
94
95"use strict";
96
97var fs = require("fs");
98var mqpacker = require("css-mqpacker");
99
100var original = fs.readFileSync("from.css", "utf8");
101var processed = mqpacker.pack(original, {
102 from: "from.css",
103 map: {
104 inline: false
105 },
106 to: "to.css"
107});
108console.log(processed.css);
109```
110
111
112### As CLI Program
113
114This 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
130When PostCSS failed to parse INPUT, CLI shows a CSS parse error in GNU error
131format instead of Node.js stack trace.
132
133The `--sort` option does not currently support a custom function.
134
135
136OPTIONS
137-------
138
139### sort
140
141By default, CSS MQPacker pack and order media queries as they are defined. If
142you want to sort queries automatically, pass `sort: true` to this module.
143
144```javascript
145postcss([
146 mqpacker({
147 sort: true
148 })
149]).process(css);
150```
151
152Currently, 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
154your own sorting function and pass it to this module like this:
155
156```javascript
157postcss([
158 mqpacker({
159 sort: function (a, b) {
160 return a.localeCompare(b);
161 }
162 })
163]).process(css);
164```
165
166In this example, all your queries will sort by A-Z order.
167
168This sorting function directly pass to `Array#sort()` method of an array of all
169your queries.
170
171
172API
173---
174
175### pack(css, [options])
176
177Packs media queries in `css`.
178
179The second argument is optional. The `options` is same as the second argument of
180PostCSS’s `process()` method. This is useful for generating Source Map.
181
182```javascript
183var fs = require("fs");
184var mqpacker = require("css-mqpacker");
185
186var css = fs.readFileSync("from.css", "utf8");
187var result = mqpacker.pack(css, {
188 from: "from.css",
189 map: {
190 inline: false
191 },
192 to: "to.css"
193});
194fs.writeFileSync("to.css", result.css);
195fs.writeFileSync("to.css.map", result.map);
196```
197
198See also [PostCSS document][2] for more about this `options`.
199
200
201LICENSE
202-------
203
204MIT: http://hail2u.mit-license.org/2014
205
206
207[1]: https://github.com/postcss/postcss
208[2]: https://github.com/postcss/postcss#source-map