-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
102 lines (87 loc) · 2.49 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
/* eslint-disable require-jsdoc */
// Build automation
// Require sudo npm install -g gulp
// For dev, initiate watch by executing either `gulp` or `gulp watch`
const gulp = require('gulp');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const uglify = require('gulp-uglify');
const shell = require('gulp-shell');
const rename = require('gulp-rename');
const _ = require('underscore');
const listFiles = require('file-lister');
const { exec } = require('child_process');
const webserver = require('gulp-webserver');
const babelify = require('babelify');
const pathJs = {
originalJs: ['./js/'],
};
// Dependency task
gulp.task('init_module', (callback) => {
exec('git submodule update --init && cd html5-common && npm install && cd ..', (err) => {
if (err) return callback(err);
callback();
return null;
});
});
const checkFileExtension = function (extension, fileName) {
if (!fileName || fileName.length < extension.length) {
return false;
}
return (fileName.lastIndexOf(extension) === fileName.length - extension.length);
};
const getFileNameFromPath = function (path) {
const start = path.lastIndexOf('/') + 1;
return path.substring(start);
};
const browserifyFn = function () {
const bundleThis = function (srcArray) {
_.each(srcArray, (sourceFile) => {
const b = browserify({
entries: sourceFile,
debug: false,
transform: [babelify],
});
b.bundle()
.pipe(source(getFileNameFromPath(sourceFile)))
.pipe(buffer())
.pipe(gulp.dest('./build/'))
.pipe(uglify())
.pipe(rename({
extname: '.min.js',
}))
.pipe(gulp.dest('./build/'));
});
};
listFiles(pathJs.originalJs, function (error, files) {
if (error) {
OO.log(error);
} else {
const filteredList = files.filter(_.bind(checkFileExtension, this, '.js'));
bundleThis(filteredList);
}
});
};
// Build All
gulp.task('build', ['init_module'], () => {
browserifyFn();
});
// Run tests
gulp.task('test', shell.task(['make test']));
// Initiate a watch
gulp.task('watch', () => {
gulp.watch('js/**/*', ['build']);
});
// The default task (called when you run `gulp` from cli)
gulp.task('default', ['build']);
// Run as webserver for debugging purpose
gulp.task('webserver', () => {
gulp.src('.')
.pipe(webserver({
livereload: true,
directoryListing: true,
open: true,
port: 9003,
}));
});