From 3575aea4ee1977451a749826760228135f44e9e1 Mon Sep 17 00:00:00 2001 From: jonstavis Date: Wed, 17 Jun 2015 16:43:30 -0400 Subject: [PATCH 1/3] Add helper function to README, fix typo, remove absolute path from manifest. --- README.md | 36 +++++++++++++++++++++++++++++++++++- index.js | 8 ++++---- 2 files changed, 39 insertions(+), 5 deletions(-) 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..97e9761 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,7 @@ var cacheBust = function(options) { method:'hash', length: 8, baseDir: "public", - file: "cachbuster.json" + file: "cachebuster.json" }, options || {}); @@ -117,7 +117,7 @@ var cacheBust = function(options) { } - output["/" + file.relative] = file_mtime[file.path]['hash']; + output[file.relative] = file_mtime[file.path]['hash']; firstFile = firstFile || file; @@ -149,7 +149,7 @@ elixir.extend('cachebust',function(src, options){ method:'hash', length: 8, baseDir: "public/", - file: "cachbuster.json" + file: "cachebuster.json" }, options || {}); @@ -165,4 +165,4 @@ elixir.extend('cachebust',function(src, options){ this.registerWatcher("cache-busting",src); return this.queueTask("cache-busting"); -}); \ No newline at end of file +}); From 93b6e83aada8c3ae4cf2cbffdddd5972f8210ce1 Mon Sep 17 00:00:00 2001 From: joe Date: Tue, 23 Jun 2015 22:30:19 -0400 Subject: [PATCH 2/3] fixed relative paths for windows installations --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 97e9761..82ecece 100644 --- a/index.js +++ b/index.js @@ -117,7 +117,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; From ea7cf5ff1c52fe33d5994534bd35adb7b5a6a990 Mon Sep 17 00:00:00 2001 From: jonstavis Date: Fri, 4 Sep 2015 11:36:22 -0400 Subject: [PATCH 3/3] Updates to support elixir 3 --- index.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 82ecece..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. @@ -154,15 +166,13 @@ elixir.extend('cachebust',function(src, options){ 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"); }); +