forked from lvl99/jquery-lvl99-nicescreen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
158 lines (142 loc) · 4.37 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
var gulp = require('gulp'),
gutil = require('gulp-util'),
browserify = require('browserify'),
browsersync = require('browser-sync'),
buffer = require('vinyl-buffer'),
concat = require('gulp-concat'),
del = require('del'),
less = require('gulp-less'),
modernizr = require('gulp-modernizr'),
prefix = require('gulp-autoprefixer'),
rename = require('gulp-rename'),
source = require('vinyl-source-stream'),
sourcemaps = require('gulp-sourcemaps'),
transform = require('vinyl-transform'),
uglify = require('gulp-uglify'),
uglifycss = require('gulp-uglifycss'),
watch = require('gulp-watch'),
watchify = require('watchify');
// Fix gulp's error handling
// See: https://github.com/gulpjs/gulp/issues/71
var origSrc = gulp.src;
gulp.src = function () {
return fixPipe(origSrc.apply(this, arguments));
};
function fixPipe(stream) {
var origPipe = stream.pipe;
stream.pipe = function (dest) {
arguments[0] = dest.on('error', function (error) {
var nextStreams = dest._nextStreams;
if (nextStreams) {
nextStreams.forEach(function (nextStream) {
nextStream.emit('error', error);
});
} else if (dest.listeners('error').length === 1) {
throw error;
}
});
var nextStream = fixPipe(origPipe.apply(this, arguments));
(this._nextStreams || (this._nextStreams = [])).push(nextStream);
return nextStream;
};
return stream;
}
/*
* Task recipes
*/
gulp.task('default', ['build', 'watch']);
gulp.task('watch', ['browsersync', 'watchfiles']);
gulp.task('build', ['copy', 'less', 'modernizr', 'main']);
gulp.task('compress', ['compresscss', 'compressjs']);
gulp.task('production', ['build', 'compress']);
/*
* JS Bundler
*/
function Bundler( src, file, dest ) {
var self = this;
// Properties
self.src = src || './src/js';
self.file = file || 'jquery-lvl99-nicescreen.js';
self.dest = dest || './dist';
self.watchify = watchify( browserify( self.src + '/' + self.file, watchify.args ) );
// Method
self.bundle = function () {
return self.watchify.bundle()
.on( 'error', gutil.log.bind( gutil, 'Browserify Error' ) )
.pipe( source( self.file ) )
// Sourcemaps
.pipe( buffer() )
.pipe( sourcemaps.init({ loadMaps: true }) )
.pipe( sourcemaps.write('./') )
//
.pipe( rename('jquery-lvl99-nicescreen.js') )
.pipe( gulp.dest(self.dest) );
};
// Events
self.watchify.on( 'update', self.bundle );
self.watchify.on( 'log', gutil.log );
}
// JavaScript bundlers
var app = new Bundler();
gulp.task('main', app.bundle);
/*
* Tasks
*/
// HTML documentation
gulp.task( 'copy', function () {
return gulp.src( ['./src/demos/**/*'])
.pipe( gulp.dest('./dist/demos') );
});
// Modernizr
gulp.task( 'modernizr', function () {
return gulp.src( ['./dist/jquery-lvl99-nicescreen.css',
'./src/js/jquery-lvl99-nicescreen.js'] )
.pipe( modernizr({
options: ["setClasses",
"addTest",
"html5printshiv",
"testProp",
"fnBind"]
}) )
.pipe( gulp.dest('./src/js') );
});
// LESS
gulp.task('less', function () {
return gulp.src(['./src/css/jquery-lvl99-nicescreen.less',
'./src/demos/**/*.less'])
.pipe( less() )
.pipe( prefix({
browsers: [ 'last 2 versions', '> 1%' ],
cascade: true
}) )
.pipe( gulp.dest('./dist') );
});
// Compress for production
gulp.task('compressjs', ['main'], function () {
return gulp.src('./dist/jquery-lvl99-nicescreen.js')
.pipe( uglify() )
.pipe( rename({
extname: '.min.js'
}) )
.pipe( gulp.dest('./dist') );
});
gulp.task('compresscss', ['less'], function () {
return gulp.src('./dist/jquery-lvl99-nicescreen.css')
.pipe( uglifycss() )
.pipe( rename({
extname: '.min.css'
}) )
.pipe( gulp.dest('./dist') );
});
// Watch Files
gulp.task('watchfiles', ['browsersync'], function () {
var watchcss = gulp.watch( './src/**/*.less', ['less', browsersync.reload] );
var watchjs = gulp.watch( './src/**/*.js', ['main', browsersync.reload] );
var watchdemos = gulp.watch( './src/demos/**/*', ['copy', browsersync.reload] );
});
// Web Server
gulp.task('browsersync', function() {
browsersync.init({
server: './dist'
});
});