Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

postcss-sprites

2createstudio16.1kMIT4.2.1TypeScript support: included

Generate spritesheets from stylesheets

postcss-plugin, postcss, css, spritesheet, spritesmith, sprite, png, svg

readme

postcss-sprites Build Status npm version

PostCSS plugin that generates spritesheets from your stylesheets.

/* Input */
.comment { background: url(images/sprite/ico-comment.png) no-repeat 0 0; }
.bubble { background: url(images/sprite/ico-bubble.png) no-repeat 0 0; }

/* ---------------- */

/* Output */
.comment { background-image: url(images/sprite.png); background-position: 0 0; }
.bubble { background-image: url(images/sprite.png); background-position: 0 -50px; }

Usage

var fs = require('fs');
var postcss = require('postcss');
var sprites = require('postcss-sprites');

var css = fs.readFileSync('./css/style.css', 'utf8');
var opts = {
    stylesheetPath: './dist',
    spritePath: './dist/images/'
};

postcss([sprites(opts)])
    .process(css, {
        from: './css/style.css',
        to: './dist/style.css'
    })
    .then(function(result) {
        fs.writeFileSync('./dist/style.css', result.css);
    });

See PostCSS docs for examples for your environment.


Options

stylesheetPath

Relative path to the folder that will keep your output stylesheet(s). If it's null the path of CSS file will be used.

  • Default: null
  • Required: false
spritePath

Relative path to the folder that will keep your output spritesheet(s).

  • Default: ./
  • Required: true
basePath

Your base path that will be used for images with absolute CSS urls.

  • Default: ./
  • Required: false
relativeTo

Indicates whether the url should be relative against current CSS context or original CSS stylesheet file.

  • Default: file
  • Required: false
  • Options: file|rule
filterBy

Defines filter functions that will manipulate the list of images founded in your stylesheet(s).

  • Default: []
  • Required: false
  • Options: Function|Function[]

Every function must return a Promise which should be resolved or rejected.

Built-in filters:

  • based on fs.stat and used to remove non exisiting images
groupBy

Defines group functions that will manipulate the list of images founded in your stylesheet(s).

  • Default: []
  • Required: false
  • Options: Function|Function[]

Every function must return a Promise which should be resolved with a string or rejected.

Built-in filters:

  • based on @2x naming convention
retina

Defines whether or not to search for retina mark in the filename.

  • Default: false
  • Required: false
hooks

Defines whether or not to search for retina mark in the filename.

  • Default: {}
  • Required: false
hooks.onSaveSpritesheet

Hook that allows to rewrite the data of produced spritesheet.

If returned value is string, it is used as filepath value, and if returned value is object, it is used as value which will be merged with current spritesheet data.

Returned value can also be Promise which should return either string or object.

  • Default: null
  • Required: false
hooks.onUpdateRule

Hook that allows to rewrite the CSS output for an image.

  • Default: null
  • Required: false
spritesmith

A spritesmith configuration.

  • Default: {}
  • Required: false
spritesmith.engine

The engine.

  • Default: pixelsmith
  • Required: false
spritesmith.algorithm

The algorithm.

  • Default: binary-tree
  • Required: false
spritesmith.padding

The space between images in spritesheet.

  • Default: 0
  • Required: false
spritesmith.engineOpts

The configuration of the engine.

  • Default: {}
  • Required: false
spritesmith.exportOpts

The export options of the engine.

  • Default: {}
  • Required: false
svgsprite

A svg-sprite configuration.

verbose

Prints the plugin output to the console.

  • Default: false
  • Required: false

The Image

Every filter or group function will be called with an Image object.

styleFilePath

An absolute path to the stylesheet - /Path/to/your/source/stylesheet.css

path

An absolute path to the image - /Path/to/your/image.png

originalUrl

The url found in your stylesheet including the query params - ../image.png?v1

url

A normalized version of the original url - ../image.png

ratio

The retina ratio of your image - 2

retina

Indicates whenever your image is retina - true

groups

The groups associated with the image - ['shapes', '@2x']

token

The string used as reference in your stylesheet - /* @replace|image.png */

coords

The position & dimensions of image in generated spritesheet - { width: 20, height: 20, x: 0, y: 0 }

spritePath

A relative path to the generated spritesheet - dist/sprite.png

