-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathreact.js
66 lines (53 loc) · 1.88 KB
/
react.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
66
/*
* grunt-react
* https://github.com/ericclemmons/grunt-react
*
* Copyright (c) 2013 Eric Clemmons, contributors
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
var transform = require('react-tools').transform;
grunt.registerMultiTask('react', 'Compile Facebook React JSX templates into JavaScript', function() {
var done = this.async();
var options = this.options();
grunt.verbose.writeflags(options, 'Options');
if (this.files.length < 1) {
grunt.verbose.warn('Destination not written because no source files were provided.');
}
grunt.util.async.forEachSeries(this.files, function(f, nextFileObj) {
var destFile = f.dest;
var files = f.src.filter(function(filepath) {
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
});
if (files.length === 0) {
if (f.src.length < 1) {
grunt.log.warn('Destination not written because no source files were found.');
}
// No src files, go to next target. Warn would have been issued above.
return nextFileObj();
}
var compiled = [];
grunt.util.async.concatSeries(files, function(file, next) {
grunt.verbose.writeln('[react] Compiling ' + file.cyan + ' --> ' + destFile.cyan);
try {
compiled.push(transform(grunt.file.read(file), options));
} catch (e) {
grunt.event.emit('react.error', file, e);
grunt.fail.warn(e);
} finally {
next();
}
}, function () {
grunt.file.write(destFile, compiled.join(grunt.util.normalizelf(grunt.util.linefeed)));
grunt.verbose.writeln('[react] File ' + destFile.cyan + ' created.');
nextFileObj();
});
}, done);
});
};