'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');
			this.push(file);
		} catch (err) {
			this.emit('error', new gutil.PluginError('gulp-react', err, {
				fileName: file.path
			}));
		}

		cb();
	});
};

'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;

		if (path.extname(filePath) === '.jsx' && str.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();
	});
};

Fix missing filePath in Error and PluginError.
Fix missing filePath in Error and PluginError.

file.contents = … somehow resets file.path on error so we have to store
it. Also, React Error does not specify fileName, so add it if missing.
