forked from gulp-community/gulp-haml
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
39 lines (33 loc) · 1.17 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
var map = require('map-stream');
var rext = require('replace-ext');
var hamlc = require('haml-coffee');
var gutil = require('gulp-util');
module.exports = function(options) {
if(!options) options = {};
// Map each file to this function
function hamlStream(file, cb) {
if (file.isNull()) return cb(null, file); // pass along
if (file.isStream()) return cb(new Error("gulp-haml-coffee: Streaming not supported"));
// gulp-haml-coffee compiles to plain HTML per default. If the `js` option is set,
// it will compile to a JS function.
var output;
try {
if (options.js) {
output = hamlc.template(file.contents.toString("utf8"), options.name, options.namespace, options);
file.path = rext(file.path, ".js");
} else {
output = hamlc.render(file.contents.toString("utf8"), options.locals || {}, options);
file.path = rext(file.path, ".html");
}
} catch (e) {
throw new gutil.PluginError('gulp-haml-coffee',
'Error compiling ' + file.path + ': ' + e, {
showStack: true
});
}
file.contents = new Buffer(output);
cb(null, file);
}
// Return a stream
return map(hamlStream);
};