-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
280 lines (235 loc) · 7.12 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
// Load all the modules from package.json
// var gulp = require('gulp'),
// https://www.sitepoint.com/fast-gulp-wordpress-theme-development-workflow/
const { task, src, dest, series, parallel, watch } = require('gulp');
const zip = require('gulp-zip');
const jshint = require( 'gulp-jshint' );
const stylish = require('jshint-stylish');
const uglify = require( 'gulp-uglify' );
const rename = require( 'gulp-rename' );
const notify = require( 'gulp-notify' );
const include = require( 'gulp-include' );
const plumber = require( 'gulp-plumber' );
const autoprefixer = require( 'gulp-autoprefixer' );
const sass = require( 'gulp-sass');
const critical = require('critical');
const imageoptim = require('gulp-imageoptim');
// const mageAlpha = require('gulp-imagealpha');
// gulp-changed, gulp-concat, gulp-imagemin, gulp-line-ending-corrector
// gulp-sourcemaps
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
// automatically reloads the page when files changed
var browserSyncWatchFiles = [
'./*.min.css',
'./js/**/*.min.js',
'./**/*.php'
];
// see: https://www.browsersync.io/docs/options/
var browserSyncOptions = {
watchTask: true,
proxy: "http://localhost:8888/wordpress/",
browser: "google chrome"
}
// browser: ["google chrome", "firefox"]
var config = {
nodeDir: './node_modules'
}
//https://www.youtube.com/watch?v=2HpNiyimo8E
// function watch_files () {
// gulp.watch([ './js/**/*.js', '!./js/dist/*.js' ], )
// }
// Default error handler
var onError = function( err ) {
console.log( 'An error occured:', err.message );
this.emit('end');
}
function buildzip() {
return src([
'*',
'./css/*',
'./fonts/*',
'./images/**/*',
'./inc/**/*',
'./js/**/*',
'./languages/*',
'./sass/**/*',
'./template-parts/*',
'./templates/*',
'!bower_components',
'!node_modules',
'!.g*',
'!gulp*',
'!package*',
'!stanleywp.zip',
], {base: "."})
.pipe(zip('stanleywp.zip'))
.pipe(dest('./outputZip'));
}
function jshintIssues(){
return src( './js/src/*.js' )
.pipe( jshint() )
.pipe( jshint.reporter( stylish ) )
.pipe( jshint.reporter( 'fail' ) );
}
function scripts() {
return src( './js/manifest.js' )
.pipe( include() )
.pipe( rename( { basename: 'scripts' } ) )
.pipe( dest( './js/dist' ) )
// Normal done, time to create the minified javascript (scripts.min.js)
// remove the following 3 lines if you don't want it
.pipe( uglify() )
.pipe( rename( { suffix: '.min' } ) )
.pipe( dest( './js/dist' ) )
.pipe(browserSync.reload({stream: true}))
.pipe( notify({ message: 'scripts task complete' }));
}
// Different options for the Sass tasks
var options = {};
options.sass = {
errLogToConsole: true,
precision: 8,
noCache: true,
//imagePath: 'assets/img',
includePaths: [
config.nodeDir + '/bootstrap/scss',
]
};
options.sassmin = {
errLogToConsole: true,
precision: 8,
noCache: true,
outputStyle: 'compressed',
//imagePath: 'assets/img',
includePaths: [
config.nodeDir + '/bootstrap/scss',
]
};
// Sass
function scss() {
return src('./sass/style.scss')
.pipe(plumber())
.pipe(sass(options.sass).on('error', sass.logError))
.pipe(autoprefixer())
.pipe(dest('.'))
.pipe(browserSync.reload({stream: true}))
.pipe(notify({ title: 'Sass', message: 'sass task complete' }));
}
// Sass-min - Release build minifies CSS after compiling Sass
function scss_min() {
return src('./sass/style.scss')
.pipe(plumber())
.pipe(sass(options.sassmin).on('error', sass.logError))
.pipe(autoprefixer())
.pipe(rename( { suffix: '.min' } ) )
.pipe(dest('.'))
.pipe(browserSync.reload({stream: true}))
.pipe(notify({ title: 'Sass', message: 'sass-min task complete' }));
}
// Optimize Images
function images() {
return src('./images/**/*')
.pipe(imageoptim.optimize())
.pipe(dest('./imagesopt/'))
.pipe( notify({ message: 'Images task complete' }));
}
// .pipe(imageoptim.optimize({jpegmini: true}))
// jpegmini is paid version
// https://jonathanmh.com/tag/gulp/
// https://diezjietal.be/blog/2015/02/18/image-optimizers.html
// https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/automating-image-optimization/
//
// var imagemin = require( 'gulp-imagemin' );
/**
* Task: `imgopt`.
*
* Minifies PNG, JPEG, GIF and SVG images.
*
* This task does the following:
* 1. Gets the source of images raw folder
* 2. Minifies PNG, JPEG, GIF and SVG images
* 3. Generates and saves the optimized images
*
* This task will run only once, if you want to run it
* again, do it with the command `gulp imgopt`.
*/
// gulp.task( 'imgopt', function() {
// gulp.src( imagesSRC )
// .pipe( imagemin({
// progressive: true,
// optimizationLevel: 3, // 0-7 low-high
// interlaced: true,
// svgoPlugins: [{removeViewBox: false}]
// }))
// .pipe( gulp.dest( imagesDestination ) )
// .pipe( notify( { message: 'DONE: Images Optimized! 💯', onLast: true } ) );
// } );
// Generate & Inline Critical-path CSS
function criticalTask (done) {
critical.generate({
base: './',
src: 'http://localhost:8888/wordpress/',
dest: 'css/home.min.css',
ignore: ['@font-face'],
dimensions: [{
width: 320,
height: 480
},{
width: 768,
height: 1024
},{
width: 1280,
height: 960
}],
minify: true
});
done();
}
task('critical', criticalTask );
task ('images', images);
// Sass-min - Release build minifies CSS after compiling Sass
task('sass-min', scss_min);
// sass
task('sass', scss);
// Concatenates all files that it finds in the manifest
// and creates two versions: normal and minified.
// It's dependent on the jshint task to succeed.
task( 'scripts', scripts );
// Jshint outputs any kind of javascript problems you might have
// Only checks javascript files inside /src directory
task( 'jshint', jshintIssues);
// Zip files up for Distribution
task('buildzip', buildzip);
// Starts browser-sync task for starting the server.
// task('browser-sync', function() {
// browserSync.init(browserSyncWatchFiles, browserSyncOptions);
// });
function browser_sync (done) {
browserSync.init(browserSyncWatchFiles, browserSyncOptions);
// browserSync.init({
// server: {
// baseDir: './'
// }
// });
done();
}
task('bSync', browser_sync);
//watcher.on('all', function() {
// console.log('File ' + path + ' was ' + event + ', running tasks...');
//});
var imagessrc = './images/**/*';
var csswatch = './sass/**/*.scss';
var jssrc = [ './js/**/*.js', '!./js/dist/*.js' ];
// watch for file changes
function watch_files () {
watch(csswatch, scss);
watch(imagessrc, images);
watch(jssrc, series(scripts, reload));
src('./style.css')
.pipe( notify({ message: 'Gulp is watching Files, Happy Coding'}) );
}
// task('default', series(parallel( 'watch', 'browser-sync' ), function() {
// Does nothing in this task, just triggers the dependent 'watch'
// }));
task('watch', series(watch_files, browser_sync));