Skip to content

Commit

Permalink
Merge pull request #180 from mikrostew/cache-options
Browse files Browse the repository at this point in the history
Add optional function for caching the asset import code
  • Loading branch information
stefanpenner committed Mar 20, 2018
2 parents e3f36e2 + ee7bc59 commit 499699d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/assets/Assets.js
Expand Up @@ -50,6 +50,13 @@ Assets.prototype.addSource = function(src, opts) {
return this.collection.addSource(src, opts);
};

/**
* @see AssetsCollection#cacheKey
*/
Assets.prototype.cacheKey = function(name) {
return this.collection.cacheKey(name);
};

/**
* creates a new AssetsCollection and adds the given source
* @see #addSource
Expand Down
14 changes: 13 additions & 1 deletion lib/importers/AssetImporter.js
Expand Up @@ -18,8 +18,20 @@ function AssetImporter(eyeglass, sass, options, fallbackImporter) {
var mod;

function importAssetsFor(mod) {
var contents;
// allow build tools to specify a function to cache the imports
if (typeof options.assetsCache === "function") {
contents = options.assetsCache(
mod.assets.cacheKey(mod.name),
function() {
return mod.assets.asAssetImport(mod.name);
}
);
} else {
contents = mod.assets.asAssetImport(mod.name);
}
importUtils.importOnce({
contents: mod.assets.asAssetImport(mod.name),
contents: contents,
file: "autoGenerated:" + URI.join(mod.name, "assets")
}, done);
}
Expand Down
54 changes: 54 additions & 0 deletions test/test_assets.js
Expand Up @@ -895,6 +895,60 @@ describe("assets", function () {
assert.equal(egMod.assets.moduleCollections.length, 1);
});

it("should cache asset import code with assetsCache()", function() {
var expected = ".test {\n" +
" background: url(\"/mod-one/mod-one.jpg\");\n" +
" background: url(\"/mod-one/subdir/sub.png\"); }\n" +
"\n" +
".all-assets {\n" +
" app-assets: \"images/foo.png\", \"fonts/foo.woff\";\n" +
" mod-assets: \"mod-one/mod-one.jpg\", \"mod-one/subdir/sub.png\"; }\n";
var rootDir = testutils.fixtureDirectory("app_assets");
var testCache = {};
var cacheHits = 0;
var cacheMisses = 0;

function assetsCache(key, getValue) {
if (testCache[key]) {
cacheHits += 1;
return testCache[key];
}
cacheMisses += 1;
return testCache[key] = getValue();
}

var eg = new Eyeglass({
file: path.join(rootDir, "sass", "uses_mod_1.scss"),
eyeglass: {
root: rootDir,
installWithSymlinks: installWithSymlinks,
engines: {
sass: sass
}
},
assetsCache: assetsCache,
});

eg.assets.addSource(rootDir, {pattern: "images/**/*"});
eg.assets.addSource(rootDir, {pattern: "fonts/**/*"});

return new Promise(function(resolve) {
testutils.assertCompiles(eg, expected, resolve);
}).then(function() {
// the first time this is compiled no cache hits
assert.equal(cacheMisses, 2);
assert.equal(cacheHits, 0);

return new Promise(function(resolve) {
testutils.assertCompiles(eg, expected, resolve);
});
}).then(function() {
// the second time, both should hit the cache
assert.equal(cacheMisses, 2);
assert.equal(cacheHits, 2);
});
});

describe("path separator normalization", function() {
var originalEnv = process.env.EYEGLASS_NORMALIZE_PATHS;
var merge = require("lodash.merge");
Expand Down

0 comments on commit 499699d

Please sign in to comment.