-
Notifications
You must be signed in to change notification settings - Fork 157
/
gulpfile.js
85 lines (77 loc) · 2.14 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
const gulp = require('gulp')
const rollup = require('rollup-stream')
const sourcemaps = require('gulp-sourcemaps')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const rename = require('gulp-rename')
const uglify = require('gulp-uglify')
const mochaPhantomjs = require('gulp-mocha-phantomjs')
const buble = require('gulp-buble')
const del = require('del')
const pump = require('pump')
const eslint = require('gulp-eslint')
// clean build folder
gulp.task('clean', () => {
return del([
'lib/*'
])
})
// lint 'em files
gulp.task('lint', () => {
return gulp.src(['src/*.js', 'tests/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
})
// compile to es2015 but leave the import/exports intact
gulp.task('compile', ['lint'], () => {
return gulp.src('./src/index.js')
.pipe(sourcemaps.init())
.pipe(buble({
transforms: {
modules: false
}
}))
.pipe(rename('isInViewport.es6.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('lib'))
})
// bundle it
gulp.task('rollup', ['compile'], () => {
return rollup({
input: './lib/isInViewport.es6.js',
format: 'umd',
name: 'isInViewport',
globals: {
jquery: 'jQuery',
window: 'window'
},
external: ['jquery', 'window']
})
.pipe(source('isInViewport.es6.js', './lib'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(rename('isInViewport.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('lib'))
})
// minify it
gulp.task('minify', ['rollup'], (cb) => {
pump([
gulp.src('./lib/isInViewport.js'),
sourcemaps.init({loadMaps: true}),
uglify({
preserveComments: (_, {type, value: text}) => type === 'comment2' && /@(author|license)/g.test(text)
}),
rename('isInViewport.min.js'),
sourcemaps.write('.'),
gulp.dest('lib')
], cb)
})
// test the transpiled, minified bundle
gulp.task('test', ['minify'], () => {
return gulp.src('./tests/tests.html')
.pipe(mochaPhantomjs())
.on('error', () => gulp.start('clean')) // remove built files if tests fail
})
gulp.task('default', ['clean', 'test'])