-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
140 lines (110 loc) · 3.53 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
/*
* biojs-vis-bioschemas-event-list
* https://github.com/ficolo/biojs-vis-bioschemas-event-list
*
* Copyright (c) 2015 Federico
* Licensed under the MIT license.
*/
// browserify build config
var buildDir = "build";
var outputFile = "list";
// packages
var gulp = require('gulp');
// browser builds
var browserify = require('browserify');
var watchify = require('watchify')
var uglify = require('gulp-uglify');
// code style
var jshint = require('gulp-jshint');
// gulp helper
var source = require('vinyl-source-stream'); // converts node streams into vinyl streams
var gzip = require('gulp-gzip');
var rename = require('gulp-rename');
var chmod = require('gulp-chmod');
var streamify = require('gulp-streamify'); // converts streams into buffers (legacy support for old plugins)
// path tools
var fs = require('fs');
var path = require('path');
var join = path.join;
var mkdirp = require('mkdirp');
var del = require('del');
// auto config
var outputFileMin = join(buildDir,outputFile + ".min.js");
var packageConfig = require('./package.json');
// a failing test breaks the whole build chain
gulp.task('build', ['build-browser', 'build-browser-gzip']);
gulp.task('default', ['lint', 'build']);
gulp.task('lint', function() {
return gulp.src('./lib/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// will remove everything in build
gulp.task('clean', function(cb) {
del([buildDir], cb);
});
// just makes sure that the build dir exists
gulp.task('init', ['clean'], function() {
mkdirp(buildDir, function (err) {
if (err) console.error(err)
});
});
// browserify debug
gulp.task('build-browser',['init'], function() {
var b = browserify({debug: true,hasExports: true});
exposeBundles(b);
return b.bundle()
.pipe(source(outputFile + ".js"))
.pipe(chmod(644))
.pipe(gulp.dest(buildDir));
});
// browserify min
gulp.task('build-browser-min',['init'], function() {
var b = browserify({hasExports: true, standalone: "biojs-vis-bioschemas-event-list"});
exposeBundles(b);
return b.bundle()
.pipe(source(outputFile + ".min.js"))
.pipe(chmod(644))
.pipe(streamify(uglify()))
.pipe(gulp.dest(buildDir));
});
gulp.task('build-browser-gzip', ['build-browser-min'], function() {
return gulp.src(outputFileMin)
.pipe(gzip({append: false, gzipOptions: { level: 9 }}))
.pipe(rename(outputFile + ".min.gz.js"))
.pipe(gulp.dest(buildDir));
});
// exposes the main package
// + checks the config whether it should expose other packages
function exposeBundles(b){
b.add("./" + packageConfig.main, {expose: packageConfig.name });
if(packageConfig.sniper !== undefined && packageConfig.sniper.exposed !== undefined){
for(var i=0; i<packageConfig.sniper.exposed.length; i++){
b.require(packageConfig.sniper.exposed[i]);
}
}
}
// watch task for browserify
// watchify has an internal cache -> subsequent builds are faster
gulp.task('watch', function() {
var util = require('gulp-util')
var b = browserify({debug: true,hasExports: true, cache: {}, packageCache: {} });
b.add("./" + packageConfig.main, {expose: packageConfig.name});
// expose other bundles
exposeBundles(b);
function rebundle(ids){
b.bundle()
.on("error", function(error) {
util.log(util.colors.red("Error: "), error);
})
.pipe(source(outputFile + ".js"))
.pipe(chmod(644))
.pipe(gulp.dest(buildDir));
}
var watcher = watchify(b);
watcher.on("update", rebundle)
.on("log", function(message) {
util.log("Refreshed:", message);
});
return rebundle();
});