-
Notifications
You must be signed in to change notification settings - Fork 22
/
gulpfile.js
134 lines (117 loc) · 3.38 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
// common
const gulp = require('gulp');
const rename = require('gulp-rename');
const plumber = require('gulp-plumber');
const log = require('fancy-log');
const beeper = require('beeper');
// css
const less = require('gulp-less');
const postcss = require('gulp-postcss');
const postcssNormalize = require('postcss-normalize');
const autoprefixer = require('autoprefixer');
const cleanCss = require('gulp-clean-css');
// images
const del = require('del');
const path = require('path');
const cache = require('gulp-cache');
const imagemin = require('gulp-imagemin');
const mozjpeg = require('imagemin-mozjpeg');
const jpegtran = require('imagemin-jpegtran');
//const pngquant = require('imagemin-pngquant');
let paths = {
src: {
less: 'assets/less/*.less',
img: ['assets/img/**/*.{png,jpg,gif,svg}'],
},
dest: {
css: 'static/css/',
img: 'static/img/',
},
watch: {
less: 'assets/less/**/*.less',
},
cache: {
tmpDir: 'tmp/',
cacheDirName: 'gulp-cache',
},
};
// LESS
gulp.task('less', function() {
return gulp.src(paths.src.less)
.pipe(plumber({errorHandler: onError}))
.pipe(less())
.pipe(postcss([
autoprefixer({cascade: false}),
postcssNormalize({forceImport: true}),
]))
.pipe(cleanCss({
level: {
1: {},
2: {
removeUnusedAtRules: true,
},
},
}))
.pipe(rename({
suffix: '.min',
}))
.pipe(gulp.dest(paths.dest.css));
});
// IMG
gulp.task('imagemin', function() {
return gulp.src(paths.src.img)
.pipe(plumber({errorHandler: onError}))
.pipe(cache(
imagemin([
imagemin.gifsicle({interlaced: true}),
mozjpeg({quality: 90}),
jpegtran({progressive: true}),
//pngquant(),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: false}]}),
], {
verbose: true,
}), {
fileCache: new cache.Cache(paths.cache),
name: 'default',
}))
.pipe(gulp.dest(paths.dest.img));
});
gulp.task('imagemin:clean-dest', function(cb) {
del.sync(paths.dest.img);
cb();
});
gulp.task('imagemin:clean-cache', function(cb) {
del.sync([
paths.cache.tmpDir + '/' + paths.cache.cacheDirName + '/default',
]);
cb();
});
gulp.task('imagemin:clean', gulp.parallel('imagemin:clean-dest', 'imagemin:clean-cache'));
// Полная сборка без вотча
gulp.task('once', gulp.parallel('less', 'imagemin'));
// Полная сборка с вотчем
gulp.task('default', gulp.series(
'once',
function watch() {
gulp.watch(paths.watch.less, gulp.task('less'));
gulp.watch(paths.src.img, gulp.task('imagemin'))
.on('unlink', function(filePath) {
del(paths.dest.img + path.basename(filePath));
})
.on('unlinkDir', function(dirPath) {
del(paths.dest.img + path.basename(dirPath));
});
},
));
// Ошибки
let onError = function(error) {
log([
(error.name + ' in ' + error.plugin).bold.red,
'',
error.message,
'',
].join('\n'));
beeper();
this.emit('end');
};