forked from jakearchibald/idb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
121 lines (102 loc) · 2.89 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
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var runSequence = require('run-sequence');
var watchify = require('watchify');
var browserify = require('browserify');
var uglifyify = require('uglifyify');
var mergeStream = require('merge-stream');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var babelify = require('babelify');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
gulp.task('clean', function (done) {
require('del')(['dist'], done);
});
gulp.task('copy-lib', function() {
return gulp.src([
'lib/idb.js'
]).pipe(gulp.dest('dist'))
.pipe(reload({stream: true}));
});
gulp.task('copy', function() {
return gulp.src([
'test/index.html',
'node_modules/mocha/mocha.css',
'node_modules/mocha/mocha.js'
]).pipe(gulp.dest('dist/test'))
.pipe(reload({stream: true}));
});
function createBundler(src) {
var b;
if (plugins.util.env.production) {
b = browserify();
}
else {
b = browserify({
cache: {}, packageCache: {}, fullPaths: true,
debug: true
});
}
b.transform(babelify.configure({
stage: 1
}));
if (plugins.util.env.production) {
b.transform({
global: true
}, 'uglifyify');
}
b.add(src);
return b;
}
function bundle(bundler, outputPath) {
var splitPath = outputPath.split('/');
var outputFile = splitPath[splitPath.length - 1];
var outputDir = splitPath.slice(0, -1).join('/');
return bundler.bundle()
// log errors if they happen
.on('error', plugins.util.log.bind(plugins.util, 'Browserify Error'))
.pipe(source(outputFile))
.pipe(buffer())
.pipe(plugins.sourcemaps.init({ loadMaps: true })) // loads map from browserify file
.pipe(plugins.sourcemaps.write('./')) // writes .map file
.pipe(plugins.size({ gzip: true, title: outputFile }))
.pipe(gulp.dest('dist/' + outputDir))
.pipe(reload({ stream: true }));
}
var bundlers = {
'test/idb.js': createBundler('./test/idb.js')
};
gulp.task('js', function () {
return mergeStream.apply(null,
Object.keys(bundlers).map(function(key) {
return bundle(bundlers[key], key);
})
);
});
gulp.task('browser-sync', function() {
browserSync({
notify: false,
port: 8000,
server: "dist",
open: false
});
});
gulp.task('watch', function () {
gulp.watch(['test/index.html'], ['copy']);
gulp.watch(['lib/idb.js'], ['copy-lib']);
Object.keys(bundlers).forEach(function(key) {
var watchifyBundler = watchify(bundlers[key]);
watchifyBundler.on('update', function() {
return bundle(watchifyBundler, key);
});
bundle(watchifyBundler, key);
});
});
gulp.task('build', function (done) {
runSequence('clean', ['copy', 'js', 'copy-lib'], done);
});
gulp.task('default', ['build']);
gulp.task('serve', function (done) {
runSequence('clean', ['copy', 'js', 'copy-lib'], ['browser-sync', 'watch'], done);
});