Skip to content

Commit

Permalink
Merge pull request #6 from onaliugo/0.3.0
Browse files Browse the repository at this point in the history
0.3.0 | Add cache-busting
  • Loading branch information
Ugo Onali committed Apr 1, 2015
2 parents 206c82a + 172bed1 commit fa6fef0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 16 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ var defaultOpts = {
* convert svg's sprite to .png
*/

png: false
png: false,

/* cacheBusting: boolean
* add a timestamp on each url matching the sprite path
*/

cacheBusting: true
};
```

Expand Down
4 changes: 4 additions & 0 deletions example/assets/template.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
height: <%= sizes.height %>px;
background: url('icons/sprite.svg') -<%= positions.x %>px 0;
}

.lt-ie9 .icon.icon--<%= name %> {
background: url('icons/sprite.png') -<%= positions.x %>px 0;
}
3 changes: 3 additions & 0 deletions lib/css/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return this.content.join('\n');
};
10 changes: 10 additions & 0 deletions lib/css/cache-busting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var Utils = require('uo-node-utils');

module.exports = function (str, opts, _opts) {
regex = new RegExp('(' + opts.files.dist + 'sprite)(.)(svg|png)', 'g');

str = str.replace(regex, function (url, $1, $2, $3, complete) {
return $1 + '-' + _opts.timestamp + $2 + $3;
});
return str;
};
13 changes: 13 additions & 0 deletions lib/css/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var _ = require('underscore');

module.exports = function (data, opts, _opts) {
var spritePath = opts.files.dist + 'sprite';
var newContent = _.template(this.template)(data);

if (opts.cacheBusting)
newContent = this.cacheBusting(newContent, opts, _opts);

this.content.push(newContent);

return newContent;
};
12 changes: 3 additions & 9 deletions lib/css/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var _ = require('underscore');
var Utils = require('uo-node-utils');

module.exports = function (template, canCompile) {
Expand All @@ -8,12 +7,7 @@ module.exports = function (template, canCompile) {
this.content = [];
this.template = Utils.read.file(template);

this.compile = function (data) {
var newContent = _.template(this.template)(data);
this.content.push(newContent);
};

this.build = function () {
return this.content.join('\n');
};
this.compile = require('./compile.js');
this.build = require('./build.js');
this.cacheBusting = require('./cache-busting.js');
};
16 changes: 11 additions & 5 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ module.exports = function () {
return;

var opts = this.opts;
var canCompileCss = this._opts.Css;
var _opts = this._opts;
var canCompileCss = _opts.Css;
var Css = new CSS(this.opts.css.template, canCompileCss);
var cacheBusting = opts.cacheBusting;
var timestamp = _opts.timestamp = new Date().getTime();

_opts.spritePath = opts.files.dist + 'sprite-' + (cacheBusting ? timestamp : '' );

SVG.export(opts.files.sketch, opts.assets);

Expand All @@ -38,24 +43,25 @@ module.exports = function () {
SVG.Wrapper.updateSizes(newX + attrs.sizes.width, attrs.sizes.height);

if (canCompileCss)
Css.compile(attrs);
Css.compile(attrs, opts, _opts);

Utils.create.file(path, content);
});

Utils.create.file(opts.files.dist + 'sprite.svg', SVG.Wrapper.build());
Utils.create.file(_opts.spritePath + '.svg', SVG.Wrapper.build());

Utils.log('✓ .svg Sprite generated');

if (canCompileCss) {
Utils.create.file(this.opts.css.dist, Css.build());
Utils.create.file(opts.css.dist, Css.build());
Utils.log('✓ Css template generated');
}

if (!opts.svg.keepSingle)
Utils.del(opts.assets);

if (opts.png)
PNG.converter(this.opts.files.dist + 'sprite.svg', function (err) {
PNG.converter(_opts.spritePath + '.svg', function (err) {
if (err) throw err;
return Utils.log('✓ .png sprite generated');
});
Expand Down
3 changes: 2 additions & 1 deletion lib/options/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = {
template: 'example/assets/template.css',
dist: 'dist/icons.css'
},
png: false
png: false,
cacheBusting: true
};

0 comments on commit fa6fef0

Please sign in to comment.