forked from sindresorhus/gulp-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (31 loc) · 876 Bytes
/
index.js
File metadata and controls
37 lines (31 loc) · 876 Bytes
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
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var react = require('react-tools');
module.exports = function (options) {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-react', 'Streaming not supported'));
return cb();
}
var str = file.contents.toString();
if (path.extname(file.path) === '.jsx' && str.indexOf('/** @jsx') === -1) {
str = '/** @jsx React.DOM */\n' + str;
}
try {
file.contents = new Buffer(react.transform(str, options));
file.path = gutil.replaceExtension(file.path, '.js');
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-react', err, {
fileName: file.path
}));
}
this.push(file);
cb();
});
};