Code Monkey home page Code Monkey logo

postcss-sprites's Introduction

PostCSS Sprites Build Status npm version

PostCSS plugin that generate sprites from your stylesheets and then updates image references.

Install

npm install postcss-sprites

Example

var postcss = require('postcss');
var sprites = require('postcss-sprites');
var opts    = {
	stylesheetPath: './dist',
	spritePath    : './dist/images/sprite.png',
	retina        : true
};

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

Input

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

.arrows { background: url(images/sprite/[email protected]) no-repeat 0 0; }
.logo { background: url(images/sprite/[email protected]) no-repeat 0 0; }

Output

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

.arrows { background-image: url(images/[email protected]); background-position: 0 0; background-size: 100px 100px; }
.logo { background-image: url(images/[email protected]); background-position: 0 -50px; background-size: 100px 100px; }

Options (plugin)

stylesheetPath

Type: String Default: ./ Example: ./dist/css Required: true

Defines relative path to output stylesheet. This is used to generate correct relative path to spritesheet for CSS rules.

spritePath

Type: String Default: ./sprite.png Example: ./dist/images/sprite.png Required: true

Can define relative path to ouput sprite.

filterBy

Type: Function[], Function Default: [] Example: filterBy Required: false

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.

Built in filters:

  • based on fs.exists, which is used to remove non existing images.

groupBy

Type: Function[], Function Default: [] Example: groupBy Required: false

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.

Built in groupers:

  • based on @2x image naming syntax, will produce sprite.@{number}x.png naming. (@{number}x image group).

retina

Type: Boolean Default: false Example: true Required: false

Defines whether or not to search for retina mark in the filename. If true then it will look for @{number}x syntax. For example: [email protected].

verbose

Type: Boolean Default: false Example: true Required: false

Suppress console.log messages.

Options (spritesmith)

All options for spritesmith are optional. For more detailed information you can visit the official page of spritesmith.

engine

Type: String Default: pixelsmith

algorithm

Type: String Default: binary-tree

padding

Type: Number Default: 0

engineOpts

Type: Object Default: {}

exportOpts

Type: Object Default: {}

The Image

Every filter or grouper is called with an image object, which have following properties:

path

Type: String Example: /Users/john/project/image.png

Resolved path to the image.

url

Type: String Example: images/image.png

Url for image found in the stylesheet.

ratio

Type: String Default: 1

Ratio of the retina image - e.g. @2x => 2

retina

Type: Boolean Default: false

The flag that indicates a retina image.

groups

Type: Array Default: []

The groups associated with the image after grouping functions.

token

Type: Object

The string used to update image reference in the stylesheet. This token is PostCSS Comment.

Advanced Example

var postcss = require('postcss');
var sprites = require('postcss-sprites');
var Q       = require('q');
var opts    = {
	stylesheetPath: './dist',
	spritePath    : './dist/images/sprite.png',
	verbose       : true,
	filterBy      : function(image) {
		return /\.jpg$/gi.test(image.url);
	},
	groupBy       : function(image) {
		return Q.Promise(function(resolve) {
			// Do something here...

			resolve('groupName');
		});
	}
};

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

Contributing

Pull requests are welcome.

License

MIT © 2createStudio

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.