-
Notifications
You must be signed in to change notification settings - Fork 60
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 cache keys for sources and collections #179
Conversation
lib/assets/AssetsSource.js
Outdated
* @returns {String} the cache key | ||
*/ | ||
AssetsSource.prototype.cacheKey = function(namespace) { | ||
function skipSomeKeys(key, value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe this closes over anything useful, so lets hoist this out to the root of the module, this will prevent the necessary redefinition of skipSomeKeys
each time cacheKey
function is invoked
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, good call
test/test_assets.js
Outdated
}); | ||
assert.equal(source1.cacheKey(), source2.cacheKey()); | ||
assert.notEqual(source1.cacheKey(), source3.cacheKey()); | ||
done(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In mocha, we should likely not use done()
, two reasons:
- sync tests, such as this don't require it. It can safely be dropped.
- async tests, should preferrably rely on a promise being returned in the
it
function
body, as it does error handling correctly (timed out tests do to test failures suck)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I'll remove the use of done() in all these places
test/test_assets.js
Outdated
}); | ||
assert.equal(source1.cacheKey(), source2.cacheKey()); | ||
assert.notEqual(source1.cacheKey(), source3.cacheKey()); | ||
done(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
test/test_assets.js
Outdated
var source2 = new AssetsSource(rootDir, {}); | ||
assert.equal(source1.cacheKey("foo"), source2.cacheKey("foo")); | ||
assert.notEqual(source1.cacheKey("foo"), source2.cacheKey("bar")); | ||
done(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
test/test_assets.js
Outdated
var source3 = new AssetsSource(rootDir2, {}); | ||
assert.equal(source1.cacheKey(), source2.cacheKey()); | ||
assert.notEqual(source1.cacheKey(), source3.cacheKey()); | ||
done(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
test/test_assets.js
Outdated
}); | ||
assert.equal(source1.cacheKey(), source2.cacheKey()); | ||
assert.equal(source1.cacheKey(), source3.cacheKey()); | ||
done(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
test/test_assets.js
Outdated
assert.equal(collection1.cacheKey(), collection2.cacheKey()); | ||
assert.notEqual(collection1.cacheKey(), collection3.cacheKey()); | ||
assert.notEqual(collection1.cacheKey(), collection4.cacheKey()); | ||
done(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
lib/assets/AssetsSource.js
Outdated
";name=" + (this.name ? this.name : (namespace ? namespace : "")) + | ||
";srcPath=" + this.srcPath + | ||
";pattern=" + this.pattern + | ||
";opts=" + JSON.stringify(this.globOpts, skipSomeKeys) + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON.stringify
isn't stable, so it will sometimes produce different ordering of keys in the output. I would recommend using json-stable-stringify
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh ok, I didn't know that - I'll change that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya, i found out the hard way...
@@ -57,6 +59,7 @@ Assets.prototype.addSource = function(src, opts) { | |||
*/ | |||
Assets.prototype.export = function(src, opts) { | |||
var assets = new AssetsCollection(); | |||
this.moduleCollections.push(assets); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this used? If it is new public API, we should be sure to add appropriate tests so that it doesn't vanish.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this is new API to have access to the AssetsCollections in modules (for caching them)
lib/assets/AssetsCollection.js
Outdated
* @returns {String} the cache key | ||
*/ | ||
AssetsCollection.prototype.cacheKey = function(name) { | ||
return this.sources.reduce(function(cacheStr, source) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we sort
sources
to ensure they remain stable for cacheKey generation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, good catch
released as v1.4.0 🎉 |
Adds a few things to allow build tools to cache the Scss generated in asAssetImport(), since otherwise this is done over and over: