-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
174 lines (135 loc) · 4.01 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var process = require('process');
var objectAssign = require('object-assign');
var gulp = require('gulp');
var gulpBabel = require('gulp-babel');
var browserify = require('browserify');
var babelify = require('babelify');
var watchify = require('watchify');
var nodemon = require('nodemon');
var del = require('del');
var paths = require('vinyl-paths');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var notifier = require('node-notifier');
var $ = require('gulp-load-plugins')();
var runSequence = require('run-sequence');
var pkg = require('./package.json');
var path = require('path');
var through = require('through');
var streamNop = through;
var fs = require('fs');
var nopTarget = function( next ){ next(); }
var libraryName = 'pathway-commons';
process.on('SIGINT', function() {
setTimeout(function() {
$.util.log($.util.colors.red('Successfully closed gulp process ' + process.pid));
process.exit(1);
}, 500);
});
var deps = Object.keys( pkg.dependencies ).concat('babel-polyfill');
var emptyTask = function( next ){
next();
};
var logError = function( err ){
notifier.notify({ title: pkg.name, message: 'Error: ' + err.message });
$.util.log( $.util.colors.red(err) );
};
var handleErr = function( err ){
logError( err );
if( this.emit ){
this.emit('end');
}
};
var getBrowserified = function( opts ){
opts = objectAssign({
debug: opts.debug,
cache: {},
packageCache: {},
fullPaths: opts.debug,
bundleExternal: true,
standalone: libraryName,
entries: [ './src' ]
}, opts );
return browserify( opts ).on( 'log', $.util.log );
};
var transform = function( b ){
return ( b
.transform( babelify.configure( JSON.parse( fs.readFileSync('./.babelrc') ) ) )
.external( deps )
) ;
};
var bundle = function( b, opts ){
return ( b
.bundle()
.on( 'error', handleErr )
.pipe( source(libraryName + ( typeof opts === "undefined" || ( opts && opts.debug ) ? ".js" : ".min.js") ) )
.pipe( buffer() )
) ;
};
var setBuildEnv = function( opts ){
if( !opts.debug ){
process.env['NODE_ENV'] = 'production';
}
};
var buildJs = function( opts ){
opts = objectAssign( { debug: true }, opts );
setBuildEnv( opts );
return bundle( transform( getBrowserified( opts ) ), opts )
.pipe( opts.debug ? streamNop() : $.uglify( require('./.uglify.json') ) )
.pipe( gulp.dest('./dist/build') )
;
};
var buildJsDeps = function( opts ){
opts = objectAssign( {
debug: true
}, opts );
setBuildEnv( opts );
var b = browserify({
debug: false
});
deps.forEach(function( dep ){
b.require( dep );
});
return ( b
.bundle()
.on( 'error', handleErr )
.pipe( source('deps.js') )
.pipe( buffer() )
.pipe( opts.debug ? streamNop() : $.uglify( require('./.uglify.json') ) )
.pipe( gulp.dest('./dist/build') )
) ;
};
gulp.task('es6Transform', () => {
gulp.src('./src/**/*.js')
.pipe(gulpBabel())
.pipe(gulp.dest('./dist'));
});
gulp.task('js', function(){ return buildJs(); });
gulp.task('js-prod', function(){ return buildJs({ debug: false }); });
gulp.task('js-deps', nopTarget);
gulp.task('js-deps-prod', nopTarget);
gulp.task('watch', ['es6Transform', 'js-deps'], function(){
$.livereload.listen({
basePath: process.cwd()
});
gulp.watch( ['./src/views/index.html', './src/demo.html', './dist/build/*'] )
.on('change', $.livereload.changed)
;
gulp.watch( ['./package.json'], ['js-deps'] );
var update = function(){
$.util.log( $.util.colors.white('JS rebuilding via watch...') );
bundle( b )
.pipe( gulp.dest('./dist/build') )
.on('finish', function(){
$.util.log( $.util.colors.green('JS rebuild finished via watch') );
})
;
};
var b = getBrowserified( { debug: true } );
transform( b );
b.plugin( watchify, { poll: true } );
b.on( 'update', update );
update();
});
gulp.task('default', ['watch'], emptyTask);
gulp.task('build', ['es6Transform', 'js', 'js-prod', 'js-deps', 'js-deps-prod'], emptyTask);