-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
263 lines (235 loc) · 6.08 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
// Gulp 3.9.1
// =====================================================================
// TODO:
// - setup SVG settings
// FIXME:
// - plugin 'gulp-changed' dont working for task 'webp'
// Load plugins
var gulp = require('gulp');
var plumber = require('gulp-plumber');
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var postcssObjectFit = require('postcss-object-fit-images');
var minify = require('gulp-csso');
var htmlmin = require('gulp-htmlmin');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var pipeline = require('readable-stream').pipeline;
var rename = require('gulp-rename');
var del = require('del');
var changed = require('gulp-changed');
var imagemin = require('gulp-imagemin');
var webp = require('imagemin-webp');
var svgstore = require('gulp-svgstore');
var ghPages = require('gulp-gh-pages');
var browserSync = require('browser-sync').create();
var runSequence = require('run-sequence');
// Dev Style
// Compile SASS into CSS & auto-inject into browsers
gulp.task('devStyle', function () {
return gulp.src("src/sass/style.scss")
.pipe(plumber())
.pipe(sass())
.pipe(plumber.stop())
.pipe(gulp.dest("src/css"))
.pipe(browserSync.stream());
});
// Prod Style
// Compile SASS into CSS, add Autoprefixer, Minify CSS, Move to Build & auto-inject into browsers
gulp.task('prodStyle', function () {
return gulp.src("src/sass/style.scss")
.pipe(plumber())
.pipe(sass())
.pipe(postcss([
autoprefixer(),
postcssObjectFit()
]))
.pipe(plumber.stop())
.pipe(minify())
.pipe(gulp.dest("build/css"))
.pipe(browserSync.stream());
});
// Minify HTML
gulp.task('html', function () {
return gulp.src("src/*.html")
.pipe(htmlmin({
collapseBooleanAttributes: true,
collapseWhitespace: true,
html5: true,
minifyCSS: true,
minifyJS: true,
processConditionalComments: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
}))
.pipe(gulp.dest("build"));
});
// Concat and Minify Polyfills JS files
gulp.task('polyfills', function () {
return gulp.src('src/js/polyfills/*.js')
.pipe(concat('polyfills-bundle.min.js'))
.pipe(uglify())
.pipe(gulp.dest('build/js'));
});
// Minify JS
gulp.task('script', function () {
return pipeline(
gulp.src('src/js/*.js', !'src/js/polyfills/*.js'),
uglify(),
gulp.dest('build/js')
);
});
// Optimize Images
gulp.task('images', function () {
return gulp.src("src/img/**/*.{png,jpg,svg}")
.pipe(changed("build/img"))
.pipe(imagemin([
imagemin.optipng({
optimizationLevel: 3
}),
imagemin.jpegtran({
progressive: true
}),
// TODO:
// - setup SVG settings (need Practice for Real Projects)
imagemin.svgo({
plugins: [{
removeViewBox: false
}]
})
]))
.pipe(gulp.dest("build/img"));
});
// Convert images to WebP
gulp.task('webp', ['images'], function () {
return gulp.src("src/img/**/*.{png,jpg}")
.pipe(changed("build/img/**/*.webp")) /* FIXME: dont working */
.pipe(imagemin([
webp({
quality: 75
})
]))
.pipe(rename({
extname: ".webp"
}))
.pipe(gulp.dest("build/img"));
});
// TODO:
// - setup SVG settings (need Practice for Real Projects)
// Dev Sprite - Combine SVG files into SVG Sprite
gulp.task('devSprite', function () {
return gulp.src("src/img/svg-sprite/*.svg")
.pipe(svgstore({
inlineSvg: true
}))
.pipe(rename("sprite.svg"))
.pipe(gulp.dest("src/img"));
});
// TODO:
// - setup SVG settings (need Practice for Real Projects)
// Prod Sprite - Combine SVG files into SVG Sprite
gulp.task('prodSprite', function () {
return gulp.src("build/img/svg-sprite/*.svg")
.pipe(svgstore({
inlineSvg: true
}))
.pipe(rename("sprite.svg"))
.pipe(gulp.dest("build/img"));
});
// TODO:
// - Merge tasks 'copy' and 'copyFavicons'
// Copy files
gulp.task('copy', function () {
return gulp.src([
"src/fonts/**/*.{woff,woff2}"
], {
base: "src"
})
.pipe(gulp.dest("build"));
});
// Copy Favicons files
gulp.task('copyFavicons', function () {
return gulp.src([
"src/favicons/*"
])
.pipe(gulp.dest("build"));
});
// Delete files
gulp.task('clean', function () {
return del("build");
});
// Dev Server
gulp.task('devServer', function () {
browserSync.init({
server: "./src",
notify: false,
open: true,
cors: true,
ui: false
});
// Watch files
gulp.watch("src/sass/**/*.scss", ['devStyle']);
gulp.watch("src/*.html")
.on('change', browserSync.reload);
gulp.watch("src/js/*.js")
.on('change', browserSync.reload);
gulp.watch("src/img/**/*")
.on('change', browserSync.reload);
gulp.watch("src/img/svg-sprite/*.svg", ['devSprite'])
.on('change', browserSync.reload);
});
// Prod Server
gulp.task('prodServer', function () {
browserSync.init({
server: "./build",
notify: false,
open: true,
cors: true,
ui: false
});
// Watch files
gulp.watch("src/sass/**/*.scss", ['prodStyle']);
gulp.watch("src/*.html", ['html'])
.on('change', browserSync.reload);
gulp.watch("src/js/*.js", ['script'])
.on('change', browserSync.reload);
gulp.watch("src/img/**/*", ['images', 'webp'])
.on('change', browserSync.reload);
gulp.watch("src/img/svg-sprite/*.svg", ['prodSprite'])
.on('change', browserSync.reload);
});
// Deploy to Github Pages
// =====================================================================
gulp.task('deployGithub', function () {
return gulp.src('./build/**/*')
.pipe(ghPages());
});
// Complex Tasks
// =====================================================================
// DEV
gulp.task('dev', function (done) {
runSequence(
"clean",
"devStyle",
"devSprite",
done
);
});
// PROD
gulp.task('build', function (done) {
runSequence(
"clean",
"html",
"prodStyle",
"polyfills",
"script",
"images",
"webp",
"prodSprite",
"copy",
"copyFavicons",
done
);
});