-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
196 lines (179 loc) · 6.35 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
/*
* zeniusEnglish Frontend Kit
* by Wisnu Mulya
*
* Copyright 2015 Zenius Education. All rights reserved
*
* Note: This is a modified version of Google Inc. Web Starter Kit
* https://github.com/google/web-starter-kit
*/
'use strict';
// Inclue Gulp & tools we'll use
var gulp = require('gulp'),
plugins = require('gulp-load-plugins')(),
del = require('del'),
runSequence = require('run-sequence'),
browserSync = require('browser-sync'),
pageSpeed = require('psi'),
reload = browserSync.reload,
path = require('path'),
paths = {
source: './src/',
templates: './src/tpl/',
distribution: './dist/'
},
AUTOPREFIXER_BROWSERS = [
'ie >= 10',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
// Build HTML templates; Copyright 2014 Objects in Space (https://medium.com/objects-in-space/considering-a-static-site-tool-learn-gulp-2fd5f9821fc4)
gulp.task('fileInclude', function() {
return gulp.src(path.join(paths.templates, '*.tpl.html'))
.pipe(plugins.fileInclude())
.pipe(plugins.rename({
extname: ""
}))
.pipe(plugins.rename({
extname: ".html"
}))
.pipe(gulp.dest(paths.source))
.pipe(plugins.size({title: 'fileInclude'}));
});
// Lint JavaScript
gulp.task('jshint', function () {
return gulp.src(path.join(paths.source, 'scripts/*.js'))
.pipe(reload({stream: true, once: true}))
.pipe(plugins.jshint())
.pipe(plugins.jshint.reporter('jshint-stylish'))
.pipe(plugins.if(!browserSync.active, plugins.jshint.reporter('fail')));
});
// Optimize images
gulp.task('images', function () {
return gulp.src(path.join(paths.source, 'images/**/*'))
.pipe(plugins.cache(plugins.imagemin({
progressive: true,
interlaced: true
})))
.pipe(gulp.dest(path.join(paths.distribution, 'images')))
.pipe(plugins.size({title: 'images'}));
});
// Copy all files at the root level (app)
gulp.task('copy', function () {
return gulp.src([
path.join(paths.source, "*"),
'!' + path.join(paths.source, '*.html'),
'node_modules/apache-server-configs/dist/.htaccess'
]).pipe(gulp.dest(paths.distribution))
.pipe(plugins.size({title: 'copy'}));
});
// Copy web fonts to dist
gulp.task('fonts', function () {
return gulp.src([path.join(paths.source, 'fonts/**')])
.pipe(gulp.dest(path.join(paths.distribution, 'fonts')))
.pipe(plugins.size({title: 'fonts'}));
});
// Compile and automatically prefix stylesheets
gulp.task('styles', function () {
// For best performance, don't add Sass partials to `gulp.src`
return gulp.src([
path.join(paths.source, 'styles/**/*.scss')
])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.changed('.tmp/styles', {extension: '.css'}))
.pipe(plugins.sass({
precision: 10,
onError: console.error.bind(console, 'Sass error:')
}))
.pipe(plugins.autoprefixer({browsers: AUTOPREFIXER_BROWSERS}))
.pipe(plugins.sourcemaps.write())
.pipe(gulp.dest('.tmp/styles'))
// Concatenate and minify styles
.pipe(plugins.if('*.css', plugins.csso()))
.pipe(gulp.dest(path.join(paths.distribution, 'styles')))
.pipe(plugins.size({title: 'styles'}));
});
// Scan your HTML for assets & optimize them
gulp.task('html', function () {
var assets = plugins.useref.assets({searchPath: '{.tmp,src}'});
return gulp.src(path.join(paths.source, '*.html'))
.pipe(assets)
// Concatenate and minify JavaScript
.pipe(plugins.if('*.js', plugins.uglify({preserveComments: 'some'})))
// Output concatenated and minified scripts to .tmp
.pipe(plugins.if('*.js', gulp.dest('.tmp')))
// Compress scripts
// .pipe(plugins.if('*.js', plugins.gzip({threshold: true})))
// Remove any unused CSS
.pipe(plugins.if('*.css', plugins.uncss({
html: path.join(paths.source, '*.html')//,
// CSS Selectors for UnCSS to ignore
// ignore: [
// /.navdrawer-container.open/,
// /.app-bar.open/
// ]
})))
// Concatenate and minify styles
// In case you are still using useref build blocks
.pipe(plugins.if('*.css', plugins.csso()))
.pipe(assets.restore())
.pipe(plugins.useref())
// Minify any HTML
.pipe(plugins.if('*.html', plugins.minifyHtml()))
// Output files
.pipe(gulp.dest(paths.distribution))
.pipe(plugins.size({title: 'html'}));
});
// Clean output directory
gulp.task('clean', del.bind(null, ['.tmp', path.join(paths.distribution, '*'), path.join('!', paths.distribution, '.git')], {dot: true}));
// Watch files for changes & reload
gulp.task('serve', ['fileInclude', 'styles'], function () {
browserSync({
notify: false,
// Customize the BrowserSync console logging prefix
logPrefix: 'WSK',
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: ['.tmp', paths.source]
});
gulp.watch([path.join(paths.templates, '**/*.tpl.html')], ['fileInclude', reload]);
gulp.watch([path.join(paths.source, 'styles/**/*.scss')], ['styles', reload]);
gulp.watch([path.join(paths.source, 'scripts/**/**/*.js')], ['jshint', 'html']);
gulp.watch([path.join(paths.source, 'images/**/*')], reload);
});
// Build and serve the output from the dist build
gulp.task('serve:dist', ['default'], function () {
browserSync({
notify: false,
logPrefix: 'WSK',
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: paths.distribution
});
});
// Build production files, the default task
gulp.task('default', ['clean'], function (cb) {
runSequence(['fileInclude', 'styles'], ['html'], ['jshint', 'images', 'fonts', 'copy'], cb);
});
// Run PageSpeed Insights
gulp.task('pagespeed', function (cb) {
// Update the below URL to the public URL of your site
pageSpeed.output('https://zeniusenglish.com/', {
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'
}, cb);
});
// Load custom tasks from the `tasks` directory
// try { require('require-dir')('tasks'); } catch (err) { console.error(err); }