-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
129 lines (105 loc) · 2.91 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 gulp = require('gulp');
var del = require('del');
// Load plugins
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream'),
sourceFile = './app/scripts/main.js',
destFolder = './dist/scripts',
destFileName = 'app.js';
var lessFile = './app/styles/main.less',
destLessFolder = './dist/styles',
destLessFile = 'main.css';
var browserSync = require('browser-sync');
var reload = browserSync.reload;
// Styles
gulp.task('styles',['clean','less']);
gulp.task('less', function() {
return gulp.src([lessFile])
.pipe($.less({
compress: false
}).on('error', function(err){
$.util.log(err);
this.emit('end');
}))
.pipe($.rename(destLessFile))
.pipe(gulp.dest(destLessFolder))
.pipe($.rename('main.min.css'))
.pipe(gulp.dest(destLessFolder));
});
var bundler = watchify(browserify({
entries: [sourceFile],
debug: true,
insertGlobals: true,
cache: {},
packageCache: {},
fullPaths: true
}));
bundler.on('update', rebundle);
bundler.on('log', $.util.log);
function rebundle() {
return bundler.bundle()
// log errors if they happen
.on('error', $.util.log.bind($.util, 'Browserify Error'))
.pipe(source(destFileName))
.pipe(gulp.dest(destFolder))
.on('end', function() {
reload();
});
}
// Scripts
gulp.task('scripts', rebundle);
gulp.task('buildScripts', function() {
return browserify(sourceFile)
.bundle()
.pipe(source(destFileName))
.pipe(gulp.dest('dist/scripts'));
});
// HTML
gulp.task('html', function() {
return gulp.src('app/*.html')
.pipe($.size());
});
// Images
gulp.task('images', function() {
return gulp.src('app/images/**/*')
.pipe(gulp.dest('dist/images'))
.pipe($.size());
});
// Clean
gulp.task('clean', function(cb) {
$.cache.clearAll();
cb(del.sync(['dist/styles', 'dist/scripts']));
});
// Bundle
gulp.task('bundle', ['styles', 'scripts'], function() {
return gulp.src('./app/*.html')
.pipe(gulp.dest('dist'));
});
// BuildBundle
gulp.task('buildBundle', ['styles', 'buildScripts'], function() {
return gulp.src('./app/*.html')
.pipe(gulp.dest('dist'));
});
// Watch
gulp.task('watch', ['html', 'bundle'], function() {
browserSync({
notify: false,
logPrefix: 'BS',
server: ['dist', 'app']
});
// Watch .html files
gulp.watch('app/*.html', ['html']);
gulp.watch(['app/styles/**/*.less', 'app/styles/**/*.css'], ['styles', 'scripts','images', reload]);
});
// Build
gulp.task('build', ['html', 'buildBundle','images'], function() {
gulp.src('dist/scripts/app.js')
.pipe($.uglify())
.pipe($.stripDebug())
.pipe(gulp.dest('dist/scripts'));
});
// Default task
gulp.task('default', ['clean', 'build']);