-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
211 lines (179 loc) · 5.34 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
210
211
'use strict';
// Load plugins
const {series, parallel, dest, src, watch} = require('gulp');
const plumber = require('gulp-plumber');
const sourcemaps = require('gulp-sourcemaps');
const sass = require('gulp-sass');
sass.compiler = require('node-sass');
const newer = require('gulp-newer');
const imagemin = require('gulp-imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');
const imageminZopfli = require('imagemin-zopfli');
const imageminGiflossy = require('imagemin-giflossy');
const terser = require('gulp-terser');
const cssnano = require('gulp-cssnano');
const del = require('del');
const master = {
distDir: 'public/assets/',
assetsDir: 'assets/',
jsFiles: 'assets/js/**/*.js',
jsPattern: '**/*.js',
jsDistDir: 'public/assets/js/',
cssFiles: 'assets/css/**/*.{css,scss}',
cssPattern: '**/*.{css}',
cssDistDir: 'public/assets/css/',
imageFiles: 'assets/images/**/*.{png,jpg,gif,svg}',
imagePattern: '**/*.{png,jpg,gif,svg}',
imagesDistDir: 'public/assets/images/',
iconFiles: 'assets/icons/**/*.{svg}',
iconPattern: '**/*.{svg}',
iconsDistDir: 'public/assets/icons/',
};
const config = {
/* parameters for image optimization */
imagemin: [
imageminMozjpeg({
//quality: 85
}),
imageminPngquant({
speed: 3,
quality: [0.95, 1], //lossy settings
}),
imageminZopfli({
more: true,
// iterations: 50 // very slow but more effective
}),
//gif
// imagemin.gifsicle({
// interlaced: true,
// optimizationLevel: 3
// }),
//gif very light lossy, use only one of gifsicle or Giflossy
imageminGiflossy({
optimizationLevel: 3,
optimize: 3, //keep-empty: Preserve empty transparent frames
lossy: 2,
}),
imagemin.svgo({
js2svg: {
pretty: true,
},
plugins: [
{
removeViewBox: false,
collapseGroups: true,
removeUselessStrokeAndFill: false,
},
],
}),
],
};
/* CSS Tasks
-------------------------------------------------------------------- */
function copyCSSLibraries () {
let libs = require('./' + master.assetsDir + 'libraries.json');
if (!libs.css.length) {
return Promise.resolve('No CSS libraries to copy.');
}
return src(libs.css, { base: './node_modules/' }).
pipe(dest(master.distDir + 'lib/css/'));
}
function copyCSS () {
return src(master.cssFiles).
pipe(newer(master.cssDistDir)).
pipe(sass()).
pipe(dest(master.cssDistDir));
}
function minifyCSS () {
return src(master.cssDistDir + master.cssPattern).
pipe(newer(master.cssDistDir)).
pipe(sourcemaps()).
pipe(cssnano()).
pipe(dest(master.cssDistDir));
}
/* Image Tasks
-------------------------------------------------------------------- */
function copyImages () {
return src(master.imageFiles).
pipe(newer(master.imagesDistDir)).
pipe(dest(master.imagesDistDir));
}
function copySprites () {
return src(master.iconFiles).
pipe(newer(master.iconsDistDir)).
pipe(dest(master.iconsDistDir));
}
function optimizeImages () {
return src(master.imageFiles).
pipe(newer(master.imagesDistDir)).
pipe(imagemin(config.imagemin, {verbose: true})).
pipe(dest(master.imagesDistDir));
}
function optimizeIcons () {
return src(master.iconFiles).
pipe(newer(master.iconsDistDir)).
pipe(imagemin(config.imagemin, {verbose: true})).
pipe(dest(master.iconsDistDir));
}
/* JavaScript Tasks
-------------------------------------------------------------------- */
function copyJavascripts () {
return src(master.jsFiles).
pipe(newer(master.distDir)).
pipe(dest(master.distDir));
}
function copyJavascriptLibraries () {
let libs = require('./' + master.assetsDir + 'libraries.json');
if (!libs.js.length) {
return Promise.resolve('No JS libraries to copy.');
}
return src(libs.js, { base: './node_modules/' }).
pipe(dest(master.distDir + 'lib/js/'));
}
function minifyJavascripts () {
return src(master.jsDistDir + master.jsPattern).
pipe(plumber()).
pipe(terser()).
pipe(dest(master.jsDistDir));
}
/* Watch
-------------------------------------------------------------------- */
function watchFiles () {
watch(master.assetsDir + '**/*', series(exports.assets));
}
/* Utility Tasks
-------------------------------------------------------------------- */
// Clean assets
function clean () {
return del([master.distDir + '/*']);
}
/* Export Tasks
-------------------------------------------------------------------- */
exports.clean = clean;
exports.watch = watchFiles;
exports.assets = parallel(
copyJavascriptLibraries,
copyJavascripts,
copyCSSLibraries,
copyCSS,
copyImages,
copySprites,
);
exports.minimize = parallel(
optimizeImages,
optimizeIcons,
minifyCSS,
minifyJavascripts,
);
exports.dev = series(
clean,
exports.assets,
exports.watch,
);
exports.build = series(
clean,
exports.assets,
exports.minimize,
);
exports.default = exports.dev;