/
gulpfile.js
144 lines (116 loc) · 3.81 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
//
// system-level requires
//
var exec = require('child_process').exec;
var path = require('path');
//
// gulp-specific tools
//
var gulp = require('gulp');
var concat = require('gulp-concat');
var vsource = require('vinyl-source-stream');
var streamify = require('gulp-streamify');
var jshint = require('gulp-jshint');
var livereload = require('gulp-livereload');
var gutil = require('gulp-util');
var header = require('gulp-header');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
//
// testing/packaging
//
var karma = require('karma');
var browserify = require('browserify');
var pkg = require('./package.json');
//
// config for the web server used to serve examples
//
var SERVERPORT = 8080;
var SOURCEGLOB = './src/**/*.js';
var EXAMPLESGLOB = './examples/**/*.js';
//
// BUILDFILExxx is the file name,
// BUILDPATHxxx is the full path: <BUILDDIR>/<BUILDFILExxx>
//
var BUILDDIR = 'build';
var BUILDFILENAME = 'react-webaudio';
var MODULENAME = 'react-webaudio';
var BUILDFILEDEV = BUILDFILENAME + '.js';
var BUILDPATHDEV = path.join(BUILDDIR, BUILDFILEDEV);
var BUILDFILEMIN = BUILDFILENAME + '.min.js';
var BUILDPATHMIN = path.join(BUILDDIR, BUILDFILEMIN);
var banner = ['/**',
' * <%= pkg.name %>',
' * @version <%= pkg.version %>',
' * @license <%= pkg.license %>',
' */',
''].join('\n');
var browserlist = ['Firefox'];
var karmaconfiguration = {
browsers: browserlist,
files: [
BUILDPATHDEV,
],
frameworks:['jasmine'],
singleRun:true
};
function errorHandler(err) {
gutil.log(err);
this.emit('end'); // so that gulp knows the task is done
}
gulp.task('help', function() {
console.log('Possible tasks:');
console.log('"default" - compile ' + pkg.name + ' into ' + BUILDPATHDEV);
console.log('"watch" - watch ' + pkg.name + ' source files and rebuild');
console.log('"test" - run tests in test directory');
console.log('"livereload" - compile and launch web server/reload server');
});
gulp.task('lint', function() {
return gulp.src([SOURCEGLOB,EXAMPLESGLOB])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(livereload());
});
gulp.task('browserify',['lint'], function() {
var bundler = browserify();
bundler.require('react');
bundler.require('./src/ReactWebAudio.js',{expose:MODULENAME});
return bundler.bundle().on('error', errorHandler)
.pipe(vsource('react-webaudio-commonjs.js'))
.pipe(gulp.dest('build'));
});
gulp.task('bundle', ['browserify'], function() {
// If we're running a gulp.watch and browserify finds and error, it will
// throw an exception and terminate gulp unless we catch the error event.
return gulp.src(['build/react-webaudio-commonjs.js','src/react-webaudio-exposeglobals.js'])
.pipe(concat(BUILDFILEDEV))
.pipe(gulp.dest('build'))
// might as well compress it while we're here
.pipe(streamify(uglify({preserveComments:'some'})))
.pipe(rename(BUILDFILEMIN))
.pipe(gulp.dest(BUILDDIR))
.pipe(livereload());
});
gulp.task('watch', ['bundle'], function() {
gulp.watch(SOURCEGLOB, ['browserify']);
gulp.watch(EXAMPLESGLOB, ['lint']);
});
gulp.task('livereload', ['lint','bundle'], function() {
var nodestatic = require('node-static');
var fileserver = new nodestatic.Server('.');
require('http').createServer(function(request, response) {
request.addListener('end', function() {
fileserver.serve(request,response);
}).resume();
}).listen(SERVERPORT);
livereload.listen();
gulp.watch([SOURCEGLOB], ['bundle']);
gulp.watch([EXAMPLESGLOB], ['lint']);
});
gulp.task('test', ['bundle'], function() {
karma.server.start(karmaconfiguration, function (exitCode) {
gutil.log('Karma has exited with code ' + exitCode);
process.exit(exitCode);
});
});
gulp.task('default', ['lint','bundle']);