-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
173 lines (139 loc) · 6.27 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var gulp = require('gulp'), // Подключаем Gulp
sass = require('gulp-sass'), //Подключаем Sass пакет,
concat = require('gulp-concat'), // Подключаем gulp-concat (для конкатенации файлов)
includer = require("gulp-x-includer"),
uglify = require('gulp-uglify'), // Подключаем gulp-uglifyjs (для сжатия JS)
cssnano = require('gulp-cssnano'), // Подключаем пакет для минификации CSS
rename = require('gulp-rename'), // Подключаем библиотеку для переименования файлов
del = require('del'), // Подключаем библиотеку для удаления файлов и папок
tinypng = require('gulp-tinypng-compress'), // Подключаем библиотеку для работы с изображениями
pngquant = require('imagemin-pngquant'), // Подключаем библиотеку для работы с png
cache = require('gulp-cache'), // Подключаем библиотеку кеширования
autoprefixer = require('gulp-autoprefixer'),// Подключаем библиотеку для автоматического добавления префиксов
livereload = require('gulp-livereload'),
babel = require('gulp-babel'),
svgSprite = require('gulp-svg-sprites'),
svgMin = require('gulp-svgmin'),
cheerio = require('gulp-cheerio'),
replace = require('gulp-replace'),
browserSync = require('browser-sync');
gulp.task('server', function () {
browserSync.init({
server: {
baseDir: './dist/',
directory: true
},
notify: false
});
// gulp.watch('src/styles/**/*.styl', ['styles']);
});
gulp.task('sass', function(){ // Создаем таск Sass
return gulp.src('app/plain/sass/*.sass') // Берем источник
.pipe(sass()) // Преобразуем Sass в CSS посредством gulp-sass
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest('app/plain/css')); // Выгружаем результата в папку app/css
// Обновляем CSS на странице при изменении
});
gulp.task('svg', function () {
return gulp.src('app/plain/svg/*.svg')
// minify svg
.pipe(svgMin({
js2svg: {
pretty: true
}
}))
// remove all fill and style declarations in out shapes
.pipe(cheerio({
run: function ($) {
// $('[fill]').removeAttr('fill');
$('[style]').removeAttr('style');
},
parserOptions: { xmlMode: true }
}))
// cheerio plugin create unnecessary string '>', so replace it.
.pipe(replace('>', '>'))
// build svg sprite
.pipe(svgSprite({
mode: "symbols",
preview: false,
// selector: "icon-%f",
svg: {
symbols: 'sprite.svg'
}
}
))
.pipe(gulp.dest('dist/plain/svg/'));
});
gulp.task("include", function(){
gulp.src(["app/*.html"])
.pipe(includer())
.pipe(gulp.dest("dist/"));
// .pipe(livereload());
});
gulp.task('scripts', () =>
gulp.src('app/plain/js/*.js')
.pipe(babel({
presets: ["es2015"]
}))
// .pipe(replace('translate3d', 'translateX'))
.pipe(uglify()) // Сжимаем JS файл
.pipe(gulp.dest('dist/plain/js')) // Выгружаем в папку app/js
);
gulp.task('css', function() {
return gulp.src('app/plain/css/*.css') // Выбираем файл для минификации
.pipe(cssnano({
reduceIdents: false,
})) // Сжимаем
.pipe(gulp.dest('dist/plain/css')) // Выгружаем в папку dist/css
.pipe(browserSync.stream());
});
gulp.task('watch', ['css', 'scripts', 'include'], function() {
gulp.watch('app/plain/sass/*.sass', ['sass']); // Наблюдение за sass файлами в папке sass
gulp.watch('app/plain/css/*.css', ['css']); // Наблюдение за sass файлами в папке sass
// gulp.watch("app/**/*.html", ['include']);
gulp.watch('app/**/*.html', ['include']).on('change', browserSync.reload); // Наблюдение за HTML файлами в корне проекта
gulp.watch('app/plain/js/*.js',['scripts']); // Наблюдение за JS файлами в папке js
gulp.watch('app/plain/svg/*.svg', ['svg']);
gulp.watch('app/img/*', ['tinypng']);
gulp.watch('dist/plain/css/*.css');
// livereload.listen();
});
gulp.task('clean', function() {
return del.sync('dist'); // Удаляем папку dist перед сборкой
});
// gulp.task('img', () =>
// gulp.src('app/img/*')
// .pipe(imagemin([
// imagemin.gifsicle({interlaced: true}),
// imagemin.jpegtran({progressive: true}),
// imagemin.optipng({optimizationLevel: 5}),
// ]))
// .pipe(gulp.dest('dist/img'))
// );
gulp.task('tinypng', function () {
gulp.src('app/img/*.{png,jpg,jpeg}')
.pipe(tinypng({
key: 'y4jVksSnWcMBv8el8RQMjFpU9RdQ6z9d',
log: true
}))
.pipe(gulp.dest('dist/img'));
});
gulp.task('build', ['img', 'sass', 'scripts'], function() {
var buildCss = gulp.src([ // Переносим библиотеки в продакшен
'app/plain/css/*.css'
])
.pipe(gulp.dest('dist/plain/css'))
var buildFonts = gulp.src('app/plain/fonts/**/*') // Переносим шрифты в продакшен
.pipe(gulp.dest('dist/plain/fonts'))
var buildJs = gulp.src('app/plain/js/**/*') // Переносим скрипты в продакшен
.pipe(gulp.dest('dist/plain/js'))
var buildHtml = gulp.src('app/*.html') // Переносим HTML в продакшен
.pipe(gulp.dest('dist'));
});
gulp.task('clear', function (callback) {
return cache.clearAll();
});
gulp.task('default', ['watch', 'server','svg', 'tinypng']);