-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
58 lines (47 loc) · 1.54 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
'use strict';
// through2 is a thin wrapper around node transform streams
var through = require('through2');
var rt = require('react-templates');
var PluginError = require('plugin-error');
var path = require('path');
var merge = require('merge');
// Consts
var PLUGIN_NAME = 'gulp-react-templates';
function normalizeName(name) {
return name.replace(/-/g, '_');
}
module.exports = function (opt) {
function replaceExtension(filePath) {
return filePath + '.js';
}
function transform(file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
var data;
var filePath = file.path;
var str = file.contents.toString('utf8');
var dest = replaceExtension(file.path);
var options = merge({
filename: file.path,
sourceFiles: [file.relative],
generatedFile: replaceExtension(file.relative)
}, opt);
var shouldAddName = options.modules === 'none' && !options.name;
if (shouldAddName) {
options.name = normalizeName(path.basename(filePath, path.extname(filePath))) + 'RT';
}
try {
data = rt.convertTemplateToReact(str, options);
} catch (err) {
return cb(new PluginError(PLUGIN_NAME, err));
}
file.contents = new Buffer(data);
file.path = dest;
cb(null, file);
}
return through.obj(transform);
};