-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
192 lines (169 loc) · 5.24 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
var gulp = require('gulp'),
sass = require('gulp-sass'),
cssmin = require('gulp-cssmin'),
rename = require('gulp-rename'),
gitmodified = require('gulp-gitmodified'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
replace = require('gulp-replace'),
fs = require("fs"),
sassLint = require('gulp-sass-lint'),
csslint = require('gulp-csslint');
const loadJsonFile = require('load-json-file');
function checkFilesizeInBytes(filename) {
const stats = fs.statSync(filename)
const fileSizeInBytes = stats.size
if (fileSizeInBytes >= 105000) {
console.log(" WARNING: " + filename + " is bigger than 98 kb, reddits LIMIT is 100 kb.");
}
}
/* Compress images (only one modified in git) */
gulp.task('images:gitmodified', function() {
return gulp.src('./theme/img/exported/*.+(jpg|jpeg|gif|png|svg)')
.pipe(gitmodified('modified'))
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest('./theme/img/exported-minified/'));
});
/* Compress images (all) */
gulp.task('images:all', function() {
return gulp.src('./theme/img/exported/*.+(jpg|jpeg|gif|png|svg)')
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest('./theme/img/exported-minified/'));
});
/* linting (css) */
gulp.task('lintcss', function(done) {
var linting = gulp.src('./theme/css/style.stylish.css')
.pipe(csslint())
//.pipe(csslint.formatter());
done();
});
/* linting (scss) */
gulp.task('lint', function(done) {
var linting = gulp.src('./theme/scss/*.scss')
.pipe(sassLint({
configFile: 'sass-lint.yml'
}))
.pipe(sassLint.format())
//.pipe(sassLint.failOnError())
;
linting.on('finish', function(){
gulp.series('replace');
done();
});
});
/* Compile sass and minify css (gitmodified) */
gulp.task('styles:gitmodified', function(done) {
/* Compile nested (adding @charset "utf-8") */
var compileNested = gulp.src('./theme/scss/*.scss')
.pipe(gitmodified('modified'))
.pipe(sass({outputStyle: 'nested'})
.on('error', sass.logError))
.pipe(gulp.dest('./theme/css/'))
;
/* Compile compressed (no added charset) */
var compile = gulp.src('./theme/scss/*.scss')
.pipe(gitmodified('modified'))
.pipe(sass({outputStyle: 'compressed'})
.on('error', sass.logError))
.pipe(rename({suffix: '.min'}))
.pipe(cssmin({showLog :true,debug:true}))
.pipe(gulp.dest('./theme/css/'))
;
compileNested.on('end', function(){
gulp.series('lint');
});
compile.on('end', function(){
checkFilesizeInBytes("./theme/css/style.min.css");
done();
});
});
/* replace reddit url placeholders with hardcoded full urls for use in stylish */
gulp.task('replace', function(done) {
var urls = [];
var failedReplacements = 0;
var missingUrls = [];
var file = './theme/css/style.css';
loadJsonFile('./replacementConfig.json').then(json => {
urls = json.urls;
fs.access(file, (err) => {
if (!err) {
replacing = gulp.src(file)
.pipe(replace(/(%%(.*?)%%)/g, function(match, p1, p2, offset, string) {
var url = searchUrl(p2, urls);
//console.log('Found "' + match + '" and replaced with "' + url + '" at ' + offset);
if (typeof url === "undefined") {
failedReplacements++;
if (missingUrls.indexOf(p2) < 0) {
missingUrls.push(p2);
}
return match;
} else {
return '"' + url + '"';
}
}))
.pipe(rename({suffix: '.stylish'}))
.pipe(gulp.dest('./theme/css/'))
;
replacing.on('end', function(){
gulp.series('lintcss');
if (failedReplacements > 0) {
console.log(' ' + failedReplacements + ' replacements failed because no matching url was found.');
console.log(' ' + '[' + missingUrls.join(", ") + ']');
};
});
}
else if (err) {
// file/path is not visible to the calling process
console.log(' ' + err.message);
console.log(' ' + 'Replacing urls not possible!');
}
});
});
done();
});
function searchUrl(placeholderKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].placeholder === placeholderKey) {
return myArray[i].url;
}
}
}
/* Compile sass and minify css (all) */
gulp.task('styles:all', function(done) {
/* Compile nested (adding @charset "utf-8") */
var compileNested = gulp.src('./theme/scss/*.scss')
.pipe(sass({outputStyle: 'nested'})
.on('error', sass.logError))
.pipe(gulp.dest('./theme/css/'))
;
/* Compile compressed (no added charset) */
var compile = gulp.src('./theme/scss/*.scss')
.pipe(sass({outputStyle: 'compressed'})
.on('error', sass.logError))
.pipe(rename({suffix: '.min'}))
.pipe(cssmin({showLog :true,debug:true}))
.pipe(gulp.dest('./theme/css/'))
;
compileNested.on('end', function() {
gulp.series('lint');
});
compile.on('end', function() {
checkFilesizeInBytes("./theme/css/style.min.css");
done();
});
});
gulp.task('default', gulp.series('styles:all', 'images:all', function init (done) {
// Watch Stylesheets
gulp.watch('./theme/scss/*.scss', gulp.series('styles:gitmodified'));
// Watch Images
gulp.watch(['./theme/img/**/*.+(jpg|jpeg|gif|png|svg)'], gulp.series('images:gitmodified'));
done();
}));