-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
executable file
·70 lines (61 loc) · 2.2 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
'use strict';
const path = require('path');
const gulp = require('gulp');
const jshint = require('gulp-jshint');
const superagent = require('superagent');
const fs = require('fs-extra');
const glob = require('glob');
// Got problems? Try logging 'em
// const logging = require('plylog');
// logging.setVerbose();
const project = require('./gulp/project.js');
const ensureLazyFragments = require('./gulp/ensure-lazy-fragments.js');
const root = path.resolve(process.cwd(), 'images');
const optimizedImagesRoot = path.resolve(process.cwd(), 'images-optimized');
const imageOptions = {
logos: '250,scale-down',
};
// Optimize images with ImageOptim
// Run with `yarn run build optimize-images`
gulp.task('optimize-images', () =>
glob('images/**/*.{jpg,png,svg}', (err, files) => {
for (const file of files) {
const relativeFile = file.substring(file.indexOf('/') + 1);
fs.ensureDirSync(path.resolve(optimizedImagesRoot, path.dirname(relativeFile)));
if (path.extname(file) === '.svg') {
fs.copySync(file, path.resolve(optimizedImagesRoot, relativeFile));
} else {
const imageCategory = path.relative(root, file).split('/')[0];
const options = imageOptions[imageCategory] || 'full';
superagent.post(`https://im2.io/nddfzrzzpk/${options}`)
.attach('file', file)
.end((err, res) => {
console.log(`Finished optimizing ${file}`);
fs.writeFileSync(path.resolve(optimizedImagesRoot, relativeFile), res.body);
});
}
}
})
);
gulp.task('ensure-images-optimized', () =>
new Promise((resolve, reject) => {
if (!fs.existsSync(optimizedImagesRoot)) {
reject('`images-optimized` does not exist. Make sure to run `yarn run build optimize-images`');
} else {
resolve();
}
})
);
gulp.task('ensure-lazy-fragments', ensureLazyFragments);
function linter() {
return gulp.src('src/**/*.html')
.pipe(jshint.extract()) // Extract JS from .html files
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
}
// Clean the build directory, split all source and dependency files into streams
// and process them, and output the complete project
gulp.task('default', gulp.series([
linter,
project
]));