/
asset_attributes.js
252 lines (197 loc) · 6 KB
/
asset_attributes.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/** internal
* class AssetAttributes
*
* `AssetAttributes` is a wrapper similar to Rubie's `Pathname` that provides
* some helper accessors.
*
* These methods should be considered internalish.
**/
'use strict';
// stdlib
var path = require('path');
// 3rd-party
var _ = require('lodash');
// internal
var prop = require('./common').prop;
var getter = require('./common').getter;
////////////////////////////////////////////////////////////////////////////////
/**
* new AssetAttributes(environment, pathanme)
**/
var AssetAttributes = module.exports = function AssetAttributes(environment, pathname) {
prop(this, 'environment', environment);
prop(this, 'pathname', pathname);
};
/**
* AssetAttributes#searchPaths -> Array
*
* Returns paths search the load path for.
**/
getter(AssetAttributes.prototype, 'searchPaths', function () {
var paths = [ this.pathname ],
exts = this.extensions.join(''),
path_without_extensions;
path_without_extensions = this.extensions.reduce(function (p, ext) {
return p.replace(ext, '');
}, this.pathname);
// optimization: bower.json can only be nested one level deep
if (path_without_extensions.indexOf(path.sep) === -1) {
paths.push(path.join(path_without_extensions, 'bower.json'));
}
if (path.basename(this.pathname, exts) !== 'index') {
paths.push(path.join(path_without_extensions, 'index' + exts));
}
return paths;
});
/**
* AssetAttributes#logicalPath -> String
*
* Reverse guess logical path for fully expanded path.
*
* This has some known issues. For an example if a file is
* shaddowed in the path, but is required relatively, its logical
* path will be incorrect.
**/
getter(AssetAttributes.prototype, 'logicalPath', function () {
var pathname = this.pathname, paths = this.environment.paths, root_path;
root_path = _.find(paths, function (root) {
return path.join(root, '/') === pathname.substr(0, root.length + 1);
});
if (!root_path) {
throw new Error('File outside paths: ' + pathname + ' isn\'t in paths: ' +
paths.join(', '));
}
pathname = pathname.replace(root_path + path.sep, '');
pathname = pathname.replace(/\\/g, '/');
pathname = this.engineExtensions.reduce(function (p, ext) {
return p.replace(ext, '');
}, pathname);
if (!this.formatExtension) {
pathname += (this.engineFormatExtension || '');
}
return pathname;
});
/**
* AssetAttributes#relativePath -> String
*
* Returns full path name relative to environment's root.
**/
getter(AssetAttributes.prototype, 'relativePath', function () {
var root = this.environment.root, pathname = String(this.pathname);
if (root === pathname.substr(0, root.length)) {
return pathname.substr(root.length);
}
return pathname;
});
/**
* AssetAttributes#extensions -> Array
*
* Returns `Array` of extension `String`s.
*
* "foo.js.coffee"
* // -> [".js", ".coffee"]
**/
getter(AssetAttributes.prototype, 'extensions', function () {
var extensions;
if (!this.__extensions__) {
extensions = path.basename(this.pathname).split('.').slice(1);
prop(this, '__extensions__', extensions.map(function (ext) {
return '.' + ext;
}));
}
return this.__extensions__.slice();
});
/**
* AssetAttributes#formatExtension -> String
*
* Returns the format extension.
*
* "foo.js.coffee"
* // -> ".js"
**/
getter(AssetAttributes.prototype, 'formatExtension', function () {
return _.find(this.extensions.reverse(), function (ext) {
return this.getMimeType(ext) && !this.getEngines(ext);
}, this.environment);
});
/**
* AssetAttributes#engineExtension -> Array
*
* Returns an `Array` of engine extensions.
*
* "foo.js.coffee.ejs"
* // -> [".coffee", ".ejs"]
**/
getter(AssetAttributes.prototype, 'engineExtensions', function () {
var env = this.environment,
exts = this.extensions,
offset = exts.indexOf(this.formatExtension);
if (offset >= 0) {
exts = exts.slice(offset + 1);
}
return _.filter(exts, function (ext) { return !!env.getEngines(ext); });
});
/**
* AssetAttributes#engines -> Array
*
* Returns an array of engine classes.
**/
getter(AssetAttributes.prototype, 'engines', function () {
var env = this.environment;
return this.engineExtensions.map(function (ext) { return env.getEngines(ext); });
});
/**
* AssetAttributes#processors -> Array
*
* Returns all processors to run on the path.
**/
getter(AssetAttributes.prototype, 'processors', function () {
return [].concat(this.environment.getPreProcessors(this.contentType),
this.engines.reverse(),
this.environment.getPostProcessors(this.contentType));
});
/**
* AssetAttributes#contentType -> String
*
* Returns the content type for the pathname.
* Falls back to `application/octet-stream`.
**/
getter(AssetAttributes.prototype, 'contentType', function () {
var mime_type;
if (!this.__contentType__) {
mime_type = this.engineContentType || 'application/octet-stream';
if (this.formatExtension) {
mime_type = this.environment.getMimeType(this.formatExtension, mime_type);
}
prop(this, '__contentType__', mime_type);
}
return this.__contentType__;
});
/**
* AssetAttributes#engineContentType -> String
*
* Returns implicit engine content type.
*
* `.coffee` files carry an implicit `application/javascript`
* content type.
**/
getter(AssetAttributes.prototype, 'engineContentType', function () {
var engine = _.find(this.engines.reverse(), function (engine) {
return !!engine.defaultMimeType;
});
return (engine || {}).defaultMimeType;
});
/**
* AssetAttributes#engineFormatExtension -> String
*
* Returns implicit engine extension.
*
* `.coffee` files carry an implicit `.js` extension (due to it's implicit
* content type of `application/javascript`).
**/
getter(AssetAttributes.prototype, 'engineFormatExtension', function () {
var type = this.engineContentType;
if (type) {
return this.environment.getExtensionForMimeType(type);
}
});