Skip to content

Commit 017e1e4

Browse files
committed
Improve: add docs & examples
1 parent 2687978 commit 017e1e4

File tree

1 file changed

+214
-7
lines changed

1 file changed

+214
-7
lines changed

README.md

Lines changed: 214 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,219 @@
1-
# postcss-sprites
2-
Generate sprites from stylesheets.
1+
# PostCSS Sprites
2+
[PostCSS](https://github.com/postcss/postcss) plugin that generate sprites from your stylesheets and then updates image references.
3+
4+
## Todo
5+
6+
- [ ] Tests
7+
- [ ] Publish as NPM module
8+
9+
## Install
310

4-
### Install
511
```
612
npm install git+https://git@github.com/2createStudio/postcss-sprites.git
713
```
814

9-
### Todo
10-
- [ ] Docs
11-
- [ ] Examples
12-
- [ ] Tests
15+
## Example
16+
17+
```javascript
18+
var postcss = require('postcss');
19+
var sprites = require('postcss-sprites');
20+
var opts = {
21+
externalStyle: './dist/sprite.css',
22+
spritePath : './dist/images',
23+
spriteName : 'sprite.png',
24+
retina : true
25+
};
26+
27+
postcss(sprites(opts))
28+
.process(css, { from: 'src/app.css', to: 'app.css' })
29+
.then(function (result) {
30+
fs.writeFileSync('app.css', result.css);
31+
});
32+
```
33+
34+
## Options (plugin)
35+
36+
#### spriteName
37+
38+
Type: `String`
39+
Default: `sprite.png`
40+
Example: `all.png`
41+
Required: `true`
42+
43+
Defines the base of output sprite. Base means that if you will group your sprites by some criteria, name will change.
44+
45+
#### spritePath
46+
47+
Type: `String`
48+
Default: `null`
49+
Example: `./dist`
50+
Required: `true`
51+
52+
Can define relative path of references in the output stylesheet.
53+
54+
#### externalStyle
55+
56+
Type: `String`
57+
Default: `false`
58+
Example: `./dist/sprite.css`
59+
Required: `false`
60+
61+
Can define standalone CSS file that will be generated from the sprite.
62+
63+
#### filterBy
64+
65+
Type: `Function[], Function`
66+
Default: `[]`
67+
Example: [filterByFolder](#)
68+
Required: `false`
69+
70+
Defines which filters apply to images found in the stylesheet. Each filter will be called with an image object. Each filter must return `Boolean` or thenable `Promise`, that will be resolved with `Boolean`. Each filter applies in series.
71+
72+
Built in filters:
73+
74+
- based on `fs.exists`, which is used to remove non existing images.
75+
76+
#### groupBy
77+
78+
Type: `Function[], Function`
79+
Default: `[]`
80+
Example: `./dist/sprite.css`
81+
Required: `false`
82+
83+
Defines logic of how to group images found in the stylesheet. Each grouper called with an image object. Each filter must return `String|Null` or thenable `Promise`, that will be resolved with `String|Null`. Each grouper applies in series.
84+
85+
Built in groupers:
86+
87+
- based on `@2x` image naming syntax, will produce `sprite.@{number}x.png` naming. (`@{number}x` image group).
88+
89+
#### retina
90+
91+
Type: `Boolean`
92+
Default: `false`
93+
Example: `true`
94+
Required: `false`
95+
96+
Defines whether or not to search for retina mark in the filename. If true then it will look for `@{number}x` syntax. For example: `image@2x.png`.
97+
98+
#### verbose
99+
100+
Type: `Boolean`
101+
Default: `false`
102+
Example: `true`
103+
Required: `false`
104+
105+
Suppress `console.log` messages.
106+
107+
## Options ([spritesmith](https://github.com/Ensighten/spritesmith))
108+
109+
All options for spritesmith are optional. For more detailed information you can visit
110+
the official page of [spritesmith](https://github.com/Ensighten/spritesmith).
111+
112+
#### engine
113+
114+
Type: `String`
115+
Default: `pixelsmith`
116+
117+
#### algorithm
118+
119+
Type: `String`
120+
Default: `binary-tree`
121+
122+
#### padding
123+
124+
Type: `Number`
125+
Default: `0`
126+
127+
#### engineOpts
128+
129+
Type: `Object`
130+
Default: `{}`
131+
132+
#### exportOpts
133+
134+
Type: `Object`
135+
Default: `{}`
136+
137+
## The Image
138+
139+
Every filter or grouper is called with an ``image`` object, which have following properties:
140+
141+
#### path
142+
143+
Type: `String`
144+
Example: `/Users/john/project/image.png`
145+
146+
Resolved path to the image.
147+
148+
#### url
149+
150+
Type: `String`
151+
Example: `images/image.png`
152+
153+
Url for image found in the stylesheet.
154+
155+
#### ratio
156+
157+
Type: `String`
158+
Default: `1`
159+
160+
Ratio of the retina image - e.g. @2x => 2
161+
162+
#### retina
163+
164+
Type: `Boolean`
165+
Default: `false`
166+
167+
The flag that indicates a retina image.
168+
169+
#### groups
170+
171+
Type: `Array`
172+
Default: `[]`
173+
174+
The groups associated with the image after grouping functions.
175+
176+
#### token
177+
178+
Type: `Object`
179+
180+
The string used to update image reference in the stylesheet.
181+
This token is [PostCSS Comment](https://github.com/postcss/postcss/blob/master/docs/api.md#comment-node).
182+
183+
#### selector
184+
185+
Type: `String`
186+
187+
The CSS selector used for this image in the external stylesheet.
188+
189+
## Advanced Example
190+
191+
```javascript
192+
var postcss = require('postcss');
193+
var sprites = require('postcss-sprites');
194+
var Q = require('q');
195+
var opts = {
196+
spritePath : './dist/images',
197+
spriteName : 'sprite.png',
198+
verbos : true,
199+
filterBy : function(image) {
200+
return /\.jpg$/gi.test(image.url);
201+
},
202+
groupBy : function(image) {
203+
return Q.Promise(function(resolve, reject) {
204+
// Do something here...
205+
206+
resolve(image); // or reject(image);
207+
});
208+
}
209+
};
210+
211+
postcss(sprites(opts))
212+
.process(css, { from: 'src/app.css', to: 'app.css' })
213+
.then(function (result) {
214+
fs.writeFileSync('app.css', result.css);
215+
});
216+
```
217+
218+
## License
219+
MIT © 2createStudio

0 commit comments

Comments
 (0)