-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
109 lines (92 loc) · 2.72 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
var gulp = require('gulp'),
gutil = require('gulp-util'),
eslint = require('gulp-eslint'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
browserify = require('browserify'),
watchify = require('watchify'),
babelify = require('babelify'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
lrload = require('livereactload'),
browserSync = require('browser-sync'),
jest = require('jest-cli');
var isProd = process.env.NODE_ENV === 'production'
var SRC = './src';
var DIST = './dist';
var EXAMPLE_SRC = './example';
var EXAMPLE_DIST = './distExample';
var jestConfig = {
rootDir: SRC,
collectCoverage: true
}
gulp.task('test', function (done) {
jest.runCLI({ config: jestConfig }, '.', function () {
done();
})
});
gulp.task('tdd', function () {
gulp.watch([ jestConfig.rootDir + '/**/*.spec.js' ], ['test']);
});
function createBundler(useWatchify, entry) {
return browserify({
entries: entry || EXAMPLE_SRC + '/index.js',
exclude: SRC + '/**/*.spec.js',
extensions: ['jsx'],
transform: [[babelify, {}]],
plugin: isProd || !useWatchify ? [] : [ lrload ], // no additional configuration is needed
debug: !isProd,
cache: {},
packageCache: {},
fullPaths: !isProd // for watchify
})
}
function lint(src, isProd) {
return gulp.src(`${src}/**/*.js`)
.pipe(eslint())
.pipe(eslint.format())
.pipe(isProd ? eslint.failOnError() : gutil.noop());
}
gulp.task('statics', function () {
gulp.src(`${EXAMPLE_SRC}/index.html`).pipe(gulp.dest(EXAMPLE_DIST));
})
gulp.task('bundle', function() {
var bundler = createBundler(false, SRC + '/index.js');
lint(SRC, true);
bundler.bundle()
.pipe(source('index.js'))
// .pipe(buffer())
// .pipe(isProd ? uglify() : gutil.noop())
// .pipe(isProd ? rename({suffix: '.min'}) : gutil.noop())
.pipe(gulp.dest(DIST));
});
gulp.task('watch', function() {
// start JS file watching and rebundling with watchify
var bundler = createBundler(true)
var watcher = watchify(bundler)
rebundle()
return watcher
.on('error', gutil.log)
.on('update', rebundle)
function rebundle() {
gutil.log('Update JavaScript bundle')
lint(SRC, isProd)
watcher
.bundle()
.on('error', gutil.log)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gulp.dest(isProd ? DIST : EXAMPLE_DIST))
}
});
gulp.task('serve', ['statics'], function () {
browserSync({
browser: 'google chrome',
server: {
baseDir: EXAMPLE_DIST,
},
});
})
gulp.task('dist', ['bundle'])
gulp.task('default', ['serve', 'watch'])