-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
99 lines (82 loc) · 2.05 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
var gulp = require('gulp');
var gulpif = require('gulp-if');
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var del = require('del');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var _ = require('lodash');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
global.isProd = false;
global.needSourceMap = true;
var handleErrors = function() {
var args = Array.prototype.slice.call(arguments);
delete args[0].stream;
$.util.log(args);
this.emit('end');
};
gulp.task('clean', function(cb) {
del([
'lib/'
], cb);
});
var bundler = _.memoize(function(watch) {
var options = {debug: true};
if (global.isProd) {
options.debug = false;
}
if (watch) {
_.extend(options, watchify.args);
}
var b = browserify('./src/component.js', options);
if (watch) {
b = watchify(b);
}
return b;
});
function bundle(cb, watch) {
return bundler(watch).bundle()
.on('error', handleErrors)
.pipe(source('component.js'))
.pipe(buffer())
.pipe(gulpif(global.needSourceMap, $.sourcemaps.init({ loadMaps: true })))
.pipe(gulpif(global.needSourceMap, $.sourcemaps.write('./')))
.pipe(gulp.dest('./lib'))
.on('end', cb)
.pipe(reload({ stream: true }));
}
gulp.task('scripts', function(cb) {
process.env.BROWSERIFYSWAP_ENV = 'dist';
var is_watch = true;
if (global.isProd) {
is_watch = false;
}
bundle(cb, is_watch);
});
var reporter = 'spec';
gulp.task('build-all', [
'clean',
'scripts'
]);
gulp.task('watch', ['build-all'], function(cb) {
if (!global.isProd) {
browserSync({
server: {
baseDir: 'dist'
}
});
reporter = 'dot';
bundler(true).on('update', function() {
gulp.start('scripts');
});
gulp.watch(['./src/main.less', './src/**/*.less'], ['styles']);
}
});
gulp.task('set_prod', function() {
global.isProd = true;
global.needSourceMap = false;
});
gulp.task('default', ['watch']);
gulp.task('build', ['set_prod', 'watch']);