forked from krasimir/react-in-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
70 lines (64 loc) · 2.09 KB
/
gulpfile.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
67
68
69
70
var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var gutil = require('gulp-util');
var sourcemaps = require('gulp-sourcemaps');
var babelify = require('babelify');
var fs = require('fs');
var duration = require('gulp-duration');
var eslint = require('gulp-eslint');
var createBundler = function(folder) {
return browserify({
entries: [
'./patterns/' + folder + '/src/app.jsx'
],
debug: true,
transform: [ babelify ]
});
}
var build = function (folder, callback) {
folder.bundler.bundle()
.on('error', gutil.log)
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true })).on('error', gutil.log)
.pipe(sourcemaps.write('./'))
.pipe(duration(folder.path + ' changed'))
.pipe(gulp.dest('./patterns/' + folder.path + '/public'))
.on('end', callback ? callback : function() {});
};
var folders = fs.readdirSync('./patterns')
.filter(function (folder) {
try { fs.statSync('./patterns/' + folder + '/src/app.jsx') } catch(err) {
gutil.log(gutil.colors.yellow('Note: Path ./patterns/' + folder + '/src/app.jsx does not exist ' +
'which is fine. It simply means that the ./patterns/' + folder + ' does not require an example or it is placed in another repo.'));
return false;
}
return true;
})
.map(function (folder) {
return { path: folder, bundler: createBundler(folder) }
});
gulp.task('build', function () {
return Promise.all(folders.map(function (folder) {
return new Promise(function (resolve, reject) {
build(folder, resolve);
});
}));
});
gulp.task('build-watch', ['build'], function () {
folders.forEach(function (folder) {
gulp.watch('./patterns/' + folder.path + '/src/**/*.*').on('change', function () {
build(folder);
});
});
});
gulp.task('lint', function () {
return gulp.src(['**/*.js','!node_modules/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('default', ['build']);
gulp.task('watch', ['build-watch']);