Skip to content

Commit

Permalink
Merge pull request #1 from ahoek/inline-images
Browse files Browse the repository at this point in the history
Add inline images task
  • Loading branch information
gisostallenberg committed Feb 26, 2016
2 parents 8f5f359 + b829621 commit 0e74a19
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,26 @@ Add to bower.json
"gulp-tasks": "https://github.com/ConnectHolland/gulp-tasks.git#^2.0"
}
}
```
```

## Inline images task

With the task inline-images you can create a 'sprite' of inline css images. The task
generates an scss file with the inline-image($name) mixin and .inline-image-* css classes.

Make sure the following packages are available:
```json
"gulp-add-src": "^0.2.0",
"gulp-concat": "^2.5.2",
"gulp-image-data-uri": "^1.2.1",
```

Example configuration of config.json:
```json
"inline-images": {
"src": [
"images/icons2x/*"
]
}
```

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-tasks",
"version": "2.1.2",
"version": "2.2.0",
"homepage": "https://github.com/ConnectHolland/gulp-tasks",
"authors": [
"Arjen Smit <[email protected]>",
Expand Down
26 changes: 26 additions & 0 deletions tasks/inline-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Generate inline scss images task
*/
'use strict';

module.exports = function(gulp, config, plugins) {
var imageDataURI = require('gulp-image-data-uri');
var concat = require('gulp-concat');
var addsrc = require('gulp-add-src');

return function() {
return gulp.src(config['inline-images'].src)
// Create the data uris of the images
.pipe(imageDataURI({
template: {
file: __dirname + '/inline-images/image-data-uri-template.scss'
}
}))
// Prepend and append scss to make the mixins
.pipe(addsrc.prepend(__dirname + '/inline-images/_inline-images-prepend.scss'))
.pipe(addsrc.append(__dirname + '/inline-images/_inline-images-append.scss'))
// Output to generated scss file
.pipe(concat('_inline-images.scss'))
.pipe(gulp.dest('./style/generated'));
};
};
15 changes: 15 additions & 0 deletions tasks/inline-images/_inline-images-append.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Inline image mixin
@mixin inline-image($name) {
$image-uri: map-get($inline-images, $name);
background-image: url('#{$image-uri}');
background-size: 100%;

@content;
}

// Create css classes
@each $name, $url in $inline-images {
.inline-image-#{$name} {
@include inline-image('#{$name}');
}
}
6 changes: 6 additions & 0 deletions tasks/inline-images/_inline-images-prepend.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Initialize the inline images map
@function map-set($map, $key, $value) {
$new: ($key: $value);
@return map-merge($map, $new);
}
$inline-images: ();
1 change: 1 addition & 0 deletions tasks/inline-images/image-data-uri-template.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$inline-images: map-set($inline-images, '{{className}}', '{{dataURISchema}}');

0 comments on commit 0e74a19

Please sign in to comment.