-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (42 loc) · 1.47 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
46
47
48
49
50
51
52
53
'use strict';
var through = require('through2');
var PluginError = require('gulp-util').PluginError;
var objectAssign = require('object-assign');
var defaultOptions = {
templateCompiler: null
// Whatever comes with your Ember package template compiler
// templateCompiler: require('../bower_components/ember/ember-template-compiler')
};
function compile (contents, opts) {
var contentsToString = String(contents);
var content = "export default Ember.HTMLBars.template(" + opts.templateCompiler.precompile(contentsToString, false) + ');';
return new Buffer(content);
}
module.exports = function (options) {
options = options || {};
function HtmlBars (file, enc, cb) {
if (file.isNull()) {
cb(null, file); // return an empty file
return;
}
// Do not do streams by gulp design
if (file.isStream()) {
cb(new PluginError('gulp-htmlbars', 'Streaming not supported', {fileName: file.path}));
return;
}
var opts = objectAssign(defaultOptions, options);
if (!opts.templateCompiler) {
cb(new PluginError('gulp-htmlbars', 'Missing value of templateCompiler from options'));
return;
}
// `file.contents` type should always be the same going out as it was when it came in
try {
file.contents = compile(file.contents, opts);
this.push(file);
} catch (err) {
this.emit('error', new PluginError('gulp-htmlbars', err, {fileName: file.path}));
}
cb();
}
return through.obj(HtmlBars);
};