-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.js
65 lines (56 loc) · 1.88 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
54
55
56
57
58
59
60
61
62
63
64
65
var through2 = require('through2');
var PluginError = require('plugin-error');
const path = require('path');
const replaceExt = require('replace-ext');
const PLUGIN_NAME = 'gulp-handlebars';
module.exports = function(opts) {
'use strict';
opts = opts || {};
var compilerOptions = opts.compilerOptions || {};
var handlebars = opts.handlebars || require('handlebars');
return through2.obj(function(file, enc, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
return callback();
}
var contents = file.contents.toString();
var compiled = null;
var defaultCompiler = function(contents, options) {
var ast = handlebars.parse(contents);
// Preprocess AST before compiling
if (opts.processAST) {
// processAST may return new AST or change it in place
ast = opts.processAST(ast) || ast;
}
return handlebars.precompile(ast, options).toString();
};
// defaultCompiler used to render any handlebars templates
// `opts.compiler` allows third party to override the internal compiler
var compiler = (typeof opts.compiler === 'function') ? opts.compiler : defaultCompiler;
try {
compiled = compiler(contents, compilerOptions);
}
catch (err) {
this.emit('error', new PluginError(PLUGIN_NAME, err, {
fileName: file.path
}));
return callback();
}
file.contents = new Buffer(compiled);
file.path = replaceExt(file.path, '.js');
// Options that take effect when used with gulp-define-module
file.defineModuleOptions = {
require: {
Handlebars: 'handlebars'
},
context: {
handlebars: 'Handlebars.template(<%= contents %>)'
},
wrapper: '<%= handlebars %>'
};
callback(null, file);
});
};