-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
209 lines (177 loc) · 6.39 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var gulp = require('gulp'),
del = require('del'),
path = require('path'),
browserify = require('browserify'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
babelify = require('babelify'),
include = require('gulp-include'),
sass = require('gulp-sass'),
postcss = require('gulp-postcss'),
sourcemaps = require('gulp-sourcemaps'),
argv = require('yargs').argv,
gulpif = require('gulp-if'),
autoprefixer = require('autoprefixer'),
babel = require('gulp-babel'),
uglify = require('gulp-uglify'),
postcssFlex = require('postcss-flexbugs-fixes'),
postcssSvg = require('postcss-svg'),
svgSprite = require('gulp-svg-sprite'),
svgSymbols = require('gulp-svg-symbols'),
postcssAssets = require('postcss-assets')
rename = require('gulp-rename'),
clean = require('gulp-clean'),
browserSync = require('browser-sync').create(),
pug = require('gulp-pug'),
notify = require('gulp-notify'),
cssnano = require('gulp-cssnano'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
svgmin = require('gulp-svgmin'),
ttf2woff = require('gulp-ttf2woff'),
ttf2woff2 = require('gulp-ttf2woff2'),
ttf2eot = require('gulp-ttf2eot');
//BrowserSync
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "./dist"
},
notify: true,
open: false
});
});
//Styles task
gulp.task('styles', function () {
var processors = [
autoprefixer(),
postcssSvg({paths: ['dist/img'], silent: false}),
postcssAssets({loadPaths: ['dist/img'], basePath: 'dist/img', baseUrl: '/img/'}),
postcssFlex(),
];
return gulp.src('src/assets/stylesheets/style.sass')
.pipe(sourcemaps.init())
.pipe(gulpif(!argv.production, sourcemaps.init()))
.pipe(sass({includePaths: ['node_modules']}).on('error', notify.onError()))
.pipe(postcss(processors).on('error', notify.onError()))
.pipe(cssnano())
.pipe(gulpif(!argv.production, sourcemaps.write()))
.pipe(gulp.dest('dist/css/'))
.pipe(browserSync.stream());
});
//Pug task
gulp.task('pug', function() {
return gulp.src('src/views/pages/**/*.pug')
.pipe(pug({pretty: true}).on('error', notify.onError(function (error) {
return 'Message to the notifier: ' + error.message;
})))
.pipe(gulp.dest('dist'));
});
gulp.task('js', function () {
return browserify({entries: './src/assets/js/scripts.js', paths: ['node_modules', 'src/assets/js'], debug: false})
.transform('babelify', {presets: ['@babel/env']})
.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gulpif(!argv.production, sourcemaps.init()))
.pipe(uglify().on('error', notify.onError(function (error) {
return 'Message to the notifier: ' + error.message;
})))
.pipe(gulpif(!argv.production, sourcemaps.write()))
.pipe(gulp.dest('dist/js/'));
});
//optimization img(jpg,png)
gulp.task('optimizationIMG', () => {
return gulp.src(['src/assets/img/**/*.png', 'src/assets/img/**/*.jpg'])
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest('dist/img/'));
});
//optimization svg
gulp.task('optimizationSVG', function () {
return gulp.src(['src/assets/img/**/*.svg', '!src/assets/img/sprites/**/*.svg'], {base: 'src/assets/img'})
.pipe(svgmin())
.pipe(gulp.dest('dist/img/'));
});
gulp.task('svgFragmentsSprite', function () {
return gulp.src('src/assets/img/sprites/svg-fragments/**/*.svg')
.pipe(svgSprite({
mode: {
view: {
dest: '.',
sprite: 'svg-fragments.svg',
bust: false
}
}
}))
.pipe(svgmin({plugins: [{cleanupIDs: false}]}))
.pipe(gulp.dest('dist/img'));
});
gulp.task('svgSymbolSprite', function () {
return gulp.src('src/assets/img/sprites/svg-symbols/**/*.svg')
.pipe(svgSymbols({
templates: ['default-svg']
}))
.pipe(gulp.dest('dist/img'));
});
//Convert fonts(ttf to woff,woff2,eot):
//ttf to woff
gulp.task('ttf2woff', function(){
gulp.src(['src/assets/fonts/*.ttf'])
.pipe(ttf2woff())
.pipe(gulp.dest('src/assets/fonts/'));
});
//ttf to woff2
gulp.task('ttf2woff2', function(){
gulp.src(['src/assets/fonts/*.ttf'])
.pipe(ttf2woff2())
.pipe(gulp.dest('src/assets/fonts/'));
});
//ttf to eot
gulp.task('ttf2eot', function(){
gulp.src(['src/assets/fonts/*.ttf'])
.pipe(ttf2eot())
.pipe(gulp.dest('src/assets/fonts/'));
});
gulp.task('convertFonts', function(done){
gulp.series('ttf2woff', 'ttf2eot', 'ttf2woff2');
done();
});
//Task moving fonts to dist folder
gulp.task('fonts', function(done){
gulp.src(['src/assets/fonts/**/*'])
.pipe(gulp.dest('dist/fonts/'));
done();
});
// Clean task
gulp.task('clean', function() {
return gulp.src('dist', {read: false})
.pipe(clean())
.pipe(notify('dist folder was removed'));
});
//Gulp watcher
gulp.task('watch', function () {
gulp.watch('src/assets/stylesheets/**/*.sass', gulp.parallel('styles'));
gulp.watch('src/views/**/*.pug', gulp.parallel('pug'));
gulp.watch('dist/**/*.html').on('change', browserSync.reload);
gulp.watch('src/assets/js/**/*.js', gulp.parallel('js'));
gulp.watch('dist/js/scripts.js').on('change', browserSync.reload);
gulp.watch(['src/assets/img/sprites/svg-fragments/**/*.svg'], gulp.parallel('svgFragmentsSprite'));
gulp.watch(['src/assets/img/sprites/svg-symbols/**/*.svg'], gulp.parallel('svgSymbolSprite'));
var svgWatcher = gulp.watch(['src/assets/img/**/*.svg', '!src/assets/img/sprites/**/*.svg'], gulp.parallel('optimizationSVG'));
var imgWatcher = gulp.watch(['src/assets/img/**/*.png', 'src/assets/img/**/*.jpg'], gulp.parallel('optimizationIMG'));
function watcher(task) {
task.on('unlink', function (filepath) {
var filePathFromSrc = path.relative(path.resolve('src/assets/img'), filepath);
var destFilePath = path.resolve('dist/img', filePathFromSrc);
del.sync(destFilePath);
});
}
watcher(svgWatcher);
watcher(imgWatcher);
});
//Default Gulp task
gulp.task('default', gulp.series('fonts', 'optimizationIMG', 'optimizationSVG', 'svgSymbolSprite', 'svgFragmentsSprite', gulp.parallel('styles', 'pug', 'js'), gulp.parallel('browser-sync', 'watch')));