-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gulpfile.js
393 lines (344 loc) · 10.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// data from package.json
var packageJSON = require('./package.json')
// Require our dependencies
const autoprefixer = require( 'autoprefixer' );
const babel = require( 'gulp-babel' );
const browserSync = require( 'browser-sync' );
const cheerio = require( 'gulp-cheerio' );
const concat = require( 'gulp-concat' );
const csslint = require('gulp-csslint' );
const cssnano = require( 'gulp-cssnano' );
const del = require( 'del' );
const eslint = require( 'gulp-eslint' );
const gulp = require( 'gulp' );
const gutil = require( 'gulp-util' );
const globbing = require( 'gulp-css-globbing' );
const imagemin = require( 'gulp-imagemin' );
const mqpacker = require( 'css-mqpacker' );
const notify = require( 'gulp-notify' );
const plumber = require( 'gulp-plumber' );
const postcss = require( 'gulp-postcss' );
const reload = browserSync.reload;
const rename = require( 'gulp-rename' );
const sass = require( 'gulp-sass' );
const sassLint = require( 'gulp-sass-lint' );
const sort = require( 'gulp-sort' );
const sourcemaps = require( 'gulp-sourcemaps' );
const svgmin = require( 'gulp-svgmin' );
const svgstore = require( 'gulp-svgstore' );
const uglify = require( 'gulp-uglify' );
const wpPot = require( 'gulp-wp-pot' );
// Set assets paths.
const paths = {
'css': [ 'assets/css/*.css', '!*.min.css' ],
'icons': 'assets/img/svg-icons/*.svg',
'images': [ 'assets/img/*', '!assets/img/*.svg', 'docs/assets/img/**/*' ],
'php': [ './*.php', './**/*.php' ],
'sass': 'assets/sass/**/*.scss',
'concat_scripts_admin': 'assets/js/src/admin/*.js',
'concat_scripts_front_end': 'assets/js/src/front-end/*.js',
'scripts': [ 'assets/js/*.js', '!assets/js/*.min.js' ]
};
/**
* Handle errors and alert the user.
*/
function handleErrors () {
const args = Array.prototype.slice.call( arguments );
notify.onError( {
'title': 'Task Failed [<%= error.message %>',
'message': 'See console.',
'sound': 'Sosumi' // See: https://github.com/mikaelbr/node-notifier#all-notification-options-with-their-defaults
} ).apply( this, args );
gutil.beep(); // Beep 'sosumi' again.
// Prevent the 'watch' task from stopping.
this.emit( 'end' );
}
/**
* Delete project-admin.css and project-admin.min.css before we minify and optimize
*/
gulp.task( 'clean:styles', () =>
del( [ 'assets/css/' + packageJSON.name + '-admin.css', 'assets/css/' + packageJSON.name + '-admin.min.css', 'assets/css/' + packageJSON.name + '-front-end.css', 'assets/css/' + packageJSON.name + '-front-end.min.css' ] )
);
/**
* Compile Sass and run stylesheet through PostCSS.
*
* https://www.npmjs.com/package/gulp-sass
* https://www.npmjs.com/package/gulp-postcss
* https://www.npmjs.com/package/gulp-autoprefixer
* https://www.npmjs.com/package/css-mqpacker
*/
gulp.task( 'postcss', [ 'clean:styles' ], () =>
gulp.src( 'assets/sass/*.scss', paths.css )
// Deal with errors.
.pipe( plumber( {'errorHandler': handleErrors} ) )
// Wrap tasks in a sourcemap.
.pipe( sourcemaps.init() )
// glob files together
.pipe(globbing({
// Configure it to use SCSS files
extensions: ['.scss']
}))
// Compile Sass using LibSass.
.pipe( sass( {
'errLogToConsole': true,
'outputStyle': 'expanded' // Options: nested, expanded, compact, compressed
} ) )
// Parse with PostCSS plugins.
.pipe( postcss( [
autoprefixer( {
'browsers': [ 'last 2 version' ]
} ),
mqpacker( {
'sort': true
} )
] ) )
// Create sourcemap.
.pipe( sourcemaps.write() )
// Create project-admin.css.
.pipe( gulp.dest( 'assets/css/' ) )
.pipe( browserSync.stream() )
);
/**
* Minify and optimize project-admin.css.
*
* https://www.npmjs.com/package/gulp-cssnano
*/
gulp.task( 'cssnano', [ 'postcss' ], () =>
gulp.src( 'assets/css/' + packageJSON.name + '-admin.css' )
.pipe( plumber( {'errorHandler': handleErrors} ) )
.pipe( cssnano( {
'safe': true // Use safe optimizations.
} ) )
.pipe( rename( packageJSON.name + '-admin.min.css' ) )
.pipe( gulp.dest( 'assets/css' ) )
.pipe( browserSync.stream() )
);
/**
* Minify and optimize project-admin.css.
*
* https://www.npmjs.com/package/gulp-cssnano
*/
gulp.task( 'cssnano-frontend', [ 'postcss' ], () =>
gulp.src( 'assets/css/' + packageJSON.name + '-front-end.css' )
.pipe( plumber( {'errorHandler': handleErrors} ) )
.pipe( cssnano( {
'safe': true // Use safe optimizations.
} ) )
.pipe( rename( packageJSON.name + '-front-end.min.css' ) )
.pipe( gulp.dest( 'assets/css' ) )
.pipe( browserSync.stream() )
);
/**
* Delete the svg-icons.svg before we minify, concat.
*/
gulp.task( 'clean:icons', () =>
del( [ 'assets/img/svg-icons.svg' ] )
);
/**
* Minify, concatenate, and clean SVG icons.
*
* https://www.npmjs.com/package/gulp-svgmin
* https://www.npmjs.com/package/gulp-svgstore
* https://www.npmjs.com/package/gulp-cheerio
*/
gulp.task( 'svg', [ 'clean:icons' ], () =>
gulp.src( paths.icons )
// Deal with errors.
.pipe( plumber( {'errorHandler': handleErrors} ) )
// Minify SVGs.
.pipe( svgmin() )
// Add a prefix to SVG IDs.
.pipe( rename( {'prefix': 'icon-'} ) )
// Combine all SVGs into a single <symbol>
.pipe( svgstore( {'inlineSvg': true} ) )
// Clean up the <symbol> by removing the following cruft...
.pipe( cheerio( {
'run': function ( $, file ) {
$( 'svg' ).attr( 'style', 'display:none' );
$( '[fill]' ).removeAttr( 'fill' );
$( 'path' ).removeAttr( 'class' );
},
'parserOptions': {'xmlMode': true}
} ) )
// Save svg-icons.svg.
.pipe( gulp.dest( 'assets/img/' ) )
.pipe( browserSync.stream() )
);
/**
* Optimize images.
*
* https://www.npmjs.com/package/gulp-imagemin
*/
gulp.task( 'imagemin', () =>
gulp.src( paths.images, {base: "./"} )
.pipe( plumber( {'errorHandler': handleErrors} ) )
.pipe( imagemin( {
'optimizationLevel': 5,
'progressive': true,
'interlaced': true
} ) )
.pipe( gulp.dest("./") )
);
/**
* Concatenate and transform JavaScript.
*
* https://www.npmjs.com/package/gulp-concat
* https://github.com/babel/gulp-babel
* https://www.npmjs.com/package/gulp-sourcemaps
*/
gulp.task( 'concat-admin', () =>
gulp.src( paths.concat_scripts_admin )
// Deal with errors.
.pipe( plumber(
{'errorHandler': handleErrors}
) )
// Start a sourcemap.
.pipe( sourcemaps.init() )
// Convert ES6+ to ES2015.
.pipe( babel( {
presets: [ 'env' ]
} ) )
// Concatenate partials into a single script.
.pipe( concat( packageJSON.name + '-admin.js' ) )
// Append the sourcemap to project-admin.js.
.pipe( sourcemaps.write() )
// Save project-admin.js
.pipe( gulp.dest( 'assets/js' ) )
.pipe( browserSync.stream() )
);
gulp.task( 'concat-front-end', () =>
gulp.src( paths.concat_scripts_front_end )
// Deal with errors.
.pipe( plumber(
{'errorHandler': handleErrors}
) )
// Start a sourcemap.
.pipe( sourcemaps.init() )
// Convert ES6+ to ES2015.
.pipe( babel( {
presets: [ 'env' ]
} ) )
// Concatenate partials into a single script.
.pipe( concat( packageJSON.name + '-front-end.js' ) )
// Append the sourcemap to project-front-end.js.
.pipe( sourcemaps.write() )
// Save project-front-end.js
.pipe( gulp.dest( 'assets/js' ) )
.pipe( browserSync.stream() )
);
/**
* Minify compiled JavaScript.
*
* https://www.npmjs.com/package/gulp-uglify
*/
gulp.task( 'uglify', [ 'concat-admin', 'concat-front-end' ], () =>
gulp.src( paths.scripts )
.pipe( rename( {'suffix': '.min'} ) )
.pipe( uglify( {
'mangle': false
} ) )
.pipe( gulp.dest( 'assets/js' ) )
);
/**
* Delete the plugin's .pot before we create a new one.
*/
gulp.task( 'clean:pot', () =>
del( [ 'languages/' + packageJSON.name + '.pot' ] )
);
/**
* Scan the plugin and create a POT file.
*
* https://www.npmjs.com/package/gulp-wp-pot
*/
gulp.task( 'wp-pot', [ 'clean:pot' ], () =>
gulp.src( paths.php )
.pipe( plumber( {'errorHandler': handleErrors} ) )
.pipe( sort() )
.pipe( wpPot( {
'domain': packageJSON.name,
'package': packageJSON.name,
} ) )
.pipe( gulp.dest( 'languages/' + packageJSON.name + '.pot' ) )
);
/**
* Sass linting.
*
* https://www.npmjs.com/package/sass-lint
*/
gulp.task( 'sass:lint', () =>
gulp.src( [
'assets/sass/**/*.scss',
'!node_modules/**'
] )
.pipe( sassLint() )
.pipe( sassLint.format() )
.pipe( sassLint.failOnError() )
);
/**
* CSS linting.
*
* https://www.npmjs.com/package/gulp-csslint
*/
gulp.task( 'css:lint', () =>
gulp.src( [
'assets/css/**/*.css',
'!assets/css/**/*.min.css'
] )
.pipe( csslint() )
.pipe( csslint.formatter() )
);
/**
* JavaScript linting.
*
* https://www.npmjs.com/package/gulp-eslint
*/
gulp.task('js:lint', () => {
// ESLint ignores files with "node_modules" paths.
// So, it's best to have gulp ignore the directory as well.
// Also, Be sure to return the stream from the task;
// Otherwise, the task may end before the stream has finished.
return gulp.src(['**/*.js','!node_modules/**', '!**.min.js'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint({
fix: true
}))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});
/**
* Process tasks and reload browsers on file changes.
*
* https://www.npmjs.com/package/browser-sync
*/
gulp.task( 'watch', function () {
// Kick off BrowserSync.
browserSync( {
'open': false, // Open project in a new tab?
'injectChanges': true, // Auto inject changes instead of full reload.
'proxy': 'testing.dev', // Use http://largo.com:3000 to use BrowserSync.
'watchOptions': {
'debounceDelay': 1000 // Wait 1 second before injecting.
}
} );
// Run tasks when files change.
gulp.watch( paths.icons, [ 'icons' ] );
gulp.watch( paths.sass, [ 'styles' ] );
gulp.watch( paths.scripts, [ 'scripts' ] );
gulp.watch( paths.concat_scripts, [ 'scripts' ] );
gulp.watch( paths.php, [ 'markup' ] );
} );
/**
* Create individual tasks.
*/
gulp.task( 'markup', browserSync.reload );
gulp.task( 'i18n', [ 'wp-pot' ] );
gulp.task( 'icons', [ 'svg' ] );
gulp.task( 'scripts', [ 'uglify' ] );
gulp.task( 'styles', [ 'cssnano', 'cssnano-frontend' ] );
gulp.task( 'lint', [ 'sass:lint', 'js:lint' ] );
gulp.task( 'default', [ 'i18n', 'icons', 'styles', 'scripts', 'imagemin'] );