forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstylusAsset.js
More file actions
executable file
·41 lines (30 loc) · 1.13 KB
/
stylusAsset.js
File metadata and controls
executable file
·41 lines (30 loc) · 1.13 KB
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
var fs = require('fs'),
path = require('path'),
crypto = require('crypto'),
nodes = require('stylus').nodes,
utils = require('stylus').utils;
module.exports = function(options) {
var getVersion = options.getVersion || function(file) {
var buf = fs.readFileSync(file);
return crypto.createHash('md5').update(buf).digest('hex').substring(0, 8);
};
return function(style) {
var paths = style.options.paths || [];
style.define('asset', function(url) {
var literal = new nodes.Literal('url("' + url.val + '")');
var evaluator = this;
var file = utils.lookup(url.val, paths);
if (!file) {
throw new Error('File ' + literal + ' not be found');
}
var version = getVersion(file);
var ext = path.extname(url.val);
var filepath = url.val.slice(0, url.val.length - ext.length);
var newUrl = options.assetVersioning == 'query' ? (url.val + '?' + version) :
options.assetVersioning == 'file' ? (filepath + '.v' + version + ext) :
url.val;
literal = new nodes.Literal('url("../i/' + newUrl + '")');
return literal;
});
};
};