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

generate hash from file to bust caching #48

Open
wants to merge 2 commits 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
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Vinyl = require('vinyl');
var fancyLog = require('fancy-log');
var merge = require('merge');
var applySourcemap = require('vinyl-sourcemaps-apply');
var crypto = require('crypto');

var Concat = require('concat-with-sourcemaps');

Expand Down Expand Up @@ -111,7 +112,12 @@ module.exports = function(options){
};

var addImports = function(aFile, fileNamesOfPartsToImport){
var parameters = options.cacheBuster ? '?z=' + Math.round((Math.random() * 999)) : '';
var generated_hash = crypto
.createHash('sha1')
.update(aFile.contents, 'binary')
.digest('hex');

var parameters = options.cacheBuster ? '?z=' + generated_hash : '';
var filePath = path.relative(aFile.base, aFile.path);
var concat = new Concat(shouldCreateSourcemaps, filePath, '\n');
for (var i = 0; i < fileNamesOfPartsToImport.length; i++) {
Expand Down
12 changes: 6 additions & 6 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('gulp-bless', function() {
var expectedSplitFile = expectedSplits[index];
newFile.relative.should.equal(path.basename(expectedSplitFile.path));

var contents = newFile.contents.toString('utf8').replace(/\?z=[0-9]+'\)/g, "?z=xxx')");
var contents = newFile.contents.toString('utf8').replace(/\?z=[0-9a-f]+'\)/g, "?z=xxx')");

contents.should.equal(expectedSplitFile.contents.toString('utf8'));
Buffer.isBuffer(newFile.contents).should.equal(true);
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('gulp-bless', function() {
should.exist(masterPart);
should.exist(subPart);
path.basename(subPart.path).should.equal("long-split" + suffix + "1.css");
var importRegex = "@import url\\('long-split" + suffix + "1.css\\?z=[0-9]+'\\);";
var importRegex = "@import url\\('long-split" + suffix + "1.css\\?z=[0-9a-f]+'\\);";
masterPart.contents.toString("utf8").should.match(new RegExp(importRegex));
done();
});
Expand Down Expand Up @@ -231,7 +231,7 @@ describe('gulp-bless', function() {
should.exist(masterPart);
should.exist(subPart);
path.basename(subPart.path).should.equal("long-split" + suffix + "-1.css");
var importRegex = "@import url\\('long-split" + suffix + "-1.css\\?z=[0-9]+'\\);";
var importRegex = "@import url\\('long-split" + suffix + "-1.css\\?z=[0-9a-f]+'\\);";
masterPart.contents.toString("utf8").should.match(new RegExp(importRegex));
done();
});
Expand Down Expand Up @@ -276,7 +276,7 @@ describe('gulp-bless', function() {
.pipe(assert.first(function(result) {
path.relative('./', result.path).should.equal(path.relative('/home', expectedSplits[0].path));
result.contents.toString('utf8')
.replace(/\?z=[0-9]+'\)/g, "?z=xxx')")
.replace(/\?z=[0-9a-f]+'\)/g, "?z=xxx')")
.should.equal(expectedSplits[0].contents.toString('utf8'));

result.sourceMap.sources.should.have.length(1);
Expand Down Expand Up @@ -399,7 +399,7 @@ describe('gulp-bless', function() {
.pipe(assert.nth(1, function(result) {
path.relative('./', result.path).should.equal(path.relative('/home', expectedSplits[0].path));
result.contents.toString('utf8')
.replace(/\?z=[0-9]+\)/g, "?z=xxx)")
.replace(/\?z=[0-9a-f]+\)/g, "?z=xxx)")
.should.equal(expectedSplits[0].contents.toString('utf8'), "not equal to content of file ./test/css/long-split-with-sourcemap-comment.css");

result.sourceMap.sources.should.have.length(1);
Expand Down Expand Up @@ -564,7 +564,7 @@ describe('gulp-bless', function() {
var expectedSplitFile = expectedSplits[index];
newFile.relative.should.equal(path.basename(expectedSplitFile.path));

var contents = newFile.contents.toString('utf8').replace(/\?z=[0-9]+'\)/g, "?z=xxx')");
var contents = newFile.contents.toString('utf8').replace(/\?z=[0-9a-f]+'\)/g, "?z=xxx')");

contents.should.equal(expectedSplitFile.contents.toString('utf8'));
Buffer.isBuffer(newFile.contents).should.equal(true);
Expand Down