-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
206 lines (178 loc) · 5.53 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
// 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 postcssPresetEnv = require('postcss-preset-env');
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');
// country data
const fs = require('fs');
const countryData = require('world-countries');
const iso6391 = require('iso-639-3/to-1.json');
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',
},
};
gulp.task('countries', function(cb) {
let countryList = countryData.map((item) => {
const lang = Object.keys(item.languages)[0];
return {
code: item.cca2, // ISO 3166-1 alpha-2
name: item.name.common || item.name.official,
lang: iso6391[lang] || '', // ISO 639-3 to ISO 639-1
};
});
countryList.sort((a, b) => {
return a.name.localeCompare(b.name);
});
if (!fs.existsSync('tmp')){
fs.mkdirSync('tmp');
}
fs.writeFile('tmp/country-list.json', JSON.stringify(countryList), cb);
});
gulp.task('country-languages', function(cb) {
let countryLanguageList = {};
countryData.forEach((item) => {
Object.keys(item.languages).forEach((langCode) => {
// take only unique languages
if (!countryLanguageList[langCode]) {
countryLanguageList[langCode] = item.languages[langCode];
}
});
});
// Object to Array
countryLanguageList = Object.keys(countryLanguageList).map((langCode) => {
return {
code: iso6391[langCode], // ISO 639-3 to ISO 639-1
name: countryLanguageList[langCode],
};
});
countryLanguageList.sort((a, b) => {
return a.name.localeCompare(b.name);
});
if (!fs.existsSync('tmp')){
fs.mkdirSync('tmp');
}
fs.writeFile('tmp/country-language-list.json', JSON.stringify(countryLanguageList), cb);
});
// 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}),
postcssPresetEnv({
stage: 2,
features: {
'all-property': false,
'case-insensitive-attributes': false,
'focus-visible-pseudo-class': false,
'focus-within-pseudo-class': false,
'matches-pseudo-class': false,
},
}),
]))
.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', 'countries', 'country-languages'));
// Полная сборка с вотчем
gulp.task('default', gulp.series(
'imagemin:clean-dest',
'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));
});
setTimeout(function() {
log('Watching...');
});
},
));
// Ошибки
let onError = function(error) {
log([
(error.name + ' in ' + error.plugin).bold.red,
'',
error.message,
'',
].join('\n'));
beeper();
this.emit('end');
};