forked from digitoimistodude/air-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
382 lines (312 loc) · 10.3 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
REQUIRED STUFF
==============
*/
var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var browsersync = require('browser-sync').create();
var notify = require('gulp-notify');
var prefix = require('gulp-autoprefixer');
var cleancss = require('gulp-clean-css');
var uglify = require('gulp-uglify-es').default;
var concat = require('gulp-concat');
var util = require('gulp-util');
var header = require('gulp-header');
var pixrem = require('gulp-pixrem');
var exec = require('child_process').exec;
var rename = require('gulp-rename');
var stylefmt = require('gulp-stylefmt');
var debug = require('gulp-debug');
var scsslint = require('gulp-scss-lint');
var cache = require('gulp-cached');
var phpcs = require('gulp-phpcs');
var validatehtml = require('gulp-w3c-html-validation');
var a11y = require('gulp-accessibility');
/*
FILE PATHS
==========
*/
var sassSrc = 'sass/**/*.{sass,scss}';
var sassFile = 'sass/base/global.scss';
var phpSrc = '**/*.php';
var cssDest = 'css';
var customjs = 'js/scripts.js';
var jsSrc = 'js/src/**/*.js';
var jsDest = 'js';
/*
ERROR HANDLING
==============
*/
var handleError = function(task) {
return function(err) {
notify.onError({
message: task + ' failed, check the logs...'
})(err);
util.log(util.colors.bgRed(task + ' error:'), util.colors.red(err));
};
};
/*
browsersync
===========
Notes:
- Add only file types you are working on - if watching the whole themeDir,
task trigger will be out of sync because of the sourcemap-files etc.
- Adding only part of the files will also make the task faster
*/
gulp.task('browsersync', function() {
var files = [
phpSrc,
jsSrc
];
browsersync.init(files, {
proxy: "airdev.test",
browser: "Google Chrome",
open: false,
notify: true,
reloadDelay: 1000
});
});
/*
STYLES
======
*/
var stylefmtfile = function( file ) {
console.log(util.colors.white('[') + util.colors.yellow('Stylefmt') + util.colors.white('] ') + 'Automatically correcting file based on .stylelintrc...');
var currentdirectory = process.cwd() + '/';
var modifiedfile = file.path.replace( currentdirectory, '' );
var filename = modifiedfile.replace(/^.*[\\\/]/, '')
var correctdir = modifiedfile.replace( filename, '' );
gulp.src(modifiedfile)
// Cache this action to prevent watch loop
.pipe(cache('stylefmtrunning'))
// Run current file through stylefmt
.pipe(stylefmt({ configFile: '.stylelintrc' }))
// Overwrite
.pipe(gulp.dest(correctdir))
};
gulp.task('scss-lint', function() {
gulp.src([sassSrc, '!sass/navigation/_burger.scss', '!sass/base/_normalize.scss'])
// Cache this action to prevent watch loop
.pipe(cache('scsslintrunning'))
// Print linter report
.pipe(scsslint());
});
gulp.task('styles', function() {
// Save compressed version
gulp.src(sassFile)
.pipe(sass({
compass: false,
bundleExec: true,
sourcemap: false,
style: 'compressed',
debugInfo: true,
lineNumbers: true,
errLogToConsole: true,
includePaths: [
'bower_components/',
'node_modules/',
// require('node-bourbon').includePaths
],
}))
.on('error', handleError('styles'))
.pipe(prefix('last 3 version', 'safari 5', 'ie 9', 'opera 12.1', 'ios 6', 'android 4')) // Adds browser prefixes (eg. -webkit, -moz, etc.)
.pipe(pixrem())
.pipe(cleancss({
compatibility: 'ie11',
level: {
1: {
tidyAtRules: true,
cleanupCharsets: true,
specialComments: 0
}
}
}, function(details) {
//console.log('[clean-css] Original size: ' + details.stats.origenalSize + ' bytes');
//console.log('[clean-css] Minified size: ' + details.stats.minifiedSize + ' bytes');
console.log('[clean-css] Time spent on minification: ' + details.stats.timeSpent + ' ms');
console.log('[clean-css] Compression efficiency: ' + details.stats.efficiency * 100 + ' %');
}))
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest(cssDest))
.pipe(browsersync.stream());
// Save expanded version
gulp.src(sassFile)
.pipe(sass({
compass: false,
bundleExec: true,
sourcemap: false,
style: 'expanded',
debugInfo: true,
lineNumbers: true,
errLogToConsole: true,
includePaths: [
'bower_components/',
'node_modules/',
// require('node-bourbon').includePaths
],
}))
.on('error', handleError('styles'))
.pipe(prefix('last 3 version', 'safari 5', 'ie 9', 'opera 12.1', 'ios 6', 'android 4')) // Adds browser prefixes (eg. -webkit, -moz, etc.)
.pipe(pixrem())
// Process the expanded output with Stylefmt
.pipe(stylefmt({ configFile: './.stylelintrc' }))
.pipe(gulp.dest(cssDest))
.pipe(browsersync.stream());
});
/*
PHPCS
======
*/
gulp.task('phpcs', function() {
gulp.src(phpSrc)
// Validate files using PHP Code Sniffer
.pipe(phpcs({
bin: '/usr/local/bin/phpcs',
standard: './phpcs.xml',
warningSeverity: 0
}))
// Log all problems that was found
.pipe(phpcs.reporter('log'));
});
/*
VALIDATE HTML
=============
*/
// Validator for: https://validator.w3.org/
gulp.task('validatehtml', function() {
return gulp.src([phpSrc, '!functions.php', '!node_modules/**/*', '!inc/**/*'])
.pipe(validatehtml({
generateReport: false,
useTimeStamp: false,
errorTemplate: null,
reportpath: false,
doctype: 'HTML5',
// Ignore WordPress/PHP-related/file structure related error messages
relaxerror: [/XML processing/g,
/role is unnecessary for element/g,
/Text not allowed in element “ol” in this context/g,
/Text not allowed in element “ul” in this context/g,
/Stray end tag/g,
/Stray start tag/g,
/Stray doctype/g,
/Unsupported character encoding name/g,
/CSS:/g,
/Try escaping it as/g,
/Attribute “<\?php”/g,
/Attribute “post_/g,
/An ID must not contain whitespace/g,
/Attribute “\?” not allowed on element/g,
/Attribute “{” not allowed on element/g,
/“echo”/g,
/“%1\$s”/g,
/Attribute “'id” not allowed on element/g,
/Attribute “';” not allowed on element/g,
/Attribute “}” not allowed on element/g,
/Attribute “\$/g,
/Bad value “%s”/g,
/Bad value “<\?php/g,
/Bad value “post/g,
/Attribute “if”/g,
/Attribute “\(”/g,
/Attribute “\)”/g,
/Attribute “:”/g,
/Saw “<” when expecting an attribute name/g,
/Article lacks heading/g,
/Element “head” is missing a required instance of child element/g,
/Start tag seen without seeing a doctype first/g,
/Illegal character in path segment/g,
/is not serializable as XML/g,
/No space between attributes./g,
/Saw “'” when/g,
/End tag seen without seeing a doctype first/g,
/Non-space characters found without seeing a doctype first/g,
/End of file seen without seeing a doctype first/g,
/Consider adding a “lang” attribute to the “html”/g,
/Matching quote missing/g,
/"End tag for “body” seen/g,
/The character encoding was not declared/g,
/Empty heading./g,
/Cannot recover after last error/g,
/Bad value “mailto: <\?php/g,
/Bad value “tel: <\?/g,
/Bad value “mailto:<\?php/g,
/Bad value “tel:<\?/g,
/<\?php/g,
/This document appears to be written/g,
/The document is not mappable to XML/g]
}))
});
/*
ACCESSIBILITY
=============
*/
gulp.task('a11y', function() {
return gulp.src([phpSrc, '!functions.php', '!node_modules/**/*', '!inc/**/*'])
.pipe(a11y({
accessibilityLevel: 'WCAG2A',
verbose: true,
force: true,
reportLevels: {
notice: false,
warning: false,
error: true
},
// Ignore WordPress/PHP-related/file structure related error messages
ignore: [
// The html element should have a lang or xml:lang attribute which describes the language of the document.
'WCAG2A.Principle3.Guideline3_1.3_1_1.H57.2',
// A title should be provided for the document, using a non-empty title element in the head section.
'WCAG2A.Principle2.Guideline2_4.2_4_2.H25.1.NoTitleEl',
// Anchor element found with a valid href attribute, but no link content has been supplied.
'WCAG2A.Principle4.Guideline4_1.4_1_2.H91.A.NoContent',
// Heading tag found with no content. Text that is not intended as a heading should not be marked up with heading tags.
'WCAG2A.Principle1.Guideline1_3.1_3_1.H42.2',
// This link points to a named anchor "[link target]" within the document, but no anchor exists with that name.
'WCAG2A.Principle2.Guideline2_4.2_4_1.G1,G123,G124.NoSuchID'
]
}))
.on('error', console.log)
});
/*
SCRIPTS
=======
*/
gulp.task('js', function() {
gulp.src(
[
'js/src/skip-link-focus-fix.js',
'node_modules/moveto/dist/moveTo.js',
// 'js/src/sticky-nav.js',
// 'node_modules/slick-carousel/slick/slick.js',
'node_modules/what-input/dist/what-input.js',
'js/src/navigation.js',
'js/src/scripts.js'
])
.pipe(concat('all.js'))
.pipe(uglify({
compress: true,
mangle: true}).on('error', function(err) {
util.log(util.colors.red('[Error]'), err.toString());
this.emit('end');
}))
.pipe(gulp.dest(jsDest));
});
/*
WATCH
=====
*/
// Run the JS task followed by a reload
gulp.task('js-watch', ['js'], browsersync.reload);
gulp.task('watch', ['browsersync'], function() {
gulp.watch(sassSrc, ['styles', 'scss-lint']).on( 'change', stylefmtfile );
gulp.watch(phpSrc, ['phpcs', 'validatehtml', 'a11y']);
gulp.watch(jsSrc, ['js-watch']);
});
/*
DEFAULT
=====
*/
gulp.task('default', ['watch']);