diff --git a/README.md b/README.md index 6420cdb..47035da 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 + +``` \ No newline at end of file diff --git a/index.js b/index.js index 97d2964..cfb6a8d 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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 = {}; @@ -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. @@ -59,7 +71,7 @@ var cacheBust = function(options) { method:'hash', length: 8, baseDir: "public", - file: "cachbuster.json" + file: "cachebuster.json" }, options || {}); @@ -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; @@ -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"); -}); \ No newline at end of file