-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
245 lines (209 loc) · 4.45 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
/**
* Genesis Sample Sass Workflow.
*
* This file adds Gulp tasks to the theme.
*
* @author Sridhar Katakam
*/
// Require our dependencies.
const autoprefixer = require('autoprefixer');
const browserSync = require('browser-sync').create();
const cleancss = require('gulp-clean-css');
const cssnano = require('gulp-cssnano');
const gulp = require('gulp');
const minify = require('gulp-minify');
const mqpacker = require('css-mqpacker');
const notify = require('gulp-notify');
const pixrem = require('gulp-pixrem');
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
// Project specific variables - CHANGE THESE
const siteName = 'genesis-sample.test'; // set your siteName here
const userName = 'sridharkatakam'; // set your Mac OS userName here
/**
* Error handling
*
* @function
*/
function handleErrors() {
const args = Array.prototype.slice.call(arguments);
notify
.onError({
title: 'Task Failed [<%= error.message %>]',
message:
'<%= error %> - See console or enable logging in the plugin.'
})
.apply(this, args);
// Prevent the 'watch' task from stopping
this.emit('end');
}
/*************
* CSS Tasks
************/
/**
* PostCSS Task Handler
*/
gulp.task('postcss', () => {
gulp
.src('./sass/style.scss')
// Error handling.
.pipe(
plumber({
errorHandler: handleErrors
})
)
// Wrap tasks in a sourcemap.
.pipe(sourcemaps.init())
// Sass magic.
.pipe(
sass({
errLogToConsole: true,
outputStyle: 'expanded' // Options: nested, expanded, compact, compressed
})
)
// Pixel fallbacks for rem units.
.pipe(pixrem())
// PostCSS magic.
.pipe(
postcss([
autoprefixer(),
mqpacker({
sort: true
})
])
)
// Create the source map.
.pipe(
sourcemaps.write('./', {
includeContent: false
})
)
// Write the CSS file.
.pipe(gulp.dest('./'))
// Inject CSS into Browser.
.pipe(browserSync.stream());
});
/**
* Minify style.css
*/
gulp.task('css:minify', ['postcss'], () => {
gulp
.src('./style.css')
// Error handling.
.pipe(
plumber({
errorHandler: handleErrors
})
)
// Combine similar rules.
.pipe(
cleancss({
level: {
2: {
all: true
}
}
})
)
// Minify and optimize style.css.
.pipe(
cssnano({
safe: false,
discardComments: {
removeAll: true
}
})
)
// Rename the file.
.pipe(rename('style.min.css'))
// Write the file.
.pipe(gulp.dest('./'))
// Inject the CSS into the browser.
.pipe(browserSync.stream())
.pipe(
notify({
message: 'Styles are built.'
})
);
});
/*******************
* JavaScript Tasks
*******************/
/**
* JavaScript Task Handler.
*/
gulp.task('js', () => {
gulp
.src(['!./js/*.min.js', './js/*.js'])
// Error handling.
.pipe(
plumber({
errorHandler: handleErrors
})
)
// Minify JavaScript.
.pipe(
minify({
ext: {
src: '.js',
min: '.min.js'
},
noSource: true
})
)
.pipe(gulp.dest('js'))
// Inject changes via browserSync.
.pipe(
browserSync.reload({
stream: true
})
)
.pipe(
notify({
message: 'Scripts are minified.'
})
);
});
/**********************
* All Tasks Listeners
*********************/
/**
* Reload browser for PHP & JS file changes and inject CSS changes.
*
* https://browsersync.io/docs/gulp
*/
gulp.task('watch', () => {
browserSync.init({
proxy: `https://${siteName}`,
host: siteName,
open: 'external',
port: 8000,
https: {
key: `/Users/${userName}/.valet/Certificates/${siteName}.key`,
cert: `/Users/${userName}/.valet/Certificates/${siteName}.crt`
}
});
// Watch Scss files. Changes are injected into the browser from within the task.
gulp.watch('./sass/**/*.scss', ['styles']);
// Watch JavaScript Files. The task tries to inject changes into the browser. If that's not possible, it reloads the browser.
gulp.watch(['./js/*.js', '!./js/*.min.js'], ['scripts']);
// Watch PHP files and reload the browser if there is a change. Add directories if needed.
gulp
.watch([
'./*.php',
'./lib/*.php',
'./lib/**/*.php'
])
.on('change', browserSync.reload);
});
/********************
* Individual tasks.
*******************/
gulp.task('styles', ['css:minify']);
gulp.task('scripts', ['js']);
gulp.task('default', ['watch'], () => {
gulp.start('styles', 'scripts');
});