-
Notifications
You must be signed in to change notification settings - Fork 60
/
AssetsCollection.js
65 lines (58 loc) · 2.11 KB
/
AssetsCollection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"use strict";
var AssetsSource = require("./AssetsSource");
var stringUtils = require("../util/strings");
var URI = require("../util/URI");
var assetRegisterTmpl = "@include asset-register(${namespace}, ${name}, ${sourcePath}, ${uri});\n";
function AssetsCollection() {
this.sources = [];
}
/**
* adds an AssetsSource to the collection
* @param {String} src - the source directory of the assets
* @param {Object} opts - the options to pass @see AssetsSource
* @returns {AssetsCollection} returns the instance of AssetsCollection for chaining
*/
AssetsCollection.prototype.addSource = function(src, opts) {
this.sources.push(new AssetsSource(src, opts));
return this;
};
/**
* returns the scss to register all the assets
* @param {String} name - the namespace to use
* @returns {String} the scss representation of the asset registration
*/
AssetsCollection.prototype.asAssetImport = function (name) {
// builds the scss to register all the assets
// this will look something like...
// @import "eyeglass/assets";
// @include asset-register(
// "namespace",
// "path/to/foo.png",
// "/absolute/namespace/path/to/foo.png",
// "namespace/path/to/foo.png"
// );
return this.sources.reduce(function(importStr, source) {
// get the assets for the entry
var assets = source.getAssets(name);
var namespace = (stringUtils.quote(assets.namespace) || "null");
// reduce the assets into a `asset-register` calls
return importStr + assets.files.reduce(function(registerStr, asset) {
return registerStr + stringUtils.tmpl(assetRegisterTmpl, {
namespace: namespace,
name: URI.sass(asset.name),
sourcePath: URI.sass(asset.sourcePath),
uri: URI.sass(asset.uri)
});
}, "");
}, '@import "eyeglass/assets";\n');
};
/**
* Build a string suitable for caching an instance of this
* @returns {String} the cache key
*/
AssetsCollection.prototype.cacheKey = function(name) {
return this.sources.reduce(function(cacheStr, source) {
return cacheStr + ":" + source.cacheKey(name);
}, "sources");
};
module.exports = AssetsCollection;