/
index.js
45 lines (43 loc) · 1.11 KB
/
index.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
// TODO: source maps
var path = require('path')
, fs = require('fs')
, existsSync = fs.existsSync || path.existsSync
, COMPILERS = {
'js': '',
'css': '',
'html': '',
'coffee': 'coffeescript',
'ls': 'livescript',
'hbs': 'handlebars',
'handlebars': 'handlebars',
'styl': 'stylus',
'less': 'less',
'jade': 'jade',
'twig': 'twig',
'dust': 'dust'
};
/**
* Convert contents to js/css/html
* @param {String} filepath
* @param {String} content
* @param {Object} [options]
* @param {Function} fn(err, content)
*/
module.exports = function(filepath, content, options, fn) {
if (!fn && 'function' == typeof options) {
fn = options;
options = {};
}
var extension = path.extname(filepath).slice(1)
, name = path.basename(filepath).replace(extension, '');
options.filepath = options.filename = filepath;
options.extension = extension;
options.name = name;
if (COMPILERS[extension]) {
return require('./lib/' + COMPILERS[extension])(content, options, fn);
} else if (COMPILERS[extension] != null) {
return fn(null, content);
} else {
return fn('no compiler found for ' + filepath, content);
}
};