-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
115 lines (102 loc) · 3.04 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
'use strict';
var gulp = require('gulp');
var gulpIf = require('gulp-if');
var gulpRev = require('gulp-rev');
var gulpRevCollector = require('gulp-rev-collector');
var gulpRevReplace = require('gulp-rev-replace');
var gulpUglify = require('gulp-uglify');
var gulpUniqueFiles = require('gulp-unique-files');
var gulpUseRef = require('gulp-useref');
var gulpCleanCSS = require('gulp-clean-css');
var gulpResponsive = require('gulp-responsive');
var gulpCheerio = require('gulp-cheerio');
var del = require('del');
var rename = require('rename');
var dirs = {
public: 'public',
screenshots: 'public/build/screenshots'
};
gulp.task('useref', ['screenshot'], function() {
var assets = gulpUseRef.assets({
searchPath: 'public'
});
return gulp.src('public/**/*.html')
.pipe(assets)
.pipe(gulpUniqueFiles())
.pipe(gulpIf('*.css', gulpCleanCSS()))
.pipe(gulpIf('*.js', gulpUglify()))
.pipe(gulpRev())
.pipe(assets.restore())
.pipe(gulpUseRef())
.pipe(gulpRevReplace({
prefix: '/'
}))
.pipe(gulp.dest('public'));
});
gulp.task('screenshot:clean', function() {
return del([dirs.screenshots + '/**/*']);
});
gulp.task('screenshot:rev', ['screenshot:clean'], function() {
return gulp.src('public/themes/screenshots/*.png')
.pipe(gulpRev())
.pipe(gulp.dest(dirs.screenshots))
.pipe(gulpRev.manifest())
.pipe(gulp.dest(dirs.screenshots));
});
gulp.task('screenshot:revreplace', ['screenshot:rev'], function() {
var destDir = '/build/screenshots';
return gulp.src([dirs.screenshots + '/rev-manifest.json', 'public/themes/index.html'])
.pipe(gulpRevCollector({
replaceReved: true,
dirReplacements: {
'/themes/screenshots': destDir
}
}))
.pipe(gulpCheerio(function($, file) {
$('img.plugin-screenshot-img.lazyload').each(function() {
var img = $(this);
var src = img.attr('data-src') || img.attr('data-org');
if (!src) return;
var jpgPath = replaceBackSlash(rename(src, {extname: '.jpg'}));
var jpg2xPath = replaceBackSlash(rename(jpgPath, {suffix: '@2x'}));
var srcset = [
jpgPath,
jpg2xPath + ' 2x'
].join(', ');
img.attr('data-src', jpgPath)
.attr('data-srcset', srcset)
.attr('data-org', src);
});
}))
.pipe(gulp.dest('public/themes'));
});
gulp.task('screenshot:resize', ['screenshot:rev'], function() {
return gulp.src(dirs.screenshots + '/*.png')
.pipe(gulpResponsive({
'*.png': [
{
width: '50%',
rename: {
extname: '.jpg'
}
},
{
rename: {
suffix: '@2x',
extname: '.jpg'
}
}
]
}, {
progressive: true,
format: 'jpeg',
quality: 70,
stats: false
}))
.pipe(gulp.dest(dirs.screenshots));
});
gulp.task('screenshot', ['screenshot:rev', 'screenshot:resize', 'screenshot:revreplace']);
gulp.task('default', ['useref', 'screenshot']);
function replaceBackSlash(str) {
return str.replace(/\\/g, '/');
}