Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server aliases are now supported. #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
npm-debug.log
tmp
.DS_STORE
.idea
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ grunt.initConfig({
defaults. Extra details are below.
{
algorithm: 'md5', // Algoirthm used for hashing files
alias: '[]', // Look for a physical path instead of using the alias
baseDir: './', // The base directory for all assets
cdnPath: false, // You're own CDN path
deleteOriginals: false, // Delete the original file after hashing
Expand All @@ -105,6 +106,22 @@ Default value: `'md5'`

`algorithm` is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are `'sha1'`, `'md5'`, `'sha256'`, `'sha512'`

#### options.alias
Type: `Array`

Default value: `'[]'`

You can override this at the file level, e.g:

```
options: {
rename: false,
alias: [{
'path-to-search': 'new-path'
}],
},
```

#### options.baseDir
Type: `String`

Expand Down Expand Up @@ -303,6 +320,9 @@ Remote URLs for CSS, JavaScript, and images are ignored by cacheBust. This assum

### Change Log

**v0.5.0** - 2015-06-23
* Added new configuration to enable server aliases.

**v0.4.13** - 2015-02-27
* Fixes issue with deleting the original files when referenced in more than one source file.
* Fixed issue with hashe in the url of assets when referenced in CSS
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-cache-bust",
"description": "Bust static assets from the cache using content hashing",
"version": "0.4.13",
"version": "0.5.0",
"author": "Ben Holland <[email protected]>",
"repository": {
"type": "git",
Expand Down
9 changes: 6 additions & 3 deletions tasks/cachebust.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,18 @@ module.exports = function(grunt) {
filename = utils.removeHashInUrl(filename);
reference = utils.removeHashInUrl(reference);

if (!grunt.file.exists(filename)) {
grunt.log.warn('Static asset "' + filename + '" skipped because it wasn\'t found, original reference=' + reference);
var physicalFilename = utils.getPhysicalPath(filename, opts.alias);
var physicalReference = utils.getPhysicalPath(reference, opts.alias);

if (!grunt.file.exists(physicalFilename)) {
grunt.log.warn('Static asset "' + physicalFilename + '" skipped because it wasn\'t found, original reference=' + physicalReference);
return false;
}

// Cater for special `?#iefix` in font face declarations - this isn't pretty
reference = reference.replace('?#', '#');

newFilename = reference.split('?')[0] + '?' + utils.generateFileHash(grunt.file.read(filename));
newFilename = reference.split('?')[0] + '?' + utils.generateFileHash(grunt.file.read(physicalFilename));
newReference = newFilename;
markup = markup.replace(new RegExp(utils.regexEscape(reference), 'g'), newFilename);
}
Expand Down
37 changes: 37 additions & 0 deletions tasks/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ module.exports = function(opts) {
return str.replace(findHash, function(match, hash, extension) {
return extension;
});
},

/**
* The current filename can be obtained using some server aliases.
* If a filename part matches one of them, then the function returns the physical filename replacing all aliases
* with the real path.
*
* @param {string} filename
* @param {array} alias It should match following structure:
* [
* {
* 'path-to-search': 'new-path'
* },
* {
* 'other-path-to-search': 'other-path'
* },...
* ]
* @return {string} physicalFilename
*/
getPhysicalPath: function(filename, alias) {
if (typeof filename !== "string") {
throw 'Filename MUST be a string';
}

var physicalFilename = filename;

if (alias && typeof alias === "object") {
alias.forEach(function(alias) {
for (var key in alias) {
if (filename.indexOf(key) > -1) {
physicalFilename = physicalFilename.replace(key, alias[key]);
}
}
});
}

return physicalFilename;
}

};
Expand Down
23 changes: 22 additions & 1 deletion tests/utils/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ module.exports = {
test.equal(utils.removeHashInUrl('/assets/scripts.js#4567#678'), '/assets/scripts.js');

test.done();
}
},

getPhysicalPath: function(test) {
test.expect(6);

test.throws(function() {
utils.getPhysicalPath();
});
test.throws(function() {
utils.getPhysicalPath(123);
});
test.doesNotThrow(function() {
utils.getPhysicalPath('/assets/style.css', true);
});
test.doesNotThrow(function() {
utils.getPhysicalPath('/assets/style.css');
});
test.equal(utils.getPhysicalPath('/assets/styles.css', true), '/assets/styles.css');
test.equal(utils.getPhysicalPath('/assets/styles.css', [{ 'assets': 'test' }]), '/test/styles.css');

test.done();
}

};