-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
82 lines (69 loc) · 2.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
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var babelify = require('babelify');
var pkg = require(__dirname + '/package.json');
var path = require('path');
var es = require('event-stream');
var fs = require('fs-extra');
gulp.task('build', function() {
return compile(false);
});
gulp.task('build-release', function() {
return compile(true);
});
gulp.task('watchify', function() {
return compile(false, true);
});
function compile(isUglify, isWatch) {
var entries = pkg.browserify.entryScripts;
var tasks = entries.map( function(entry) {
var extname = path.extname(entry);
var output = path.basename(entry, extname) + '.js';
var bundler = null;
bundler = getBundler(entry, isWatch);
function bundle() {
return bundler.bundle()
.on('error', handleError)
.pipe(source(output))
.pipe(buffer())
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.duration("compiled :" + entry))
.pipe($.if(isUglify, $.uglify()))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest("app/js/dst/"));
}
bundler.on('update', bundle);
return bundle();
});
es.merge.apply(null, tasks);
}
function getBundler(entry, isWatch) {
var bundler = null;
var options = { debug: true };
if (isWatch) {
options.cache = {};
options.packageCache = {};
options.fullPaths = true;
}
bundler = browserify(entry, options);
if (isWatch) {
bundler = watchify(bundler, {poll: true});
}
bundler.transform(babelify);
return bundler;
}
function handleError() {
var notify = require('gulp-notify');
var args = Array.prototype.slice.call(arguments);
notify
.onError({
title: 'Task Error',
message: "<%= error %>"
})
.apply(this, args);
this.emit('end');
}