-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
168 lines (143 loc) · 4.19 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
// General purpose
var gulp = require('gulp');
var cache = require('gulp-cached'); // to only process files that have changed
var sourcemaps = require('gulp-sourcemaps'); // to have nice debugging in developper tools even after minification
var rename = require('gulp-rename');
var del = require('del');
var cp = require('glob-cp');
var concat = require('gulp-concat');
// Images
var webp = require('imagemin-webp');
var imagemin = require('gulp-imagemin');
var responsive = require('gulp-responsive-images');
// CSS
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');
// HTML
var googlecdn = require('gulp-google-cdn');
var htmlmin = require('gulp-htmlmin');
// JS
var uglify = require('gulp-uglify');
// Folder structure
var imgOr = 'src/img-original/',
imgSrc = 'src/img/',
webpSrc = imgSrc + '/webp/',
imgDist = 'dist/img/',
cssSrc = 'src/css/',
cssDist = 'dist/css/',
jsSrc = 'src/js/',
jsDist = 'dist/js';
// Clear the whole cache
cache.caches = {};
// ===========
// SRC CHORES
// ===========
var responsiveOptions = {
'gorilla.png': [{
width: 35,
suffix: '-pin'
}],
'sugar-cane.png': [{
width: 35,
suffix: '-pin'
}],
'*': [{
width: 120
}]
};
// .webp generation and compression
gulp.task('webp', function () {
return gulp.src(imgOr + '*.{jpg,jpeg,png}')
.pipe(responsive(responsiveOptions))
.pipe(cache('webp-cache'))
.pipe(webp()())
.pipe(gulp.dest(webpSrc));
});
// Image compression
gulp.task('imagemin', function () {
var options = {
optimizationLevel: 5, // .png
progressive: true // .jpg
};
return gulp.src(imgOr + '*.{jpg,jpeg,png}')
.pipe(cache('imagemin-cache'))
.pipe(imagemin(options))
.pipe(responsive(responsiveOptions))
.pipe(gulp.dest(imgSrc));
});
gulp.task('css', function () {
var autoprefixerOptions = {
browsers: ['> 1%'],
cascade: false
};
return gulp.src([cssSrc + '*.css', '!' + cssSrc + '*min.css'])
.pipe(cache('css-cache'))
.pipe(sourcemaps.init())
.pipe(postcss([ autoprefixer(autoprefixerOptions), cssnano() ]))
.pipe(rename({suffix: '.min'}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(cssSrc));
});
// Merge css files for dist? See gulp-concat
gulp.task('js', function() {
return gulp.src([jsSrc + '**/*.js', '!' + jsSrc + 'app.js', '!' + jsSrc + 'uglified.js'])
.pipe(sourcemaps.init())
.pipe(concat('uglified.js'))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(jsSrc));
});
// Replaces script references with Google CDN ones (when using bower)
// gulp.task('googlecdn', function () {
// return gulp.src('./src/index.html')
// .pipe(googlecdn(require('./src/bower.json')))
// .pipe(rename({suffix: '.test'}))
// .pipe(gulp.dest('src'));
// });
// Watcher
gulp.task('watch', function () {
gulp.watch(imgOr + '*.{jpg,jpeg,png}', ['webp', 'imagemin']);
gulp.watch([cssSrc + '*.css', '!' + cssSrc + '*min.css'], ['css']);
gulp.watch([jsSrc + '**/*.js', '!' + jsSrc + 'uglified.js'], ['js']);
});
// ==================
// SRC TO DIST CHORES
// ==================
gulp.task('cleanDist', function () {
del([imgDist]);
del([cssDist]);
return del([jsDist]);
});
// populateDist wait that cleanDist finishes
gulp.task('populateDist', ['cleanDist'], function () {
var options = { recursive: true };
cp(imgSrc, imgDist, options);
cp(cssSrc, cssDist, options);
cp('src/bower_components', 'dist/bower_components', options);
gulp.src([jsSrc + 'uglified.js', jsSrc + 'app.js'])
.pipe(gulp.dest(jsDist));
});
gulp.task('htmlmin', function () {
var options = {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
conservativeCollapse: true,
preserveLineBreaks: true,
minifyJS: true,
minifyCSS: true
};
return gulp.src('src/*.html')
.pipe(htmlmin(options))
.pipe(gulp.dest('dist/'))
});
// ===============
// COMPOSITE TASKS
// ===============
gulp.task('default', function () {
console.log('This is the only thing I do :)');
});
gulp.task('athome', ['webp', 'imagemin', 'css', 'js', 'watch']);
// Careful! Order has no importance, tasks run asynchronously by default
gulp.task('dist', ['cleanDist', 'populateDist', 'htmlmin']);