forked from bilibili/flv.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
130 lines (111 loc) · 3.56 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
/*
* Copyright (C) 2016 Bilibili. All Rights Reserved.
*
* @author zheng qian <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const del = require('del');
const gulp = require('gulp');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
const eslint = require('gulp-eslint');
const sourcemaps = require('gulp-sourcemaps');
const babelify = require('babelify');
const browserify = require('browserify');
const watchify = require('watchify');
const buffer = require('vinyl-buffer');
const source = require('vinyl-source-stream');
function doWatchify() {
let customOpts = {
entries: 'src/index.js',
standalone: 'flvjs',
debug: true,
transform: ['babelify', 'browserify-versionify'],
plugin: ['browserify-derequire']
};
let opts = Object.assign({}, watchify.args, customOpts);
let b = watchify(browserify(opts));
b.on('update', doBundle.bind(global, b));
b.on('log', console.log.bind(console));
return b;
}
function doBundle(b) {
return b.bundle()
.on('error', console.error.bind(console))
.pipe(source('flv.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/'));
}
function doLint(paths, exit) {
return gulp.src(paths)
.pipe(eslint())
.pipe(eslint.format())
.pipe(exit ? eslint.failAfterError() : eslint.result(function () {}));
}
gulp.task('default', ['clean', 'lint', 'build']);
gulp.task('release', ['clean', 'lint', 'build', 'minimize']);
gulp.task('watch', ['clean'], function () {
let gulpWatcher = gulp.watch(['gulpfile.js', 'src/**/*.js']);
gulpWatcher.on('change', function (e) {
if (e.type === 'changed' || e.type === 'added') {
return doLint(e.path, false);
}
});
return doBundle(doWatchify());
});
gulp.task('clean', function () {
return del([
'dist/*'
]);
});
gulp.task('lint', function () {
return doLint(['gulpfile.js', 'src/**/*.js'], true);
});
gulp.task('build', ['clean', 'lint'], function () {
let b = browserify({
entries: 'src/index.js',
standalone: 'flvjs',
debug: true,
transform: ['babelify', 'browserify-versionify'],
plugin: ['browserify-derequire']
});
return doBundle(b);
});
gulp.task('minimize', ['lint', 'build'], function () {
let options = {
sourceMap: true,
sourceMapIncludeSources: true,
sourceMapRoot: './src/',
mangle: true,
compress: {
sequences: true,
dead_code: true,
conditionals: true,
booleans: true,
unused: true,
if_return: true,
join_vars: true
}
};
return gulp.src('dist/flv.js')
.pipe(rename({extname: '.min.js'}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify(options))
.on('error', console.error.bind(console))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/'));
});