-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
320 lines (282 loc) · 8.57 KB
/
gulpfile.babel.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
'use-strict';
/* eslint no-console: 0 */
/**
* Gulpfile.
* * setup your configurations in "gulpwp.config.js"
*
* Gulp with WordPress.
*
* Implements:
* * 1. Live reloads browser with BrowserSync (InjectCSS instead of browser page reload).
* * 2. CSS: Sass to CSS conversion, error catching, Autoprefixing, Sourcemaps, CSS minification
* * 3. JS: Concatenates & uglifies Vendor and Custom JS files.
* * 4. Watches files for changes in CSS, JS, PHP, IMGS.
*
* @author Shane Biggs <https://twitter.com/moxbiggs/>
*/
import { config } from './gulpwp.config.js'; // set your URL and file paths here
import { src, dest, watch, series, parallel } from 'gulp';
import autoprefixer from 'autoprefixer';
import babel from 'rollup-plugin-babel';
import beep from 'beepbeep';
import { create } from 'browser-sync';
import commonjs from 'rollup-plugin-commonjs';
import concat from 'gulp-concat';
import cssnano from 'cssnano';
import del from 'del';
import fs from 'fs';
import minimatch from 'minimatch';
import newer from 'gulp-newer';
import notify from 'gulp-notify';
import plumber from 'gulp-plumber';
import postcss from 'gulp-postcss';
import rename from 'gulp-rename';
import resolve from 'rollup-plugin-node-resolve';
import rollup from 'gulp-better-rollup';
import sass from 'gulp-sass';
import sourcemaps from 'gulp-sourcemaps';
import uglify from 'rollup-plugin-uglify';
import unzip from 'gulp-unzip';
const browserSync = create();
/**
* Custom Error Handler.
*
* @param {string} error
*/
const errorHandler = ( error ) => {
notify.onError( '\n\n❌ ===> ERROR: <%= error.message %>\n' )( error );
beep();
};
/**
* Task: `server`.
* * uses Browsersync for live Reloads, CSS injections, Localhost tunneling.
* { @link http://www.browsersync.io/docs/options/ }
*
* @callback requestCallback
* @param {requestCallback} done - The response when finished
*
* run with `gulp server`
*/
export function server( done ) {
browserSync.init( {
proxy: config.projectURL,
port: 3000,
injectChanges: true,
} );
done();
watchFiles();
}
/**
* Task: `reload`.
* * helper function to reload BrowserSync server
* * used in watchFiles function
*
* * run with `gulp reload`
*
* @callback requestCallback
* @param {requestCallback} done - The response when finished
*/
export function reload( done ) {
browserSync.reload();
done();
}
/**
* Task: `watchFiles`.
* * watches for changes to php, sass, js, imgs
* * runs coresponding functions if changes
* * reloads BrowserSync
*
* * run with `gulp watchFiles`
*/
export function watchFiles() {
watch( config.phpSrc, reload );
watch( config.imgsSrc, reload );
watch( config.sassSrc, series( css, reload ) );
watch( config.jsSrc, series( js, reload ) );
watch( config.jsVendorSrc, series( jsVendor, reload ) );
}
/**
* Task: `css`.
* * compiles sass to css, runs autoprefixer and minimizes css
* * copies css to ./style.css with sourcemap
*
* * run with `gulp css`
*/
export function css() {
return src( config.sassSrc, { allowEmpty: true } )
.pipe( plumber( errorHandler ) ) // initialize plumber with error handling
.pipe( sourcemaps.init() ) // initialize sourcemaps
.pipe( sass().on( 'error', sass.logError ) ) // convert sass and compress
.pipe( postcss( [ autoprefixer( { grid: true } ), cssnano() ] ) ) // run postcss autoprefixer, cssnano to minify
.pipe( sourcemaps.write( '.' ) ) // write sourcemaps file in current directory
.pipe( plumber.stop() )
.pipe( dest( config.sassDest ) ) // put CSS and sourcemaps in dist/css folder
.pipe( browserSync.stream() )
.pipe( notify( { message: '✅ 👍 ✅ Completed Task: "css"', onLast: true } ) );
}
/**
* Task: `js`.
* * compiles JS: create sourcemaps, transpile with Babel, rename file, minify output
* * copies js files to ./js/min with sourcemap
*
* * run with `gulp js`
*/
export function js() {
return src( config.jsSrc, { allowEmpty: true } )
.pipe( newer( config.jsDest ) ) // only run on newer files
.pipe( plumber( errorHandler ) ) // initialize plumber first
.pipe( sourcemaps.init() ) // initialize sourcemaps
.pipe( rollup( {
plugins: [
resolve(),
commonjs(),
babel(),
uglify.uglify(), // minify js
],
}, {
format: 'iife',
} ) )
.pipe( rename( { suffix: '.min' } ) ) // rename file to use .min.js
.pipe( sourcemaps.write( '.' ) ) // write sourcemaps file in current directory
.pipe( plumber.stop() )
.pipe( dest( config.jsDest ) ) // put js files and sourcemaps in dist/js folder
.pipe( browserSync.stream() )
.pipe( notify( { message: '✅ 👍 ✅ Completed Task: "js"', onLast: true } ) );
}
/**
* Task: `jsVendor`.
* * compiles Vendor JS: create sourcemaps, transpile with Babel, rename file, minify output
* * copies js files to ./js/min with sourcemap
*
* * run with `gulp jsVendor`
*/
export function jsVendor() {
return src( config.jsVendorSrc, { allowEmpty: true } )
.pipe( newer( config.jsVendorDest ) ) // only run on newer files
.pipe( plumber( errorHandler ) ) // initialize plumber first
.pipe( sourcemaps.init() ) // initialize sourcemaps
.pipe( rollup( {
plugins: [
resolve(),
commonjs(),
babel(),
uglify.uglify(), // minify js
],
}, {
format: 'iife',
} ) )
.pipe( rename( { suffix: '.min' } ) ) // rename file to use .min.js
.pipe( sourcemaps.write( '.' ) ) // write sourcemaps file in current directory
.pipe( plumber.stop() )
.pipe( dest( config.jsVendorDest ) ) // put js files and sourcemaps in dist/js folder
.pipe( browserSync.stream() )
.pipe( notify( { message: '✅ 👍 ✅ Completed Task: "jsVendor"', onLast: true } ) );
}
/**
* ! OPTIONAL FUNCTIONS
* * MUST RUN THESE FUNCTIONS MANUALLY
* * `gulp build` and `gulp fonts`
*/
/**
* Task: `unzipFonts`.
* * unzips webfonts into ./fonts
*/
function unzipFonts() {
return src( config.fontsSrc, { allowEmpty: true } )
.pipe( unzip( {
filter( entry ) {
return minimatch( entry.path, config.fontsInclude );
},
} ) )
.pipe( dest( config.fontsDest ) )
.pipe( browserSync.stream() )
.pipe( notify( { message: '✅ 👍 ✅ Completed Task: "unzipFonts"', onLast: true } ) );
}
/**
* Task: `createFontsCSS`.
* * extracts and creates the fonts css
*/
function createFontsCSS() {
return src( config.fontsSrc, { allowEmpty: true } )
.pipe( unzip( {
filter( entry ) {
return minimatch( entry.path, config.fontsCSSInclude );
},
} ) )
.pipe( concat( config.fontsCSSFilename ) )
.pipe( dest( config.fontsCSSDest ) )
.pipe( browserSync.stream() )
.pipe( notify( { message: '✅ 👍 ✅ Completed Task: "createFontsCSS"', onLast: true } ) );
}
/**
* Task: `addFontsCSSToSass`.
* * adds fonts css to "sass/variables-site/_typography.scss"
*/
function addFontsCSSToSass() {
return src( config.fontsSassSrc, { allowEmpty: true } )
.pipe( concat( '_typography.scss' ) )
.pipe( dest( config.fontsSassDest ) )
.pipe( browserSync.stream() )
.pipe( notify( { message: '✅ 👍 ✅ Completed Task: "addFontsCSSToSass"', onLast: true } ) );
}
/**
* Task: `cleanFonts`.
* * deletes unecessary leftover files
*/
function cleanFonts() {
return ( del( [ config.fontsCSSDest, config.fontsSrc ] ) );
}
/**
* Task: `deletePHPCS`.
* * deletes the _underscores "phpcs.xml.dist" file
*/
function deletePHPCS() {
return ( del( [ './phpcs.xml.dist' ] ) );
}
/**
* Task: `makeFolders`.
* * creates the ./fonts and ./imgs file structure
* * add desired folders to the folders array
*
* @callback requestCallback
* @param {requestCallback} done - The response when finished
*/
function makeFolders( done ) {
const folders = [
'imgs',
'fonts',
'js/vendor',
];
folders.forEach( ( dir ) => {
if ( ! fs.existsSync( dir ) ) {
fs.mkdirSync( dir );
console.log( { message: '📁 folder created:' + dir, onLast: true } );
}
} );
done();
}
/**
* ! Gulp default:
* * starts BrowserSync server
* * watches files for changes and reloads browser if changes
*
* * run with `gulp`
*/
exports.default = series( parallel( css, js, jsVendor ), server );
/**
* Task: `build`.
* * deletes "phpcs.xml.dist" file
* * creates declared folders
*
* * run with `gulp build`
*/
exports.build = series( deletePHPCS, makeFolders );
/**
* Task: `fonts`.
* * unzips fonts
* * extracts the css and adds it to "sass/variables-site/_typography.scss"
* * deletes uncessary files
*
* * run with `gulp fonts`
*/
exports.fonts = series( unzipFonts, createFontsCSS, addFontsCSSToSass, cleanFonts );