Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
borodean committed Dec 28, 2014
2 parents 742a0a1 + 16a70fb commit b5a4575
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 9 deletions.
42 changes: 35 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Usage

```js
gulp.task('assets', function () {
var postcss = require('postcss');
var postcss = require('gulp-postcss');
var assets = require('postcss-assets');

return gulp.src('source/*.css')
Expand Down Expand Up @@ -127,6 +127,33 @@ var options = {
};
```

Cachebuster
-----------

PostCSS Assets can bust the assets cache, changing urls depending on asset’s modification date:

```js
var options = {
cachebuster: true
};
```

```css
body {
background: url('/images/icons/baz.png?14a931c501f');
}
```

To define a custom cachebuster pass a function as an option:

```js
var options = {
cachebuster: function (path) {
return fs.statSync(path).mtime.getTime().toString(16);
}
};
```

Image dimensions
----------------

Expand Down Expand Up @@ -166,9 +193,10 @@ SVG files would be inlined unencoded, because [then they benefit in size](http:/
Full list of options
--------------------

| Option | Description | Default |
|:-----------------|:--------------------------------------------------------------------------------|:--------|
| `basePath` | Root directory of the project. | `.` |
| `baseUrl` | URL of the project when running the web server. | `/` |
| `loadPaths` | Specific directories to look for the files. | `[]` |
| `relativeTo` | Directory to relate to when resolving URLs. If `false`, disables relative URLs. | `false` |
| Option | Description | Default |
|:-----------------|:----------------------------------------------------------------------------------|:--------|
| `basePath` | Root directory of the project. | `.` |
| `baseUrl` | URL of the project when running the web server. | `/` |
| `cachebuster` | If cache should be busted. Pass a function to define custom busting strategy. | `false` |
| `loadPaths` | Specific directories to look for the files. | `[]` |
| `relativeTo` | Directory to relate to when resolving URLs. When `false`, disables relative URLs. | `false` |
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ module.exports = function (options) {
options.relativeTo = false;
}

if (options.cachebuster === true) {
options.cachebuster = function (path) {
var mtime = fs.statSync(path).mtime;
return mtime.getTime().toString(16);
};
}

function getImageSize(assetStr, density) {
var assetPath = resolvePath(assetStr.value);
var size;
try {
size = sizeOf(assetPath);
if (typeof density !== 'undefined') {
density = parseFloat(density.value, 10);
console.log(density);
size.width = +(size.width / density).toFixed(4);
size.height = +(size.height / density).toFixed(4);
}
Expand Down Expand Up @@ -100,6 +106,14 @@ module.exports = function (options) {
var baseToAsset = path.relative(options.basePath, matchPath(assetPath));
assetUrl.pathname = url.resolve(options.baseUrl, baseToAsset);
}
if (options.cachebuster) {
if (assetUrl.search) {
assetUrl.search = assetUrl.search + '&';
} else {
assetUrl.search = '?';
}
assetUrl.search += options.cachebuster(assetPath);
}
return cssesc(url.format(assetUrl));
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-assets",
"version": "1.0.0",
"version": "1.1.0",
"description": "PostCSS plugin to manage assets",
"author": "Vadim Borodean <[email protected]>",
"license": "MIT",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/cachebuster.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: url('test/fixtures/alpha/kateryna.jpg');
background: url('test/fixtures/alpha/kateryna.jpg?foo=bar');
}
4 changes: 4 additions & 0 deletions test/fixtures/cachebuster.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: url('/test/fixtures/alpha/kateryna.jpg?20');
background: url('/test/fixtures/alpha/kateryna.jpg?foo=bar&20');
}
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ function compareFixtures(t, name, msg, opts, postcssOpts) {
t.equal(actual, expected, msg);
}

function modifyFile(path) {
var atime = fs.statSync(path).atime;
var mtime = new Date();
fs.utimesSync(path, atime, mtime);
}

test('path resolving', function (t) {

compareFixtures(t, 'resolve', 'resolves paths');
Expand Down Expand Up @@ -80,3 +86,19 @@ test('dimensions', function (t) {
compareFixtures(t, 'dimensions', 'resolves dimensions', { basePath: 'test/fixtures/' });
t.end();
});

test('cachebuster', function (t) {
var options = { cachebuster: true };
var a = process('cachebuster', options);
modifyFile('test/fixtures/alpha/kateryna.jpg');
var b = process('cachebuster', options);
t.notEqual(a, b, 'busts cache');

compareFixtures(t, 'cachebuster', 'accepts buster function', {
cachebuster: function (path) {
return path.length.toString(16);
}
});

t.end();
});

0 comments on commit b5a4575

Please sign in to comment.