diff --git a/README.md b/README.md index 8a3068a..08126da 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ The `src` part of the configuration you should have seen before as it's used by #### Summary ``` -// Here is a short summary of the options and some of their +// Here is a short summary of the options and some of their defaults. Extra details are below. { algorithm: 'md5', // Algorithm used for hashing files @@ -83,7 +83,9 @@ defaults. Extra details are below. jsonOutputFilename: 'grunt-cache-bust.json', // The file path and name of the exported JSON. Is relative to baseDir length: 16, // The length of the hash value separator: '.', // The separator between the original file name and hash - queryString: false // Use a query string for cache busting instead of rewriting files + queryString: false, // Use a query string for cache busting instead of rewriting files + nestedMappings: false, // Create Nested Mapping for the same file + nestedMappingsPattern: [] // Array of Objects with pattern and splitAt values } ``` @@ -179,6 +181,55 @@ Default value: `false` Use a query string for cache busting instead of rewriting files. +#### options.nestedMappings +Type: `Boolean` +Default value: `false` + +When set as `true`, it will create nested Mapping for the same filename as per the given pattern in `nestedMappingsPattern` + +#### options.nestedMappingsPattern +Type: `Array` +Default value: `[]` + +Add Array of Objects with `pattern` and `splitAt` as keys. + +Example +```js +cacheBust: { + generate: { + options: { + assets: ['**'], + baseDir: 'build/', + jsonOutputFilename:'staticFilesCacheMapping.json', + jsonOutput: true, + nestedMappings: true, + nestedMappingsPattern: [ + { + pattern: 'assets/images/', + splitAt: 'assets/' + }, + { + pattern: 'assets/fonts/', + splitAt: 'assets/' + }, + ] + }, + src: ['build/assets/**/*.css'] + } +} +``` + +It will create the staticFilesCacheMapping.json like : + +``` +{ + "assets/fonts/myCustomFont/myFont_semibold-webfont.woff2":"assets/fonts/myCustomFont/myFont_semibold-webfont.62772b8a9895121d.woff2", + "fonts/myCustomFont/myFont_semibold-webfont.woff":"fonts/myCustomFont/myFont_semibold-webfont.d842eed1cd97157c.woff", + "assets/images/homepage/uncle.png":"assets/images/homepage/uncle.1cce4a56e28c269b.png", + "images/homepage/trust-seal.png":"images/homepage/trust-seal.6ffe0c838d82331e.png" +} +``` + ### Usage Examples #### The most basic setup diff --git a/package.json b/package.json index 0d68d4d..54f59d4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-cache-bust", "description": "Bust static assets from the cache using content hashing", - "version": "1.3.0", + "version": "1.4.0", "author": "Ben Holland ", "repository": { "type": "git", diff --git a/tasks/cachebust.js b/tasks/cachebust.js index c4bf30c..b96c505 100644 --- a/tasks/cachebust.js +++ b/tasks/cachebust.js @@ -17,7 +17,9 @@ var DEFAULT_OPTIONS = { jsonOutputFilename: 'grunt-cache-bust.json', length: 16, separator: '.', - queryString: false + queryString: false, + nestedMappings: false, + nestedMappingsPattern: [] }; module.exports = function() { @@ -73,6 +75,18 @@ module.exports = function() { } } + if (opts.nestedMappings) { + opts.nestedMappingsPattern.map(function(mapping) { + if (file.match(mapping.pattern)) { + var splitAt = mapping.splitAt; + var nestedFile = file.split(splitAt)[1]; + var newNestedFilename = newFilename.split(splitAt)[1]; + + obj[nestedFile] = newNestedFilename; + } + }); + } + obj[file] = newFilename; return obj;