spriteUrl

A CSS url to the generated spritesheet - sprite.png

spriteWidth

The total width of the spritesheet - 200

spriteHeight

The total height of the spritesheet - 400


Advanced


Contributing

Pull requests are welcome.

$ git clone git@github.com:2createStudio/postcss-sprites.git
$ npm install
$ npm run watch

changelog

Change Log

[4.2.1] - 2017-02-07

Fixed

  • Preserve source of original declarations - See #90

[4.2.0] - 2017-02-07

Added

Fixed

  • Typos in examples
  • Rejected promises without error
  • Incorrect extraction of the urls

[4.1.1] - 2016-12-7

Changed

  • Apply background-size to all generated sprites
  • Use a hash instead of real path for id attribute in SVG sprites

[4.1.0] - 2016-11-22

Added

  • Better support for modular stylesheets
  • originalUrl property to Image object
  • Support for Promises in onSaveSpritesheet hook

[4.0.0] - 2016-08-26

Added

  • Support for SVG files

Fixed

  • Support for CommonJS
  • Broken options when the plugin is used in process with a watch flag(e.g postcss-cli)

Changed

  • Params passed to onSaveSpritesheet hook

[3.3.0] - 2016-07-26

Added

  • The filepath of stylesheet to Image object

[3.2.3] - 2016-07-22

Fixed

  • Usage of Result.warn

[3.2.2] - 2016-07-22

Fixed

  • Warnings in built-in filter

[3.2.1] - 2016-07-21

Changed

  • Use console.log instead of Result.warn to print the success message

Fixed

  • Broken tests under Node 6

[3.2.0] - 2016-07-18

Changed

  • Use Result.warn API instead of console.warn
  • Publish the original source to npm

[3.1.2] - 2016-04-11

Fixed

  • Typescript support

[3.1.1] - 2016-03-23

Fixed

  • NaN paddings in retina sprites

[3.1.0] - 2016-02-04

Added

  • Tests for examples
  • Info logs

Changed

  • 'Usage' example

Fixed

  • Typos in examples

[3.0.3] - 2016-01-20

Fixed

  • Fix "Fix: Broken CommonJS export"

Changed

  • Add note in README how to require properly the module

[3.0.2] - 2016-01-20

Fixed

  • Broken CommonJS export

[3.0.1] - 2016-01-19

Fixed

  • Typos in README
  • Typos in plugin output

Removed

  • Unused code for responsive sprites

[3.0.0] - 2016-01-18

Added

  • basePath option
  • relativeTo option
  • hooks option

Changed

  • Move spritesmith options in their own object called spritesmith

Removed

[2.0.3] - 2016-01-06

Changed

  • Revert styleFilePath behaviour

Fixed

  • Scan regex

[2.0.2] - 2015-10-30

Changed

  • Make styleFilePath relative to specific file

[2.0.1] - 2015-09-23

Changed

  • Use Node.remove instead of Node.removeSelf

[2.0.0] - 2015-08-28

Added

  • Support for PostCSS@5

[1.0.9] - 2015-07-20

Added

  • outputDimensions option
  • skipPrefix option

[1.0.8] - 2015-07-08

Fixed

  • Bug with paths in CSS output

[1.0.7] - 2015-06-29

Fixed

  • Bug with multiple background-size properties

[1.0.6] - 2015-06-09

Added

  • stylesheetPath option

Changed

  • spritePath option

Removed

  • externalStylesheet option
  • baseUrl option
  • spriteName option

[1.0.5] - 2015-06-08

Added

  • Support for JPEG/JPG sprites

Fixed

  • Typos in README

[1.0.4] - 2015-06-05

Fixed

  • Bug with background declarations inside AtRule nodes

[1.0.3] - 2015-06-01

Added

  • Support for rgb & rgba colors

Changed

  • Date format in CHANGELOG

[1.0.2] - 2015-06-01

Added

  • Tests
  • Travis CI integration
  • baseUrl option

Fixed

  • Bug with unused tokens
  • Bug with groupBy & filterBy functions with gulp
  • CSS properties order in output stylesheet

[1.0.1] - 2015-05-28

Fixed

  • verbose option
  • Confusing parts in README
  • Bug with spriteRef

[1.0.0] - 2015-05-27

Added

  • Initial version