Skip to content

jansanchez/css-url-versioner

Repository files navigation

CSS Url Versioner NPM version Build Status Coveralls Status Code Climate Status

A node package for css url versioner

Getting Started

Install:

npm install --save-dev css-url-versioner

How to use:

var cssVersioner = require('css-url-versioner');

fs.readFile('test/css/test.css', 'utf8', function (err, data) {
    var newCss = cssVersioner({
        content: data
    });
    console.log(newCss.output);
});

or

var cssVersioner = require('css-url-versioner');
var data = '.some_selector{background-image: url("img/sprite.png");}';

var cssVersioned = cssVersioner({
    content: data
});

console.log(cssVersioned.output);

//logs: '.some_selector{background-image: url("img/sprite.png?v=2014111");}';

Options:

content

Required: true

Here go the css content we want versioning

{content: '.some_selector{background-image: url("img/sprite.png");}'}

variable:

Default: v

Here go the variable of our version, for '?myVariable=yyyymmdd'

{variable: 'myVariable'}

version:

Default: yyyymmdd

Here you go a custom version if you so desire, but if we do not put this key, then the default version will be the current date.

{version: '0.0.2'}

lastcommit:

Default: false

If we want that version to be our short version of last commit in git, configured 'lastcommit' to true.

{lastcommit: true}

Examples

lastcommit: true

var cssVersioner = require('css-url-versioner');
var data = '.some_selector{background-image: url("img/sprite.png");}';

var cssVersioned = cssVersioner({
    content: data,
    lastcommit: true
});

console.log(cssVersioned.output);

//logs: '.some_selector{background-image: url("img/sprite.png?v=dc31e29");}';

lastcommit: true and variable: 'myVersion'

var cssVersioner = require('css-url-versioner');
var data = '@font-face{ 
    			font-family: 'gotham'; 
				src: url("fonts/gotham.svg#gotham") format('svg');
			}';

var cssVersioned = cssVersioner({
    content: data,
    variable: 'myVersion'
    lastcommit: true
});

console.log(cssVersioned.output);

//logs: '@font-face{ font-family: 'gotham'; src: url("fonts/gotham.svg?myVersion=dc31e29#gotham") format('svg'); }';

version: Math.random()

var cssVersioner = require('css-url-versioner');
var data = '.some_selector{background-image: url("img/sprite.png");}';

var cssVersioned = cssVersioner({
    content: data,
    version: Math.random()
});

console.log(cssVersioned.output);

//logs: '.some_selector{background-image: url("img/sprite.png?v=0.82140917");}';