-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
executable file
·268 lines (230 loc) · 8.55 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
/**
*
* Gulpfile setup
*
* @since 1.0.0
* @author Ahmad Awais
* @package neat
*/
// Project configuration
var project = 'neat', // Project name, used for build zip.
url = 'neat.dev', // Local Development URL for BrowserSync. Change as-needed.
bower = './assets/bower_components/'; // Not truly using this yet, more or less playing right now. TO-DO Place in Dev branch
build = './buildtheme/', // Files that you want to package into a zip go here
buildInclude = [
// include common file types
'**/*.php',
'**/*.html',
'**/*.css',
'**/*.js',
// include specific files and folders
'screenshot.png',
// exclude files and folders
'!node_modules/**/*',
'!assets/bower_components/**/*',
'!style.css.map',
'!assets/js/custom/*',
'!assets/css/patrials/*'
];
// assets = './assets/', // Your main project assets and naming 'source' instead of 'src' to avoid confusion with gulp.src
// Load plugins
var gulp = require('gulp'),
browserSync = require('browser-sync'), // Asynchronous browser loading on .scss file changes
reload = browserSync.reload,
autoprefixer = require('gulp-autoprefixer'), // Autoprefixing magic
minifycss = require('gulp-uglifycss'),
filter = require('gulp-filter'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
newer = require('gulp-newer'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cmq = require('gulp-combine-media-queries'),
runSequence = require('gulp-run-sequence'),
sass = require('gulp-sass'),
plugins = require('gulp-load-plugins')({ camelize: true }),
ignore = require('gulp-ignore'), // Helps with ignoring files and directories in our run tasks
rimraf = require('gulp-rimraf'), // Helps with removing files and directories in our run tasks
zip = require('gulp-zip'), // Using to zip up our packaged theme into a tasty zip file that can be installed in WordPress!
plumber = require('gulp-plumber'), // Helps prevent stream crashing on errors
cache = require('gulp-cache'),
sourcemaps = require('gulp-sourcemaps');
/**
* 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',
'**/*.{png,jpg,gif}'
];
browserSync.init(files, {
proxy: url
});
});
/**
* Styles
*
* Looking at src/sass and compiling the files into Expanded format, Autoprefixing and sending the files to the build folder
*
* Sass output styles: https://web-design-weekly.com/2014/06/15/different-sass-output-styles/
*/
gulp.task('styles', function () {
return gulp.src('./assets/css/*.scss')
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass({
errLogToConsole: true,
//outputStyle: 'compressed',
outputStyle: 'compact',
// outputStyle: 'nested',
// outputStyle: 'expanded',
precision: 10
}))
.pipe(sourcemaps.write({includeContent: false}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(autoprefixer('last 2 version', '> 1%', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(sourcemaps.write('.'))
.pipe(plumber.stop())
.pipe(gulp.dest('./'))
.pipe(filter('**/*.css')) // Filtering stream to only css files
.pipe(cmq()) // Combines Media Queries
.pipe(reload({stream:true})) // Inject Styles when style file is created
.pipe(rename({ suffix: '.min' }))
.pipe(minifycss({
maxLineLen: 80
}))
.pipe(gulp.dest('./'))
.pipe(reload({stream:true})) // Inject Styles when min style file is created
.pipe(notify({ message: 'Styles task complete', onLast: true }))
});
/**
* Scripts: Vendors
*
* Look at src/js and concatenate those files, send them to assets/js where we then minimize the concatenated file.
*/
gulp.task('vendorsJs', function() {
return gulp.src(['./assets/js/vendor/*.js', bower+'**/*.js'])
.pipe(concat('vendors.js'))
.pipe(gulp.dest('./assets/js'))
.pipe(rename( {
basename: "vendors",
suffix: '.min'
}))
.pipe(uglify())
.pipe(gulp.dest('./assets/js/'))
.pipe(notify({ message: 'Vendor scripts task complete', onLast: true }));
});
/**
* Scripts: Custom
*
* Look at src/js and concatenate those files, send them to assets/js where we then minimize the concatenated file.
*/
gulp.task('scriptsJs', function() {
return gulp.src('./assets/js/custom/*.js')
.pipe(concat('custom.js'))
.pipe(gulp.dest('./assets/js'))
.pipe(rename( {
basename: "custom",
suffix: '.min'
}))
.pipe(uglify())
.pipe(gulp.dest('./assets/js/'))
.pipe(notify({ message: 'Custom scripts task complete', onLast: true }));
});
/**
* Images
*
* Look at src/images, optimize the images and send them to the appropriate place
*/
gulp.task('images', function() {
// Add the newer pipe to pass through newer images only
return gulp.src(['./assets/img/raw/**/*.{png,jpg,gif}'])
.pipe(newer('./assets/img/'))
.pipe(rimraf({ force: true }))
.pipe(imagemin({ optimizationLevel: 7, progressive: true, interlaced: true }))
.pipe(gulp.dest('./assets/img/'))
.pipe( notify( { message: 'Images task complete', onLast: true } ) );
});
/**
* Clean gulp cache
*/
gulp.task('clear', function () {
cache.clearAll();
});
/**
* Clean tasks for zip
*
* 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() {
return gulp.src(['./assets/bower_components', '**/.sass-cache','**/.DS_Store'], { read: false }) // much faster
.pipe(ignore('node_modules/**')) //Example of a directory to ignore
.pipe(rimraf({ force: true }))
// .pipe(notify({ message: 'Clean task complete', onLast: true }));
});
gulp.task('cleanupFinal', function() {
return gulp.src(['./assets/bower_components','**/.sass-cache','**/.DS_Store'], { read: false }) // much faster
.pipe(ignore('node_modules/**')) //Example of a directory to ignore
.pipe(rimraf({ force: true }))
// .pipe(notify({ message: 'Clean task complete', onLast: true }));
});
/**
* Build task that moves essential theme files for production-ready sites
*
* buildFiles copies all the files in buildInclude to build folder - check variable values at the top
* buildImages copies all the images from img folder in assets while ignoring images inside raw folder if any
*/
gulp.task('buildFiles', function() {
return gulp.src(buildInclude)
.pipe(gulp.dest(build))
.pipe(notify({ message: 'Copy from buildFiles 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(['assets/img/**/*', '!assets/images/raw/**'])
.pipe(gulp.dest(build+'assets/img/'))
.pipe(plugins.notify({ message: 'Images copied to buildTheme folder', 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' ])
return gulp.src(build+'/**/')
.pipe(zip(project+'.zip'))
.pipe(gulp.dest('./'))
.pipe(notify({ message: 'Zip task complete', onLast: true }));
});
gulp.task('delBuild', function(){
return gulp.src([build+'/**/', project+'.zip'])
.pipe(rimraf({ force: true }))
.pipe(notify({ message: 'Zip and build folder deleted', onLast: true }));
});
// ==== TASKS ==== //
/**
* Gulp Default Task
*
* Compiles styles, fires-up browser sync, watches js and php files. Note browser sync task watches php files
*
*/
// Package Distributable Theme
gulp.task('build', function(cb) {
runSequence('styles', 'cleanup', 'vendorsJs', 'scriptsJs', 'buildFiles', 'buildImages', 'buildZip','cleanupFinal', cb);
});
// Watch Task
gulp.task('default', ['styles', 'vendorsJs', 'scriptsJs', 'images', 'browser-sync'], function () {
gulp.watch('./assets/img/raw/**/*', ['images']); //Some issue, have to run manual for now
gulp.watch('./assets/css/**/*.scss', ['styles']);
gulp.watch('./assets/js/**/*.js', ['scriptsJs', browserSync.reload]);
});