/
gulpfile.js
117 lines (97 loc) · 3.11 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
110
111
112
113
114
115
116
117
var gulp = require('gulp')
var gutil = require('gulp-util')
var source = require('vinyl-source-stream')
var watchify = require('watchify')
var livereload = require('gulp-livereload')
var connect = require('gulp-connect')
var uglify = require('gulp-uglify')
var browserify = require('browserify')
var xtend = require('xtend')
var format = require('format-text')
var streamify = require('gulp-streamify')
var fs = require('fs')
var demos = require('./demos')
var path = require('path')
var asyncEach = require('async-each')
var htmlTemplate = fs.readFileSync(__dirname+'/template.html', 'utf8')
if (gutil.env.demo) {
var s = gutil.env.demo.split('-')
gutil.env.f = s[0]
gutil.env.d = s.slice(1).join('-')
}
gulp.task('dev', function(){
if (typeof gutil.env.f !== 'string' && typeof gutil.env.d !== 'string')
return gutil.log(gutil.colors.red('Must specify a demo!'))
var d = getDemo(gutil.env.f, gutil.env.d) || { name: gutil.env.d, folder: gutil.env.f }
bundleDemo(d, !gutil.env.prod)
})
gulp.task('production', function(cb) {
asyncEach(demos, function (d) {
return bundleDemo(d, false)
}, cb)
})
gulp.task('watch', ['dev'], function(){
connect.server({
port: 8000,
root: 'dist'
})
livereload.listen(35729)
});
gulp.task('html', function(cb) {
demos.forEach(function(d) {
var jsFile = bundleFilename(d)
var demoTemplate = htmlTemplate
if (d.template)
demoTemplate = fs.readFileSync(d.template, 'utf8')
var doc = format(demoTemplate, xtend({
title: d.name,
entry: 'js/'+jsFile
}, d))
var f = 'dist/'
var outfile = d.outfile || (d.folder+'-'+d.name+'.html')
fs.writeFileSync(path.join(f, outfile), doc)
})
if (cb)
cb()
})
gulp.task('copy', function() {
return gulp.src(['image/icons/**'])
.pipe(gulp.dest('dist/icons'))
})
gulp.task('default', ['html', 'copy', 'production'])
function bundleFilename(d) {
return 'bundle-'+d.folder+'-'+d.name+'.js'
}
function bundleDemo(d, watch) {
var entry = path.join(d.folder, d.name+'.js')
var outfile = bundleFilename(d)
return bundle(entry, outfile, watch, d.transforms )
}
function bundle(entry, out, watch, transforms) {
var args = xtend(watchify.args, { debug: watch })
var b = watch ? watchify(browserify(args)) : browserify()
b.on('update', doBundle)
b.add('./'+entry)
if (transforms)
transforms.forEach(function(t) { b.transform(t) })
function doBundle() {
var p = b.bundle()
.on('error', function(e) {
gutil.beep()
gutil.log( gutil.colors.red('Bundle error: ',e.message) )
})
.pipe(source(out))
if (!watch)
p = p.pipe(streamify(uglify({ mangle: true, compress: true })))
p = p.pipe(gulp.dest('./dist/js'))
if (watch)
p = p.pipe(livereload())
return p
}
return doBundle()
}
function getDemo(folder, name) {
return demos.filter(function(d) {
return d.name === name && d.folder === folder
})[0]
}