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 cache keys for sources and collections #179

Merged
merged 4 commits into from
Mar 16, 2018
Merged

Generate cache keys for sources and collections #179

merged 4 commits into from
Mar 16, 2018

Conversation

mikrostew
Copy link
Contributor

@mikrostew mikrostew commented Mar 15, 2018

Adds a few things to allow build tools to cache the Scss generated in asAssetImport(), since otherwise this is done over and over:

  • keeps a list of references to the AssetsCollections produced for modules
  • adds functions to produce cache keys for AssetsSource and AssetsCollection

@mikrostew mikrostew changed the title Make cache keys Generate cache keys for sources and collections Mar 15, 2018
* @returns {String} the cache key
*/
AssetsSource.prototype.cacheKey = function(namespace) {
function skipSomeKeys(key, value) {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good call

});
assert.equal(source1.cacheKey(), source2.cacheKey());
assert.notEqual(source1.cacheKey(), source3.cacheKey());
done();
Copy link
Member

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:

  1. sync tests, such as this don't require it. It can safely be dropped.
  2. 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)

Copy link
Contributor Author

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

});
assert.equal(source1.cacheKey(), source2.cacheKey());
assert.notEqual(source1.cacheKey(), source3.cacheKey());
done();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

var source2 = new AssetsSource(rootDir, {});
assert.equal(source1.cacheKey("foo"), source2.cacheKey("foo"));
assert.notEqual(source1.cacheKey("foo"), source2.cacheKey("bar"));
done();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

var source3 = new AssetsSource(rootDir2, {});
assert.equal(source1.cacheKey(), source2.cacheKey());
assert.notEqual(source1.cacheKey(), source3.cacheKey());
done();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

});
assert.equal(source1.cacheKey(), source2.cacheKey());
assert.equal(source1.cacheKey(), source3.cacheKey());
done();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

assert.equal(collection1.cacheKey(), collection2.cacheKey());
assert.notEqual(collection1.cacheKey(), collection3.cacheKey());
assert.notEqual(collection1.cacheKey(), collection4.cacheKey());
done();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

";name=" + (this.name ? this.name : (namespace ? namespace : "")) +
";srcPath=" + this.srcPath +
";pattern=" + this.pattern +
";opts=" + JSON.stringify(this.globOpts, skipSomeKeys) +
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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);
Copy link
Member

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.

Copy link
Contributor Author

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)

* @returns {String} the cache key
*/
AssetsCollection.prototype.cacheKey = function(name) {
return this.sources.reduce(function(cacheStr, source) {
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, good catch

@stefanpenner stefanpenner merged commit c1db1ec into linkedin:master Mar 16, 2018
@stefanpenner
Copy link
Member

released as v1.4.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants