forked from yanwsh/videojs-panorama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
185 lines (164 loc) · 5.06 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
/**
* Created by yanwsh on 4/3/16.
*/
'use strict';
var fs = require('fs');
var stream = require('stream');
var babelify = require('babelify');
var browserify = require('browserify');
var versionify = require('browserify-versionify');
var del = require('del');
var gulp = require('gulp');
var sass = require('gulp-sass');
var cssnano = require('gulp-cssnano');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var runSequence = require('run-sequence');
var path = require('path');
var mergeStream = require('merge-stream');
var buffer = require('vinyl-buffer');
var source = require('vinyl-source-stream');
var lazypipe = require('lazypipe');
var browserSync = require('browser-sync').create();
var rollupBabelLibBundler = require('rollup-babel-lib-bundler');
var babel = require('rollup-plugin-babel');
var json = require('rollup-plugin-json');
var nodeResolve = require('rollup-plugin-node-resolve');
var commonJS = require('rollup-plugin-commonjs');
var config = {
distPath: './dist',
versions: [4, 5]
};
gulp.task('build', function (done) {
runSequence(
'clean',
'build-script',
'build-script-es6',
'build-styles',
function (error) {
if(error){
console.log(error.message);
}
done(error);
}
)
});
gulp.task('clean', function (done) {
del.sync(['dist'], {force: true});
done();
});
function buildProdJs(destPath) {
var process = lazypipe()
.pipe(buffer)
.pipe(uglify)
.pipe(rename, {suffix: '.min'})
.pipe(gulp.dest, destPath);
return process();
}
gulp.task('build-script', function () {
var buildProcesses = config.versions.map(function (version) {
var fileName = 'plugin_v' + version + ".js";
var entryFile = path.join('src/scripts', fileName);
var destPath = config.distPath;
return browserify({
entries: entryFile,
debug: true
})
.transform(versionify, {
placeholder: '__VERSION__'
})
.transform(babelify, {
presets: ['es2015'],
sourceMaps: true
})
.bundle()
.pipe(source(fileName))
.pipe(rename('videojs-panorama.v' + version + '.js'))
.pipe(gulp.dest(destPath))
.pipe(buildProdJs(destPath));
});
return mergeStream.apply(this, buildProcesses);
});
gulp.task('build-script-es6', function(done){
var fileName = 'plugin_es6.js';
var entryFile = path.join('src/scripts', fileName);
var destPath = config.distPath;
var promise = rollupBabelLibBundler({
name: 'videojs-panorama',
dest: destPath,
entry: entryFile,
format: ['es6', 'cjs'],
postfix: {es6: '.es6', cjs: '.common'},
plugins: [
babel({
"presets": ["es2015-rollup"],
babelrc: false
}),
json(),
nodeResolve({
main: true,
jsnext: true,
extensions: ['.js', '.json'],
preferBuiltins: false,
skip: ['video.js', 'three']
}),
commonJS()
]
});
promise.then(function(){
setTimeout(function () {
var outputFiles = ['videojs-panorama.es6.js', 'videojs-panorama.common.js'];
outputFiles.forEach(function (file) {
var outputFile = path.join(destPath, file);
var buffer = fs.readFileSync(outputFile);
var bufferStream = new stream.PassThrough();
var wstream = fs.createWriteStream(outputFile);
bufferStream.end(buffer);
bufferStream.pipe(
versionify(outputFile,{
placeholder: '__VERSION__',
version: require('./package.json').version
})
).pipe(
wstream
);
});
done();
}, 500);
}).catch(function(error){
console.log(error);
done();
});
});
function buildProdCss(destPath) {
var process = lazypipe()
.pipe(buffer)
.pipe(cssnano)
.pipe(rename, {suffix: '.min'})
.pipe(gulp.dest, destPath);
return process();
}
gulp.task('build-styles', function () {
var fileName = 'plugin.scss';
var entryFile = path.join('src/styles', fileName);
var destPath = config.distPath;
return gulp.src(entryFile)
.pipe(sass())
.pipe(rename('videojs-panorama.css'))
.pipe(gulp.dest(destPath))
.pipe(buildProdCss(destPath));
});
gulp.task('browser-sync', ['build'], function() {
browserSync.init({
server: {
baseDir: "./"
},
open: 'ui',
online: true,
});
});
gulp.task('watch', ['browser-sync'], function () {
gulp.watch('src/scripts/**/*.js', ['build-script']);
gulp.watch('src/styles/**/*.scss', ['build-styles']);
});
gulp.task('default', ['watch']);