-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.js
295 lines (255 loc) · 6.55 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
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import del from 'del';
import panini from 'panini';
import BrowserSync from 'browser-sync';
import {output as pagespeed} from 'psi';
import pkg from './package.json';
const browserSync = BrowserSync.create();
const $ = gulpLoadPlugins();
const productionEnv = $.util.env.env === 'production';
const paths = {
appRoot: {
src: 'app/',
dest: 'dist/'
},
styles: {
manifesto: 'app/stylesheets/application.scss',
src: 'app/stylesheets/**/*.{scss, sass}',
dest: 'dist/stylesheets'
},
scripts: {
src: 'app/scripts/**/*.{js, coffee}',
dest: 'dist/scripts/'
},
images: {
src: 'app/assets/images/**/*.{jpg,jpeg,png,gif,webp}',
dest: 'dist/assets/images/'
},
fonts: {
src: 'app/assets/fonts/**/{*.woff, *.woff2}',
dest: 'dist/assets/fonts/'
},
views: {
src: 'app/views/',
dest: 'dist/'
}
};
/*Utilities*/
// Run PageSpeed Insights
export function runPageSpeedInsights(done){
console.log(pkg.homepage)
pagespeed(pkg.homepage, {
strategy: 'mobile'
// By default we use the PageSpeed Insights free (no API key) tier.
// Use a Google Developer API key if you have one: http://goo.gl/RkN0vE
// key: 'YOUR_API_KEY'
})
done()
}
export function handleError(task) {
return function (err) {
$.notify.onError({
message: task + ' failed, check the logs..',
sound: true
})(err);
$.util.log($.util.colors.bgRed(task + ' error:'), $.util.colors.red(err));
this.emit('end');
};
};
/*
* For small tasks you can use arrow functions and export
*/
const clean = (done) => del([ paths.appRoot.dest ], done);
export { clean }
// export function clean(done) {
// del([ paths.appRoot.dest ])
// done()
// }
/*Copy Common App RootFiles */
export function copyRootFiles() {
return gulp.src([paths.appRoot.src + '/*.*', paths.appRoot.src + '/CNAME'], {since: gulp.lastRun('copyRootFiles'), dot: true})
.pipe(gulp.dest(paths.appRoot.dest));
}
/*
* Copy & Optimize static assets
*/
export function images() {
return gulp.src(paths.images.src, {since: gulp.lastRun('images')})
.pipe($.newer(paths.images.dest)) // pass through newer images only
.pipe($.imagemin({
optimizationLevel: 5,
progressive: true,
interlaced: true
}))
.pipe($.if(productionEnv, $.size({title: $.util.colors.bgRed('[SIZE] Images: ')})))
.pipe(gulp.dest(paths.images.dest));
}
/*Copy paste fonts*/
export function fonts() {
return gulp.src(paths.fonts.src, {since: gulp.lastRun('fonts')})
.pipe(gulp.dest(paths.fonts.dest));
}
/*
* STYESHEETS
*/
export function styles() {
return gulp.src(paths.styles.manifesto)
.pipe($.plumber())
.pipe($.if(!productionEnv, $.sourcemaps.init({
loadMaps: true
})))
.pipe($.sass({
precision: 10,
sourceComments: !productionEnv,
outputStyle: productionEnv ? 'compressed' : 'nested'
}))
.on('error', handleError('styles'))
.pipe($.autoprefixer({
browsers: [
'last 2 versions',
'ie >= 10',
'android >= 4.4'
]
}))
.pipe($.if(productionEnv,$.cleanCss()))
.pipe($.rename({
basename: 'app'
}))
.pipe($.if(productionEnv, $.size({title: $.util.colors.bgRed('[SIZE] Styles: ')})))
.pipe($.if(!productionEnv, $.sourcemaps.write({
includeContent: true,
sourceRoot: '.'
})))
.pipe(gulp.dest(paths.styles.dest))
.pipe(browserSync.reload({stream: true}))
}
export function scripts() {
return gulp.src(paths.scripts.src, { sourcemaps: true })
.pipe($.cached('scripts'))
.pipe($.babel())
.pipe($.if(productionEnv, $.uglify()))
.pipe($.remember('scripts'))
.pipe($.concat('app.js'))
.pipe($.if(productionEnv,
$.size({
title: $.util.colors.bgRed('[SIZE] Scripts: ')
})))
.pipe(gulp.dest(paths.scripts.dest));
}
/*
* Static sites like a bauss using Panini by Zurb
*
* repo: https://github.com/zurb/panini
*/
export function views() {
return gulp.src(paths.views.src + 'pages/**/*.html' )
.pipe(panini({
root: paths.views.src + 'pages/',
layouts: paths.views.src + 'layouts/',
partials: paths.views.src + 'partials/**/',
helpers: paths.views.src + 'helpers/',
data: paths.views.src + 'data/'
}))
.pipe(
$.if (productionEnv,
$.htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeOptionalTags: true
})))
.pipe($.if(productionEnv, $.size({
title: $.util.colors.bgRed('[SIZE] Views: '),
showFiles: true
})))
.pipe(gulp.dest(paths.views.dest))
}
export function paniniRefresh(done){
panini.refresh()
done()
}
export function viewsBuildAndStream() {
return views()
.pipe(browserSync.stream())
}
export function paniniRebuild() {
return gulp.series(
paniniRefresh,
viewsBuildAndStream
);
};
/*
* Local server using BrowserSync
*/
export function browserSyncServer(done){
var config = {
server: {
baseDir: paths.appRoot.dest,
}
}
//run TUNNEL=true gulp to start public tunnel url to share.
if (process.env.TUNNEL === 'true') {
config.tunnel = "";
}
browserSync.init(config);
done()
}
/*
* Listen for Changes
*/
export function watch() {
gulp.watch(paths.images.src, images);
gulp.watch(paths.fonts.src, fonts);
gulp.watch(paths.styles.src, styles);
gulp.watch(paths.scripts.src, scripts);
gulp.watch(paths.views.src, paniniRebuild());
$.util.log($.util.colors.bgGreen('Watching for changes...'));
}
/*
* Build
*
* Create a deployable folder
*/
const build = gulp.series(
clean,
gulp.parallel(
images,
fonts,
styles,
scripts,
views
)
);
/*
* Serve
*
* Serve the deployable folder watch for changes and start a dev server
*/
const serve = gulp.series(
build,
gulp.parallel(watch, browserSyncServer)
);
/*
* Deploy To gitHubPages
*
* Serve the deployable folder watch for changes and start a dev server
*/
export function githubPages() {
return gulp.src([paths.appRoot.dest + '**/*.*', paths.appRoot.dest + 'CNAME'])
.pipe($.ghPages());
}
const deploy = gulp.series(
build,
copyRootFiles,
githubPages
);
/* Export const functions */
export { build, serve, deploy};
/* Default gulp task as serve*/
export default serve;