forked from samwx/html-skeleton-gulp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
141 lines (118 loc) · 2.84 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
// Load gulp plugins with 'require' function of nodejs
var gulp = require('gulp'),
plumber = require('gulp-plumber'),
gutil = require('gulp-util'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
rename = require('gulp-rename'),
minifyCSS = require('gulp-minify-css'),
less = require('gulp-less'),
path = require('path'),
livereload = require('gulp-livereload'),
browserSync = require('browser-sync');
// Handle less error
var onError = function (err) {
gutil.beep();
console.log(err);
};
// Path configs
var css_files = 'assets/css/*.css', // .css files
css_path = 'assets/css', // .css path
js_files = 'assets/js/*.js', // .js files
less_file = 'assets/less/style.less', // .less files
dist_path = 'assets/dist';
//Extension config
var extension = 'html';
/***** Functions for tasks *****/
function js() {
return gulp.src(js_files)
.pipe(plumber({
errorHandler: onError
}))
.pipe(concat('dist'))
.pipe(rename('concat.min.js'))
.pipe(uglify())
.pipe(gulp.dest(dist_path))
.pipe(livereload());
}
function css() {
return gulp.src(css_files)
.pipe(concat('dist'))
.pipe(rename('all.min.css'))
.pipe(minifyCSS({keepBreaks:true}))
.pipe(gulp.dest(dist_path))
.pipe(livereload());
}
function lessTask(err) {
return gulp.src(less_file)
.pipe(plumber({
errorHandler: onError
}))
.pipe(less({ paths: [ path.join(__dirname, 'less', 'includes') ] }))
.pipe(gulp.dest(css_path))
.pipe(livereload());
}
function reloadBrowser() {
return gulp.src('*.' + extension)
.pipe(livereload());
}
function browserSync() {
return browserSync({
server: {
baseDir: "./"
}
//proxy: "localhost/html-skeleton-gulp/"
});
}
// The 'Browser Sync' task
gulp.task('browser-sync', function() {
return browserSync();
});
// The 'js' task
gulp.task('js', function() {
return js();
});
// The 'css' task
gulp.task('css', function(){
return css();
});
// The 'less' task
gulp.task('less', function(){
return lessTask();
});
// Reload browser when have *.html changes
gulp.task('reload-browser', function() {
return reloadBrowser();
});
// The 'default' task.
gulp.task('default', function() {
livereload.listen();
//browserSync();
browserSync({
server: {
baseDir: "./"
}
//proxy: "localhost/html-skeleton-gulp/"
});
gulp.watch('*.' + extension, function(){
browserSync.reload();
});
gulp.watch(less_file, function() {
// if (err) return console.log(err);
return lessTask();
});
gulp.watch(css_files, function() {
console.log('CSS task completed!');
browserSync.reload();
return css();
});
gulp.watch(js_files, function() {
console.log('JS task completed!');
browserSync.reload();
return js();
});
gulp.watch('*.' + extension, function(){
console.log('Browse reloaded!');
return reloadBrowser();
});
});