forked from ianmcgregor/assets-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
129 lines (111 loc) · 2.93 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
'use strict';
var browserify = require('browserify'),
browserSync = require('browser-sync'),
buffer = require('vinyl-buffer'),
chalk = require('chalk'),
collapse = require('bundle-collapser/plugin'),
derequire = require('gulp-derequire'),
exorcist = require('exorcist'),
gulp = require('gulp'),
jshint = require('gulp-jshint'),
rename = require('gulp-rename'),
source = require('vinyl-source-stream'),
strip = require('gulp-strip-debug'),
uglify = require('gulp-uglify'),
watchify = require('watchify');
var standaloneName = 'assetsLoader',
entryFileName = 'index.js',
bundleFileName = 'assets-loader.js';
// log
function logError(msg) {
console.log(chalk.bold.red('[ERROR] ' + msg.toString()));
}
// bundler
var bundler = watchify(browserify({
entries: ['./src/' + entryFileName],
standalone: standaloneName,
debug: true
}, watchify.args));
function bundle() {
return bundler
.bundle()
.pipe(exorcist('./dist/' + bundleFileName + '.map'))
.on('error', logError)
.pipe(source(bundleFileName))
.pipe(buffer())
.pipe(derequire())
.pipe(gulp.dest('./dist/'))
.pipe(rename({ extname: '.min.js' }))
.pipe(strip())
.pipe(uglify())
.pipe(gulp.dest('./dist/'));
}
bundler.on('update', bundle); // on any dep update, runs the bundler
gulp.task('bundle', ['jshint'], bundle);
// release bundle with extra compression (can't get collapse to work with watchify)
function bundleRelease(minify) {
var bundler = browserify({
entries: ['./src/' + entryFileName],
standalone: standaloneName,
debug: !minify
});
if(minify) {
bundler = bundler.plugin(collapse);
}
var stream = bundler.bundle()
.on('error', logError);
if(!minify) {
stream = stream.pipe(exorcist('./dist/' + bundleFileName + '.map'));
}
stream = stream.pipe(source(bundleFileName))
.pipe(buffer())
.pipe(derequire())
.pipe(strip());
if(minify) {
return stream.pipe(rename({extname: '.min.js'}))
.pipe(uglify())
.pipe(gulp.dest('./dist/'));
} else {
return stream.pipe(gulp.dest('./dist/'));
}
}
gulp.task('release', function() {
bundleRelease(true);
bundleRelease(false);
});
// connect browsers
gulp.task('connect', function() {
browserSync.init({
server: {
baseDir: ['./', 'examples']
},
files: [
'dist/*',
'examples/**/*'
],
reloadDebounce: 500
});
});
// reload browsers
gulp.task('reload', function() {
browserSync.reload();
});
// js hint
gulp.task('jshint', function() {
return gulp.src([
'./gulpfile.js',
'src/**/*.js',
'test/**/*.js',
'examples/**/*.js',
'!examples/js/highlight.pack.js'
])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
});
// watch
gulp.task('watch', function() {
gulp.watch('test/**/*.js', ['jshint']);
gulp.watch('examples/**/*.js', ['jshint']);
});
// default
gulp.task('default', ['connect', 'watch', 'bundle']);