Skip to content

Commit

Permalink
cacheKey for AssetsSource
Browse files Browse the repository at this point in the history
  • Loading branch information
mikrostew committed Mar 14, 2018
1 parent a729aca commit 55b548c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/assets/AssetsSource.js
Expand Up @@ -82,4 +82,32 @@ AssetsSource.prototype.toString = function() {
return this.srcPath + "/" + this.pattern;
};

/**
* Build a string suitable for caching this asset
* @returns {String} the cache key
*/
AssetsSource.prototype.cacheKey = function(namespace) {
function skipSomeKeys(key, value) {
// these are set to this.srcPath, which is already included in the cacheKey
if (key === "cwd" || key === "root") {
return undefined;
}
// these are added inside glob and always set to true, which happens after this string
// is created when glob.sync() is run in #getAssets()
// (see #setopts() in glob/common.js)
if (key === "nonegate" || key === "nocomment") {
return undefined;
}
return value;
}

return "[" +
"httpPrefix=" + (this.httpPrefix ? this.httpPrefix : "") +
";name=" + (this.name ? this.name : (namespace ? namespace : "")) +
";srcPath=" + this.srcPath +
";pattern=" + this.pattern +
";opts=" + JSON.stringify(this.globOpts, skipSomeKeys) +
"]";
};

module.exports = AssetsSource;
64 changes: 64 additions & 0 deletions test/test_assets.js
Expand Up @@ -983,6 +983,70 @@ describe("assets", function () {
});
});
});

describe("cache key", function() {
var rootDir = testutils.fixtureDirectory("app_assets");
var rootDir2 = testutils.fixtureDirectory("app_assets_odd_names");

it("generates different cacheKey for different httpPrefix", function(done) {
var source1 = new AssetsSource(rootDir, {
httpPrefix: "foo",
});
var source2 = new AssetsSource(rootDir, {
httpPrefix: "bar",
});
assert.notEqual(source1.cacheKey(), source2.cacheKey());
done();
});

it("generates different cacheKey for different name", function(done) {
var source1 = new AssetsSource(rootDir, {
name: "foo",
});
var source2 = new AssetsSource(rootDir, {
name: "bar",
});
assert.notEqual(source1.cacheKey(), source2.cacheKey());
done();
});

it("generates different cacheKey for different namespace", function(done) {
var source1 = new AssetsSource(rootDir, {
});
assert.notEqual(source1.cacheKey("foo"), source1.cacheKey("bar"));
done();
});

it("generates different cacheKey for different srcPath", function(done) {
var source1 = new AssetsSource(rootDir, {});
var source2 = new AssetsSource(rootDir2, {});
assert.notEqual(source1.cacheKey(), source2.cacheKey());
done();
});

it("generates different cacheKey for different pattern", function(done) {
var source1 = new AssetsSource(rootDir, {
pattern: "images/**/*",
});
var source2 = new AssetsSource(rootDir, {
pattern: "images/**/*.jpg",
});
assert.notEqual(source1.cacheKey(), source2.cacheKey());
done();
});

it("generates different cacheKey for different globOpts", function(done) {
var source1 = new AssetsSource(rootDir, {
});
var source2 = new AssetsSource(rootDir, {
globOpts: {
dot: true
}
});
assert.notEqual(source1.cacheKey(), source2.cacheKey());
done();
});
});
});
});
});

0 comments on commit 55b548c

Please sign in to comment.