-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
117 lines (115 loc) · 3.41 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
var gulp = require('gulp'),
sass = require('gulp-sass'),
plumber = require('gulp-plumber'),
autoprefix = require('gulp-autoprefixer'),
rename = require('gulp-rename'),
resolveDependencies = require('gulp-resolve-dependencies'),
concat = require('gulp-concat'),
jsHint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imageMin = require('gulp-imagemin'),
cache = require('gulp-cache'),
del = require('del'),
browserSync = require('browser-sync').create(),
// source and distribution folder
source = 'develope/',
dest = 'dist/',
// Bootstrap scss source
bsRoot = { in : './node_modules/bootstrap/' },
// css source file: .scss files
scss = { in : source + 'scss/main.scss',
out: dest + 'css/',
watch: source + 'scss/**/*',
sassOpts: {
outputStyle: 'expanded',
precison: 3,
errLogToConsole: true,
includePaths: [bsRoot.in + 'scss']
}
},
js = { in : source + 'js/main.js',
out: dest + 'js/',
watch: source + 'js/**/*',
pattern: /\*= require [\s-]*(.*\.js)/g
},
img = { in : source + 'img/*',
out: dest + 'img/',
watch: source + 'img/*',
};
// compile html
gulp.task('html', function() {
return gulp
.src(source + '*.html')
.pipe(gulp.dest(dest));
});
// compile styles
gulp.task('styles', function() {
return gulp
.src([scss.in])
.pipe(plumber({
errorHandler: function(error) {
console.log(error.message);
this.emit('end');
}
}))
.pipe(sass(scss.sassOpts))
.pipe(autoprefix())
.pipe(gulp.dest(scss.out))
.pipe(rename({ suffix: '.min' }))
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(gulp.dest(scss.out))
.pipe(browserSync.reload({ stream: true }));
});
// compile scripts
gulp.task('scripts', function() {
gulp.src([js.in])
.pipe(resolveDependencies({
pattern: js.pattern
}))
.on('error', function(err) {
console.log(err.message);
})
.pipe(concat('main.js'))
.pipe(plumber({
errorHandler: function(error) {
console.log(error.message);
this.emit('end');
}
}))
.pipe(jsHint())
.pipe(jsHint.reporter('default'))
.pipe(gulp.dest(js.out))
.pipe(rename({ suffix: '.min' }))
.pipe(uglify())
.pipe(gulp.dest(js.out))
.pipe(browserSync.reload({ stream: true }));
});
// compile images
gulp.task('images', function() {
return gulp
.src([img.in])
.pipe(cache(imageMin({ optimizationLevel: 5, progressive: true, interlaced: true })))
.pipe(gulp.dest(img.out))
.pipe(browserSync.reload({ stream: true }));
});
// run server
gulp.task('serve', function() {
browserSync.init({
notify: true,
server: {
baseDir: 'dist/'
},
'browser': ['chrome']
});
});
gulp.task('clean', function() {
return del(['dist/**']);
});
// default task
gulp.task('default', ['html', 'styles', 'scripts', 'images'], function() {
gulp.watch(source + '*.html', ['html']).on('change', browserSync.reload);
gulp.watch(scss.watch, ['styles']);
gulp.watch(js.watch, ['scripts']);
gulp.watch(img.watch, ['images']);
gulp.start('serve');
});