This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
93 lines (78 loc) · 2.22 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
const gulp = require('gulp')
const gutil = require('gulp-util')
const babel = require('gulp-babel')
const plumber = require('gulp-plumber')
const browserify = require('browserify')
const dirname = require('path').dirname
const source = require('vinyl-source-stream')
const srcdir = __dirname + '/src'
const destdir = __dirname + '/dist'
/**
* 1. transpile all babel files
* 2. watch src dir
* 3. transpile on changed
* 4. browserify if the file is web or titanium
*/
gulp.task('watch', x => {
gulp.start('babel:all')
gulp.watch('src/**/*.js', (info) => {
const src = info.path
const relpath = src.slice(srcdir.length + 1)
const dest = destdir + '/' + dirname(relpath)
gutil.log(`[${info.type}]: ${relpath}`)
compileBabel(src, dest)
.on('end', x => {
gutil.log(`compile finished: ${dest}`)
if (isSubDir(relpath, ['common', 'web'])) gulp.start('browserify:web')
if (isSubDir(relpath, ['common', 'titanium'])) gulp.start('browserify:ti')
})
})
})
/**
* 1. transpile all babel files
* 2. browserify titanium and web files
*/
gulp.task('babel:all', x => {
compileBabel('src/**/*.js', 'dist')
.on('end', x => {
console.log('compilation finished: all js files.')
gulp.start('browserify:web')
gulp.start('browserify:ti')
})
})
/**
* browserify titanium files
*/
gulp.task('browserify:ti', x => {
return browserify('dist/titanium/faster-titanium.js')
.bundle()
.pipe(plumber())
.pipe(source('faster-titanium.bundle.js'))
.pipe(gulp.dest('dist/titanium'))
})
/**
* browserify web files
*/
gulp.task('browserify:web', x => {
return browserify('dist/web/main.js')
.bundle()
.pipe(plumber())
.pipe(source('main.bundle.js'))
.pipe(gulp.dest('dist/web'))
})
/**
* check if given relpath is in dirs
*/
function isSubDir(relpath, dirs) {
const subdir = relpath.split('/')[0]
return dirs.some(dir => dir === subdir)
}
/**
* transipile babel src to dest
*/
function compileBabel(src, dest) {
return gulp.src(src)
.pipe(plumber())
.pipe(babel())
.pipe(gulp.dest(dest))
}