-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
363 lines (339 loc) · 11.4 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
'use strict';
/****************************************
DEPENDENCIES
*****************************************/
/*
* This list of dependency variables comes from the package.json file. Ensure any dependency listed here is also added to package.json.
* These variables are declared here at the top and are used throughout the gulpfile to complete certain tasks and add functionality.
*/
const fs = require('fs');
const autoprefixer = require('autoprefixer');
const browsersync = require('browser-sync').create();
const concat = require('gulp-concat');
const cssnano = require('cssnano');
const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const newer = require('gulp-newer');
const notify = require('gulp-notify');
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const rev = require('gulp-rev');
const sass = require('gulp-sass');
const shell = require('gulp-shell');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const path = require('path');
const merge = require('merge-stream');
/****************************************
SOURCE PATHS
*****************************************/
/**
* The 'config' object defines where all the assets are found.
* Changing the values of this object will change where all the tasks below look for files
*/
// Common defaults
const path_src = './assets/src/';
const path_dist = './assets/dist/';
// Pathing config
const config = {
theme: {
name: 'power_dist', // if you change this value, update your file enqueue's too. This is a prefix for all file names (usage example: config.theme.name)
},
css: {
sass: path_src + 'sass/style.scss',
sass_comps: path_src + 'sass/**/*.scss',
sass_sections: './sections/**/*.scss',
vendor_src: path_src + 'vendor/css/**/*.css',
dist: path_dist + 'css/',
},
js: {
src: [
path_src + 'js/**/*.js', // Wildcard - Used as a catch-all. This will add all .js files located within assets/src/js/ to be compiled.
// path_src + 'js/main.js', // Manual - FOR DEPENDENCIES - if you want to control your enqueue order, manually add each file in the order you'd like
],
src_sections: [
'./sections/**/*.js', // Wildcard - Used as a catch-all. This will add all .js files located within assets/src/js/ to be compiled.
// './template-parts/blocks/custom-content/custom-content.js', // Manual - FOR DEPENDENCIES - if you want to control your enqueue order, manually add each file in the order you'd like
],
src_sections_min: './sections/',
src_vendor: [
path_src + 'vendor/js/**/*.js', // Wildcard - used as a catch-all. This will add all .js files located within assets/src/vendor/js/ to be compiled and minfied.
],
dist: path_dist + 'js/',
},
imgs: {
src: [
path_src + 'imgs/*.jpg',
path_src + 'imgs/*.jpeg',
path_src + 'imgs/*.png',
path_src + 'imgs/*.gif',
path_src + 'imgs/*.svg',
path_src + 'imgs/**/*.jpg',
path_src + 'imgs/**/*.jpeg',
path_src + 'imgs/**/*.png',
path_src + 'imgs/**/*.gif',
path_src + 'imgs/**/*.svg',
],
dist: path_dist + 'imgs/',
},
docs: {
serve: './assets/docs',
index: './assets/src/docs/index.html',
json: './assets/src/docs/jsdoc.json',
},
};
/****************************************
HELPER FUNCTIONS
*****************************************/
/**
* Get subfolders within directory
* source: https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-task-steps-per-folder.md
* - Used for Block Scripts
*
* @param {*} dir
*/
function getFolders(dir) {
return fs.readdirSync(dir).filter(function (file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
/****************************************
STANDARD TASKS
*****************************************/
/**
* COMPILE GLOBAL SASS :: UN-MINIFIED & MINIFIED
*/
function styles() {
// Define plugins for "PostCSS"
var plugins_expanded = [autoprefixer()];
var plugins_min = [cssnano()];
// Run SASS Task
return (
gulp
.src(config.css.sass)
.pipe(
plumber(
{
errorHandler: notify.onError(
'SASS Global Error: <%= error.message %>'
),
} // on error, send push
)
)
.pipe(sourcemaps.init()) // Begin SCSS mapping
.pipe(
sass({
outputStyle: 'expanded',
})
)
.pipe(postcss(plugins_expanded))
.pipe(sourcemaps.write()) // Write SCSS maps
.pipe(rename(config.theme.name + '-custom.css'))
.pipe(gulp.dest(config.css.dist)) // DIST un-minified file
// minify for production
// .pipe(rename(config.theme.name + "-custom.min.css")) // rename with .min
// .pipe(postcss(plugins_min)) // minify
.pipe(gulp.dest(config.css.dist))
); // DIST minified version
}
/**
* COMPILE & MINIFY VENDOR & MISC CSS
*/
function styles_vendor() {
return gulp
.src(config.css.vendor_src)
.pipe(
plumber(
{
errorHandler: notify.onError(
'Vendor CSS Error: <%= error.message %>'
),
} // on error, send push
)
)
.pipe(concat(config.theme.name + '-vendor.min.css')) // group files together
.pipe(postcss([cssnano()])) // minify
.pipe(gulp.dest(config.css.dist)); // DIST minified version
}
/**
* COMPILE CUSTOM JS :: UN-MINIFIED & MINIFIED
*/
function scripts_global() {
return gulp
.src(config.js.src)
.pipe(
plumber(
{
errorHandler: notify.onError(
'JS Global Error: <%= error.message %>'
),
} // on error, send push
)
)
.pipe(concat(config.theme.name + '-custom.js')) // group files together
.pipe(gulp.dest(config.js.dist)); // DIST un-minified file
// minify for production
// .pipe(rename(config.theme.name + "-custom.min.js")) // rename with .min
// .pipe(uglify()) // minify
//.pipe(gulp.dest(config.js.dist)) // DIST minified version
}
/**
* COMPILE & MINIFY VENDOR JS
*/
function scripts_vendor() {
return (
gulp
.src(config.js.src_vendor)
.pipe(
plumber(
{
errorHandler: notify.onError(
'Vendor JS Error: <%= error.message %>'
),
} // on error, send push
)
)
.pipe(concat(config.theme.name + '-vendor.js')) // group files together
.pipe(gulp.dest(config.js.dist)) // DIST un-minified file
// minify for production
.pipe(rename(config.theme.name + '-vendor.min.js')) // rename with .min
.pipe(uglify()) // minify
.pipe(gulp.dest(config.js.dist))
); // DIST minified version
}
/**
* OPTIMIZE IMAGES & DIST TO THEME
*/
function images() {
return gulp
.src(config.imgs.src)
.pipe(
plumber(
{
errorHandler: notify.onError(
'Images Error: <%= error.message %>'
),
} // on error, send push
)
)
.pipe(newer(config.imgs.dist)) // check DIST for existing assets
.pipe(
imagemin([
// optimize images per image type
imagemin.gifsicle({ interlaced: true }),
imagemin.jpegtran({ progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [
{
removeViewBox: false,
collapseGroups: true,
},
],
}),
])
)
.pipe(gulp.dest(config.imgs.dist)); // DIST optimized versions
}
/****************************************
DEFINED TASKS
*****************************************/
/**
* BROWSER SYNC
*
* https://browsersync.io/docs/gulp
* This will not reload the browser on every change
* It will just output an IP that is available to any device on the network.
* Meant for Testing PC and Devices.
*/
function browser_sync(done) {
browsersync.init({
proxy: 'http://wp.test:8888', // replace yoursitename with the url of your local site.
open: false,
});
done();
}
/**
* COMMAND LINE
*
* Define a command to run (you may need to 'cd' into the correct directory first)
*/
let shell_cmd = 'echo sample command_line command;';
shell_cmd += 'echo sample 2nd command;'; // Can be a series of commands seperated by ';'
// Run command_line var
gulp.task(
'command_line',
shell.task(shell_cmd, {
shell: 'bash',
})
);
/****************************************
ACTIONS
*****************************************/
// BUILD TASK - COMPILES SCSS, CSS & JS, but does NOT watch for file changes
const build = gulp.series([
styles_vendor,
styles,
scripts_vendor,
scripts_global,
images,
]);
// WATCH AND LOG SOURCE FILE CHANGES
function watch() {
// WATCH SASS / CSS
gulp.watch(config.css.sass, gulp.series([styles])).on('change', (event) => {
console.log('File ' + event + ' was updated, running tasks...');
});
gulp.watch(config.css.sass_comps, gulp.series([styles])).on(
'change',
(event) => {
console.log('File ' + event + ' was updated, running tasks...');
}
);
gulp.watch(config.css.sass_sections, gulp.series([styles])).on(
'change',
(event) => {
console.log(
'File ' + event + ' was updated was it, running tasks...'
);
}
);
gulp.watch(config.css.vendor_src, gulp.series([styles_vendor])).on(
'change',
(event) => {
console.log('File ' + event + ' was updated, running tasks...');
}
);
// WATCH JS
gulp.watch(config.js.src, gulp.series([scripts_global])).on(
'change',
(event) => {
console.log('File ' + event + ' was updated, running tasks...');
}
);
gulp.watch(config.js.src_vendor, gulp.series([scripts_vendor])).on(
'change',
(event) => {
console.log('File ' + event + ' was updated, running tasks...');
}
);
// WATCH IMAGES
gulp.watch(config.imgs.src, gulp.series([images])).on('change', (event) => {
console.log('File ' + event + ' was updated, running tasks...');
});
}
// DEFAULT GULP TASK
const start = gulp.series([build, watch]);
/****************************************
EXPORTS
*****************************************/
exports.styles = styles;
exports.styles_vendor = styles_vendor;
exports.scripts_global = scripts_global;
exports.scripts_vendor = scripts_vendor;
exports.images = images;
exports.browser_sync = browser_sync;
exports.build = build;
exports.watch = watch;
exports.default = start;