Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The second parameter of .cachebust() allows you to set these options:
- "mtime" - The time that the file was last modified, according to the filesystem.
- **length**: The *maximum* length that a cache-busting string should be. (default is 8)
- **baseDir**: The path (relative to laravel's root dir) where the generated json file should be. (default is "public")
- **file**: The filename of the json file. (default is "cachbuster.json")
- **file**: The filename of the json file. (default is "cachebuster.json")

### Example of gulpfile.js - With options ###
```Javascript
Expand All @@ -55,3 +55,37 @@ elixir(function(mix) {
);
});
```

### Using the versioned assets
You may define a modified version of the `elixir()` helper function to make use of the versioned assets in your views:
```php
if ( ! function_exists('elixir_cachebust'))
{
/**
* Get the path to a versioned Elixir Cachebust file.
*
* @param string $file
* @return string
*/
function elixir_cachebust($file)
{
static $manifest = null;

if (is_null($manifest))
{
$manifest = json_decode(file_get_contents(public_path().'/cachebuster.json'), true);
}

if (isset($manifest[$file]))
{
return asset($file) . '?' . $manifest[$file];
}

throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
}
}
```

```html
<link href="{{ elixir_cachebust('css/app.css') }}" rel="stylesheet">
```
30 changes: 20 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Requirements:
var elixir = require('laravel-elixir');
var utilities = require('laravel-elixir/ingredients/commands/Utilities');
var gulp = require('gulp');
var crypto = require('crypto');
var path = require('path');
Expand All @@ -9,6 +8,9 @@ var gutil = require('gulp-util');
var del = require('del');
var objectAssign = require('object-assign');


var Task = elixir.Task;

// Variable to remember file mtime and hash:
var file_mtime = {};

Expand Down Expand Up @@ -47,6 +49,16 @@ function generateHash(str, length)
return hash || uuid(length);
}

function prefixDirToFiles(dir, files) {
if ( ! Array.isArray(files)) files = [files];

return files.map(function(file) {
file = file.replace(new RegExp('^' + dir), '');

return [dir, file].join('/').replace('//', '/');
});
}

/**
* Cycles through each file and if the file has been modified,
* a new cache busting string is generated and output to a json file.
Expand All @@ -59,7 +71,7 @@ var cacheBust = function(options) {
method:'hash',
length: 8,
baseDir: "public",
file: "cachbuster.json"
file: "cachebuster.json"
},
options || {});

Expand Down Expand Up @@ -117,7 +129,7 @@ var cacheBust = function(options) {

}

output["/" + file.relative] = file_mtime[file.path]['hash'];
output[file.relative.replace(/\\/g, '/')] = file_mtime[file.path]['hash'];

firstFile = firstFile || file;

Expand Down Expand Up @@ -149,20 +161,18 @@ elixir.extend('cachebust',function(src, options){
method:'hash',
length: 8,
baseDir: "public/",
file: "cachbuster.json"
file: "cachebuster.json"
},
options || {});


src = utilities.prefixDirToFiles(options.baseDir, src);
src = prefixDirToFiles(options.baseDir, src);

gulp.task("cache-busting", function() {
new Task("cachebust", function() {
return gulp.src( src, {base: './public'} )
.pipe(cacheBust(options))
.pipe(gulp.dest(options.baseDir));
});
}).watch(src);

this.registerWatcher("cache-busting",src);
});

return this.queueTask("cache-busting");
});