This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
gulpfile.js
387 lines (364 loc) · 10.6 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
383
384
385
386
387
'use strict';
/**
* Project Setup
*
* Setting up variables for project name and directories
*/
/******************************************************************************
| > PROJECT VARIABLES
******************************************************************************/
var project = 'slintheme';
var url = 'slintheme.site';
var build = './build/';
var vendors = './library/vendors/';
var source = 'assets/';
var themeBuild = [
'**/*.php',
'page-templates/**/*.php',
'./style.css',
'./gulpfile.js',
'./.jshintrc',
'./.bowerrc',
'./.gitignore',
'composer.phar',
'./*.json',
'./*.md',
'./screenshot.png',
'!wpcs/**/*',
'!node_modules/**/*',
'!vendor/**/*',
'!assets/bower_components/**/*',
'!**/*-min.css',
'!assets/js/vendor/*',
'!assets/css/*',
'!**/*-min.js',
'!assets/js/production.js'
];
/******************************************************************************
| > PLUGINS
******************************************************************************/
var autoprefixer = require( 'gulp-autoprefixer' );
var browserSync = require( 'browser-sync' ).create();
var concat = require( 'gulp-concat' );
var del = require( 'del' );
var filter = require( 'gulp-filter' );
var gulp = require( 'gulp' );
var imagemin = require( 'gulp-imagemin' );
var jshint = require( 'gulp-jshint' );
var minifycss = require( 'gulp-uglifycss' );
var notify = require( 'gulp-notify' );
var phpcs = require( 'gulp-phpcs' );
var pixrem = require( 'gulp-pixrem' );
var plugins = require( 'gulp-load-plugins' )({ camelize: true });
var plumber = require( 'gulp-plumber' );
var reload = browserSync.reload;
var rename = require( 'gulp-rename' );
var replace = require( 'gulp-replace' );
var runSequence = require( 'run-sequence' );
var sass = require( 'gulp-sass' );
var sourcemaps = require( 'gulp-sourcemaps' );
var uglify = require( 'gulp-uglify' );
var zip = require( 'gulp-zip' );
/**
* Browser Sync
*
* The 'cherry on top!' Asynchronous browser syncing of assets across multiple devices!! Watches for changes to js, image and php files
* Although, I think this is redundant, since we have a watch task that does this already.
*/
gulp.task(
'browser-sync', function() {
var files = [
'**/*.php', '**/*.js',
'**/*.{png,jpg,gif}'
];
browserSync.init(
files, {
proxy: url, // The hostname we are proxying.
host: url, // The hostname we want to use in the browser.
open: 'external' // Load either the 'local' hostname or the 'external' hostname.
}
);
}
);
/******************************************************************************
| > CSS TASKS
******************************************************************************/
gulp.task(
'styles', function() {
return gulp.src(
[
source + 'sass/**/*.scss',
'!' + source + 'sass/**/navigation-offcanvas.scss',
'!' + source + 'sass/**/flexnav.scss'
]
)
.pipe(
plumber(
{
errorHandler: function(err) {
console.log( err );
this.emit( 'end' );
}
}
)
)
.pipe( sourcemaps.init() )
.pipe(
sass(
{
sourceComments: 'map',
sourceMap: 'sass',
outputStyle: 'nested'
}
).on( 'error', sass.logError )
)
.pipe(
autoprefixer(
'last 2 version',
'safari 5', 'ie 8',
'ie 9',
'opera 12.1',
'ios 6',
'android 4'
)
)
.pipe( sourcemaps.write( '../maps' ) )
.pipe( plumber.stop() )
.pipe( replace( '@charset "UTF-8";', '' ) ) // Removes UTF-8 Encoding string atop CSS files.
.pipe( gulp.dest( source + 'css' ) )
.pipe( filter( '**/*.css' ) ) // Filtering stream to only css files.
.pipe( reload( {stream:true} ) ) // Inject Styles when style file is created.
.pipe( rename( { suffix: '-min' } ) )
.pipe(
minifycss(
{
maxLineLen: 80
}
)
)
.pipe( gulp.dest( source + 'css' ) )
.pipe( reload( {stream:true} ) ) // Inject Styles when min style file is created.
.pipe( notify( { message: 'Styles task complete', onLast: true } ) );
}
);
gulp.task(
'stylesAddons', function() {
return gulp.src(
[
source + 'sass/**/navigation-offcanvas.scss',
source + 'sass/**/flexnav.scss'
]
)
.pipe(
plumber(
{
errorHandler: function(err) {
console.log( err );
this.emit( 'end' );
}
}
)
)
.pipe( sourcemaps.init() )
.pipe(
sass(
{
sourceComments: 'map',
sourceMap: 'sass',
outputStyle: 'compressed'
}
).on( 'error', sass.logError )
)
.pipe(
autoprefixer(
'last 2 version',
'safari 5', 'ie 8',
'ie 9',
'opera 12.1',
'ios 6',
'android 4'
)
)
.pipe( sourcemaps.write( '../maps' ) )
.pipe( plumber.stop() )
.pipe( replace( '@charset "UTF-8";', '' ) ) // Removes UTF-8 Encoding string atop CSS files.
.pipe( gulp.dest( source + 'css' ) )
.pipe( filter( '**/*.css' ) ) // Filtering stream to only css files.
.pipe( reload( {stream:true} ) ) // Inject Styles when style file is created.
.pipe( rename( { suffix: '-min' } ) )
.pipe(
minifycss(
{
maxLineLen: 80
}
)
)
.pipe( gulp.dest( '../css' ) )
.pipe( reload( {stream:true} ) ) // Inject Styles when min style file is created.
.pipe( notify( { message: 'Styles task complete', onLast: true } ) );
}
);
/******************************************************************************
| > JS TASKS
******************************************************************************/
gulp.task(
'js', function() {
return gulp.src( [source + 'js/app/**/*.js'] )
.pipe( concat( 'development.js' ) )
.pipe( gulp.dest( source + 'js' ) )
.pipe(
rename(
{
basename: 'production',
suffix: '-min'
}
)
)
.pipe( uglify() )
.pipe( gulp.dest( source + 'js/' ) )
.pipe( notify( { message: 'Scripts task complete', onLast: true } ) );
}
);
/**
* JsHint Tasks
*
* Scan our own JS code excluding vendor JS libraries and perform jsHint task.
*/
gulp.task(
'jsHint', function() {
return gulp.src( [source + 'js/app/**/*.js'] )
.pipe( jshint( '.jshintrc' ) )
.pipe( jshint.reporter( 'default' ) )
.pipe( notify( { message: 'jsHint task complete', onLast: true } ) );
}
);
/**
* Images
*
* Look at src/images, optimize the images and send them to the appropriate place
*/
gulp.task(
'images', function() {
return gulp.src( [source + 'img/raw/**/*.{png,jpg,gif}'] )
.pipe( imagemin( { optimizationLevel: 5, progressive: true, interlaced: true } ) )
.pipe( gulp.dest( source + 'img/' ) )
.pipe( notify( { message: 'images task complete', onLast: true } ) );
}
);
/**
* Clean
*
* Being a little overzealous, but we're cleaning out the build folder, codekit-cache directory and annoying DS_Store files and Also
* clearing out unoptimized image files in zip as those will have been moved and optimized
*/
gulp.task(
'cleanup', function(cb) {
return del( [ project + '.zip', '**/build', './assets/bower_components', './library/vendors/composer', '**/.sass-cache', '**/.codekit-cache', '**/.DS_Store', '!node_modules/**'], cb );
}
);
gulp.task(
'cleanupFinal', function(cb) {
return del( ['**/build', './assets/bower_components', '**/.sass-cache', '**/.codekit-cache', '**/.DS_Store', '!node_modules/**'], cb );
}
);
/**
* Build task that moves essential theme files for production-ready sites
*
* First, we're moving PHP files to the build folder for redistribution. Also we're excluding the library, build and src directories. Why?
* Excluding build prevents recursive copying and Inception levels of bullshit. We exclude library because there are certain non-php files
* there that need to get moved as well. So I put the library directory into its own task. Excluding src because, well, we don't want to
* distribute uniminified/unoptimized files. And, uh, grabbing screenshot.png cause I'm janky like that!
*/
gulp.task(
'buildPhp', function() {
return gulp.src( themeBuild )
.pipe( gulp.dest( build ) )
.pipe( notify( { message: 'Moving files complete', onLast: true } ) );
}
);
// Copy Assets to Build.
gulp.task(
'buildAssets', function() {
return gulp.src( [source + '**', source + 'js/**/*.js'] )
.pipe( gulp.dest( build + '/assets' ) )
.pipe( notify( { message: 'Copy of Assets directory complete', onLast: true } ) );
}
);
// Copy Library to Build.
gulp.task(
'buildLibrary', function() {
return gulp.src( ['./library/**'] )
.pipe( gulp.dest( build + 'library' ) )
.pipe( notify( { message: 'Copy of Library directory complete', onLast: true } ) );
}
);
/**
* Zipping build directory for distribution
*
* Taking the build folder, which has been cleaned, containing optimized files and zipping it up to send out as an installable theme.
*/
gulp.task(
'buildZip', function() {
return gulp.src( [build + '/**/', './.jshintrc', './.bowerrc', './.gitignore'] )
.pipe( zip( project + '.zip' ) )
.pipe( gulp.dest( './' ) )
.pipe( notify( { message: 'Zip task complete', onLast: true } ) );
}
);
/**
* Images
*
* Look at src/images, optimize the images and send them to the appropriate place
*/
gulp.task(
'buildImages', function() {
return gulp.src( [source + 'assets/img/**/*', '!assets/images/originals/**'] )
// .pipe(plugins.cache(plugins.imagemin({ optimizationLevel: 7, progressive: true, interlaced: true })))
.pipe( gulp.dest( build + 'assets/img/' ) )
.pipe( plugins.notify( { message: 'Images task complete', onLast: true } ) );
}
);
// Package Distributable Theme.
gulp.task(
'build', function(cb) {
runSequence( 'styles', 'cleanup', 'js', 'buildPhp', 'buildLibrary', 'buildAssets', 'buildImages', 'buildZip', 'cleanupFinal', cb );
}
);
/**
* "Sniff" the PHP to check
* against WordPress coding standards.
*/
var php_files = ['**/*.php','!library/**','!vendor/**','!node_modules/**'];
gulp.task(
'php', function () {
return gulp.src( php_files )
// Validate files using PHP Code Sniffer.
.pipe(
phpcs(
{
bin: './vendor/bin/phpcs',
standard: 'WordPress-Core'
}
)
)
// Log all problems that were found.
.pipe( phpcs.reporter( 'log' ) );
}
);
/**
* Holds all the test tasks.
*/
gulp.task( 'test',['php','jsHint'] );
/******************************************************************************
| > WATCH TASKS
******************************************************************************/
// Watch Task.
gulp.task(
'default', ['styles', 'js', 'images', 'browser-sync'], function() {
gulp.watch( source + 'sass/**/*.scss', ['styles'] );
gulp.watch( source + 'sass/**/*.scss', ['stylesAddons'] );
gulp.watch( source + 'js/app/**/*.js', ['js', browserSync.reload] );
gulp.watch( source + 'js/app/**/*.js', ['jsHint'] );
gulp.watch( source + 'img/**/*.{png,jpg,gif}', ['images'] );
}
);