Skip to content

Latest commit

 

History

History
39 lines (34 loc) · 1.03 KB

webpack-hot-load.md

File metadata and controls

39 lines (34 loc) · 1.03 KB

Webpack Hot Load

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.

Input
var path = require('path');
var postcss = require('postcss');
var sprites = require('postcss-sprites');
var revHash = require('rev-hash');

module.exports = function loadPostcssPlugins() {
	return [
		sprites({
			stylesheetPath: './css',
			spritePath: './css/images/',
			hooks: {
				onUpdateRule: function(rule, token, image) {
					// `this` is the webpack loader context
					this.addDependency(image.path); // adds a watch to the file
				},
				onSaveSpritesheet: function(opts, spritesheet) {
					return join(
						opts.spritePath,
						spritesheet.groups.concat([
							revHash(spritesheet.image),
							spritesheet.extension
						]).join('.')
					);
				}
			}
		})
	];
};