-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
164 lines (133 loc) · 4.28 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
var gulp = require('gulp');
// browser builds
var browserify = require('browserify');
var watchify = require('watchify')
var uglify = require('gulp-uglify');
// testing
var mocha = require('gulp-mocha');
var mochaPhantomJS = require('gulp-mocha-phantomjs');
// code style
var jshint = require('gulp-jshint');
var coveralls = require('gulp-coveralls');
// gulp helper
var source = require('vinyl-source-stream'); // converts node streams into vinyl streams
var gzip = require('gulp-gzip');
var clean = require('gulp-rimraf');
var rename = require('gulp-rename');
var chmod = require('gulp-chmod');
var streamify = require('gulp-streamify'); // converts streams into buffers (legacy support for old plugins)
var watch = require('gulp-watch');
// path tools
var fs = require('fs');
var path = require('path');
var join = path.join;
var mkdirp = require('mkdirp');
// browserify build config
var buildDir = "build";
var packageConfig = require('./package.json');
var outputFile = "viewer";
var outputFileMin = join(buildDir,outputFile + ".min.js");
// a failing test breaks the whole build chain
gulp.task('build', ['build-browser', 'build-browser-gzip']);
gulp.task('default', ['lint', 'test', 'coveralls', 'build']);
gulp.task('lint', function() {
return gulp.src('./lib/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('test', ['test-unit', 'test-dom']);
gulp.task('test-unit', function () {
return gulp.src('./test/unit/**/*.js', {read: false})
.pipe(mocha({reporter: 'spec',
useColors: false}));
});
gulp.task('test-dom', ["build-test"], function () {
return gulp
.src('test/index.html')
.pipe(mochaPhantomJS());
});
// browserify debug
gulp.task('build-test',['init'], function() {
var b = browserify({debug: true});
b.add('./test/dom/index');
return b.bundle()
.pipe(source("test.js"))
.pipe(chmod(644))
.pipe(gulp.dest(buildDir));
});
gulp.task('coveralls', function () {
return gulp.src('coverage/lcov.info')
.pipe(coveralls());
});
gulp.task('test-watch', function() {
gulp.watch(['./src/**/*.js','./lib/**/*.js', './test/**/*.js'], function() {
gulp.run('test');
});
});
// will remove everything in build
gulp.task('clean', function() {
return gulp.src(buildDir).pipe(clean());
});
// 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: "oo"});
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('./index.js', {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('./index.js', {expose: packageConfig.name});
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();
});