-
Notifications
You must be signed in to change notification settings - Fork 431
/
gulpfile.js
244 lines (220 loc) · 7.32 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
gifshot Build File
*/
// Third-party dependencies
var gulp = require('gulp'),
uglify = require('gulp-uglify'),
insert = require('gulp-insert'),
rename = require('gulp-rename'),
mocha = require('gulp-mocha'),
istanbul = require('gulp-istanbul'),
rimraf = require('gulp-rimraf'),
_ = require('lodash'),
rjs = require('requirejs'),
amdclean = require('amdclean'),
// end of Third-party dependencies
fs = require('fs'),
// The package.json object
packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')),
// The license text from LICENSE.text
licenseText = '/*' + fs.readFileSync('./LICENSE.txt', 'utf8') + '\n*/\n',
// Configurations for different dependencies
configs = {
'rjs': {
'findNestedDependencies': true,
'baseUrl': 'src/modules/',
'preserveLicenseComments': false,
'optimize': 'none',
'skipModuleInsertion': true,
'include': ['index']
},
'amdclean': {
// Will not transform conditional AMD checks - Libraries use this to provide optional AMD support
'transformAMDChecks': false,
'aggressiveOptimizations': false,
'createAnonymousAMDModule': true,
'prefixTransform': function(moduleName) {
return moduleName.substring(moduleName.indexOf('_') + 1, moduleName.length);
},
// Wraps the library in an IIFE (Immediately Invoked Function Expression)
'wrap': {
'start': ';(function(window, document, navigator, undefined) {\n',
'end': '\n}(typeof window !== "undefined" ? window : {}, typeof document !== "undefined" ? document : { createElement: function() {} }, typeof window !== "undefined" ? window.navigator : {}));'
},
'escodegen': {
'comment': false
}
},
'jshint': {
'loopfunc': true
}
},
// Which gifshot modules to remove for each custom build
customBuild = {
'webcam': {
'modulesToRemove': [
'isExistingImagesGIFSupported',
'isExistingVideoGIFSupported',
'existingImages',
'existingVideo'
]
},
'image': {
'modulesToRemove': [
'isWebCamGIFSupported',
'isExistingVideoGIFSupported',
'stopVideoStreaming',
'existingVideo',
'existingWebcam',
'videoStream'
]
},
'video': {
'modulesToRemove': [
'isExistingImagesGIFSupported',
'isWebCamGIFSupported',
'stopVideoStreaming',
'existingImages',
'existingWebcam'
]
}
},
// Get's all of the command line arguments that start with a --
getCommandLineArguments = function() {
var args = [];
process.argv.forEach(function(arg, iterator) {
if (arg.charAt(0) === '-' && arg.charAt(1) === '-') {
args.push(arg.substring(2, arg.length));
}
});
return args;
};
// Task that uses the Require.js optimizer to concatenate all of the gifshot
// AMD modules into a single source file
gulp.task('concat', function(cb) {
var outputFile = 'src/gifshot.js',
rjsOptions = _.merge(_.clone(configs.rjs), {
'out': outputFile
});
rjs.optimize(rjsOptions, function() {
var amdcleanOptions = _.merge(_.clone(configs.amdclean), {
'filePath': outputFile
});
fs.writeFileSync(outputFile, amdclean.clean(amdcleanOptions));
cb(); // finished task
}, function(err) {
return cb(err); // return error
});
});
// Task that creates a customized gifshot.js file (only including modules that are testable)
// and runs the Mocha unit tests and Instanbul test coverage
gulp.task('test', ['concat'], function(cb) {
var outputFile = 'tests/gifshot.test.js',
rjsOptions = _.merge(_.clone(configs.rjs), {
'out': outputFile
});
rjs.optimize(rjsOptions, function() {
var amdcleanOptions = _.merge(_.clone(configs.amdclean), {
'filePath': outputFile,
'removeModules': [
'gifWriter',
'NeuQuant',
'AnimatedGIF',
'createAndGetGIF',
'existingImages',
'existingVideo',
'getBase64GIF',
'processFrameWorker',
'screenShot',
'videoStream'
]
});
fs.writeFileSync(outputFile, amdclean.clean(amdcleanOptions));
gulp.src(outputFile)
.pipe(istanbul()) // Covering files
.on('finish', function() {
gulp.src('tests/gifshot-tests.js')
.pipe(mocha({
reporter: 'nyan'
}))
.pipe(istanbul.writeReports()) // Creating the reports after tests runned
.on('end', cb); // finished task
});
}, function(err) {
return cb(err); // return error
});
});
// Copies src/gifshot.js to build/gifshot.js and demo/gifshot.js
gulp.task('copy', ['concat', 'test'], function() {
gulp.src(['src/gifshot.js'])
.pipe(insert.prepend(licenseText))
.pipe(gulp.dest('build'))
.pipe(gulp.dest('demo/js/dependencies'))
});
// Uglify.js task that minifies build/gifshot.js and adds gifshot.min.js to the build folder
gulp.task('minify', ['concat', 'test', 'copy'], function() {
gulp.src(['src/gifshot.js'])
.pipe(uglify())
.pipe(rename('gifshot.min.js'))
.pipe(insert.prepend(licenseText))
.pipe(gulp.dest('build'));
});
// Cleanup task that removes certain temporary files
gulp.task('cleanup', ['concat', 'test', 'copy', 'minify'], function() {
gulp.src(['src/gifshot.js', 'tests/gifshot.test.js'], {
read: false
})
.pipe(rimraf());
});
// Task that creates the custom gifshot build and adds it to the custom/build directory
gulp.task('custom-build', function() {
var customBuildModules = getCommandLineArguments(),
modulesToRemove = [],
outputFile = 'build/custom/gifshot.custom.js',
rjsOptions;
// If no custom builds are specified, default to the webcam custom build
if (!customBuildModules.length) {
customBuildModules.push('webcam');
}
// Find all of the modules to remove
customBuildModules.forEach(function(currentModule) {
// If the custom build paramater is an existing custom build, remove all associated files
if (customBuild[currentModule] && customBuild[currentModule].modulesToRemove) {
customBuild[currentModule].modulesToRemove.forEach(function(currentModule) {
modulesToRemove.push(currentModule);
});
} else {
// Removes the associated module
modulesToRemove.push(currentModule);
}
});
rjsOptions = _.merge(_.clone(configs.rjs), {
'out': outputFile
});
rjs.optimize(rjsOptions, function() {
var amdcleanOptions = _.merge(_.clone(configs.amdclean), {
'filePath': outputFile,
'removeModules': modulesToRemove
});
fs.writeFileSync(outputFile, amdclean.clean(amdcleanOptions));
gulp.src(outputFile)
.pipe(insert.prepend(licenseText))
.pipe(gulp.dest('demo'))
.pipe(uglify())
.pipe(rename('gifshot.custom.min.js'))
.pipe(insert.prepend(licenseText))
.pipe(gulp.dest('build/custom'));
cb(); // finished task
}, function(err) {
return cb(err); // return error
});
});
// The default build task (called when you run `gulp`)
gulp.task('default', ['concat', 'test', 'copy', 'minify', 'cleanup']);
// The watch task that runs the default task on any gifshot module file changes
gulp.task('watch', function() {
var watcher = gulp.watch('src/modules/**/*.js', ['default']);
watcher.on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
});