Skip to content

Commit bf83fb9

Browse files
authored
Merge pull request #85 from jedmao/update-docs
Add "Webpack Hot Load" example, update others
2 parents 68d6e97 + 5ac5757 commit bf83fb9

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ Every filter or group function will be called with an ``Image`` object.
256256
- [Skip Prefix](examples/skip-prefix.md)
257257
- [Responsive Spritesheets](examples/responsive-spritesheets.md)
258258
- [Relative To Rule](examples/relative-to-rule.md)
259+
- [Webpack Hot Load](examples/webpack-hot-load.md)
259260

260261
----
261262

examples/output-dimensions.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ var updateRule = require('postcss-sprites/lib/core').updateRule;
1515
var opts = {
1616
stylesheetPath: './css',
1717
spritePath: './css/images/',
18+
retina: true,
1819
hooks: {
1920
onUpdateRule: function(rule, token, image) {
2021
// Use built-in logic for background-image & background-position
2122
updateRule(rule, token, image);
2223

2324
['width', 'height'].forEach(function(prop) {
25+
var value = image.coords[prop];
26+
if (image.retina) {
27+
value /= image.ratio;
28+
}
2429
rule.insertAfter(rule.last, postcss.decl({
2530
prop: prop,
26-
value: image.coords[prop] + 'px'
31+
value: value + 'px'
2732
}));
2833
});
2934
}

examples/skip-prefix.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Skip Prefix
22

3+
There [used to be a `skipPrefix` plugin option](https://github.com/2createStudio/postcss-sprites/blob/b525125d9bae2911335cb27d0263558eee9f7723/README.md#skipprefix) that allowed you to skips the prefix in the name of output spritesheet (e.g., `sprite.customGroup.png => customGroup.png`). This allows you to do the same thing.
4+
35
###### Input
46

57
```css
@@ -8,7 +10,7 @@
810
.square { background: url(images/square.png) no-repeat 0 0; }
911
```
1012

11-
```javascript
13+
```js
1214
var path = require('path');
1315
var postcss = require('postcss');
1416
var sprites = require('postcss-sprites');
@@ -18,11 +20,11 @@ var opts = {
1820
hooks: {
1921
onSaveSpritesheet: function(opts, spritesheet) {
2022
// We assume that the groups is not an empty array
21-
var filenameChunks = spritesheet.groups.concat('png');
23+
var filenameChunks = spritesheet.groups.concat(spritesheet.extension);
2224
return path.join(opts.spritePath, filenameChunks.join('.'));
2325
}
2426
}
25-
}
27+
};
2628
```
2729

2830
----

examples/webpack-hot-load.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Webpack Hot Load
2+
3+
If you want to hot load image assets as they are introduced or edited, you can configure your postcss loader to add a hash to each sprite sheet as it is saved.
4+
5+
* [webpack loader context: `addDependency`](https://webpack.github.io/docs/loaders.html#adddependency)
6+
* [`rev-hash`](https://www.npmjs.com/package/rev-hash)
7+
8+
###### Input
9+
10+
```js
11+
var path = require('path');
12+
var postcss = require('postcss');
13+
var sprites = require('postcss-sprites');
14+
var revHash = require('rev-hash');
15+
16+
module.exports = function loadPostcssPlugins() {
17+
return [
18+
sprites({
19+
stylesheetPath: './css',
20+
spritePath: './css/images/',
21+
hooks: {
22+
onUpdateRule: function(rule, token, image) {
23+
// `this` is the webpack loader context
24+
this.addDependency(image.path); // adds a watch to the file
25+
},
26+
onSaveSpritesheet: function(opts, spritesheet) {
27+
return join(
28+
opts.spritePath,
29+
spritesheet.groups.concat([
30+
revHash(spritesheet.image),
31+
spritesheet.extension
32+
]).join('.')
33+
);
34+
}
35+
}
36+
})
37+
];
38+
};
39+
```

0 commit comments

Comments
 (0)