forked from truongsinh/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
40 lines (36 loc) · 1.32 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
'use strict';
const argv = require('yargs').argv;
const fsExtra = require('fs-extra');
const fs = fsExtra;
const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const myLog = console.log
const path = require('canonical-path');
const yamljs = require('yamljs');
const _configYml = yamljs.load('_config.yml');
const source = _configYml.source || _throw();
gulp.task('compress-images', () => {
const baseDir = argv.path
? path.resolve(argv.path)
: path.resolve(source);
if (!fs.existsSync(baseDir)) throw `ERROR: compress-images: path DNE "${baseDir}".`;
myLog(`Compressing image files under: ${baseDir}`);
return gulp.src([
`${baseDir}/**/*.gif`,
`${baseDir}/**/*.jpg`,
`${baseDir}/**/*.jpeg`,
`${baseDir}/**/*.png`,
`!${baseDir}/**/.*/**`,
`!${baseDir}/**/build/**`,
`!${baseDir}/**/node_modules/**`,
], { base: baseDir })
.pipe(imagemin([
// imagemin.gifsicle({interlaced: true}),
// imagemin.jpegtran({progressive: true}),
imagemin.optipng({ optimizationLevel: 4 })]))
.pipe(gulp.dest(baseDir))
});
// Used to ensure that values/properties that are looked up from external
// sources are actually defined (when expecting a truthy value).
// Idiom: const foo = lookup('bar') || _throw()
function _throw(opt) { throw 'Unexpected value' + opt ? `:${opt}` : ''; }