forked from sindresorhus/gulp-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (36 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
40
41
42
'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();
var filePath = file.path;
// Regex Credits: http://stackoverflow.com/a/15123777
var JS_COMMENTS_REGEX = /(?:\/\*(?:[\s\S]*?)\*\/)|(?:\/\/(?:.*)$)/gm;
var commentMatches = str.match(JS_COMMENTS_REGEX);
if (path.extname(filePath) === '.jsx' && !(commentMatches && (commentMatches[0].indexOf('/**') !== -1 && commentMatches[0].indexOf('@jsx') !== -1))) {
str = '/** @jsx React.DOM */\n' + str;
}
try {
file.contents = new Buffer(react.transform(str, options));
file.path = gutil.replaceExtension(filePath, '.js');
this.push(file);
} catch (err) {
err.fileName = err.fileName || filePath;
this.emit('error', new gutil.PluginError('gulp-react', err, {
fileName: filePath
}));
}
cb();
});
};