-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
236 lines (185 loc) · 5.34 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
/**
* Main Gulp file
*
* This main Gulp file imports all modules and
* creates all Gulp tasks accordingly.
*
* Table of contents
* 1) Load plugins
* 2) Styles
* 3) Scripts
* 4) Images
* 5) SVG
* 6) Critical CSS
* 7) Purify CSS
* 8) Clean
* 9) Clear
* 10) Default
* 11) Watch
*/
/**
* 1) Load plugins
*
* Require all modules and define variables.
*/
var gulp = require('gulp'),
browserSync = require('browser-sync').create(),
del = require('del'),
critical = require('critical'),
$ = require('gulp-load-plugins')();
/**
* 2) Styles
*
* First find all .scss-files within the /assets/scss/ directory,
* after that autoprefix what's necessary, then minify, finally move
* to /assets/dist/css/style.css.
*/
gulp.task('styles', function() {
return gulp.src('assets/scss/styles.scss')
.pipe($.sass())
.pipe($.autoprefixer('last 2 versions', 'Firefox ESR', 'Opera 12.1'))
.pipe($.cssnano())
.pipe(gulp.dest('assets/dist/css'))
.pipe(browserSync.stream())
.pipe($.notify({ title: 'Styles', message: 'Task completed' }))
});
gulp.task('styles-ie', function() {
return gulp.src('assets/scss/ie.scss')
.pipe($.sass())
.pipe($.autoprefixer('ie 8', 'ie 9'))
.pipe($.pixrem({ rootValue: '1em' }))
.pipe($.cssnano())
.pipe(gulp.dest('assets/dist/css'))
.pipe(browserSync.stream())
.pipe($.notify({ title: 'Styles', message: 'Task completed' }))
});
/**
* 3) Scripts
*
* First find all .js-files withing the /assets/js/ directory,
* concat all files, add the .min-suffix, uglify and then move to /assets/dist/js.
*/
gulp.task('scripts-head', function() {
return gulp.src(['assets/js/head.js', 'assets/js/vendor/modernizr.js'])
.pipe($.concat('head.js'))
.pipe($.rename({ suffix: '.min' }))
.pipe($.uglify())
.pipe(gulp.dest('assets/dist/js'))
.pipe(browserSync.stream())
.pipe($.notify({ title: 'Scripts', message: 'Task completed' }))
});
gulp.task('scripts', function() {
return gulp.src(['node_modules/jquery/dist/jquery.js', 'assets/js/vendor/*.js', 'assets/js/**/*.js', '!assets/js/head.js', '!assets/js/vendor/modernizr.js'])
.pipe($.concat('main.js'))
.pipe($.rename({ suffix: '.min' }))
.pipe($.uglify())
.pipe(gulp.dest('assets/dist/js'))
.pipe(browserSync.stream())
.pipe($.notify({ title: 'Scripts', message: 'Task completed' }))
});
/**
* 4) Images
*
* First find all files within the /assets/img/ directory,
* after that optimize the images and save them to /assets/dist/img.
*/
gulp.task('images', function() {
return gulp.src('assets/img/**/*')
.pipe($.cache($.imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(gulp.dest('assets/dist/img'))
.pipe(browserSync.stream())
.pipe($.notify({ title: 'Images', message: 'Task completed' }))
});
/**
* 5) SVG
*
* First find all SVG files within the /assets/img/ directory,
* after that optimize SVGs, make fallback PNGs and save them to /assets/img/icons
*/
gulp.task('svg2png', function() {
return gulp.src('assets/img/**/*.svg')
.pipe($.svg2png())
.pipe(gulp.dest('assets/dist/img/icons/png'))
.pipe($.notify({ title: 'SVG 2 PNG', message: 'Task completed' }))
});
gulp.task('svg', ['svg2png'], function() {
return gulp.src('assets/img/**/*.svg')
.pipe(
$.svgSymbols({
className: '.icon--%f'
})
)
.pipe(gulp.dest('assets/dist/img/icons'))
.pipe($.notify({ title: 'SVG', message: 'Task completed' }))
});
/**
* 6) Critical CSS
*
* When we type `$ gulp critical` in the command line,
* this command will extract the critical css and inlines HTML and CSS.
*
* WARNING: you should have a play with it's output and change the `dest` while you're at it.
* You can also chose to ignore multiple selectors from your css.
*/
gulp.task('critical', function() {
critical.generate({
inline: true,
base: './',
src: 'index.html',
css: ['assets/dist/css/styles.css'],
dest: 'index.html',
minify: true,
width: 1300,
height: 900,
ignore: ['body.debug:before']
});
});
/**
* 7) Purify CSS
*
* When we tye `$ gulp purify` in the command line,
* this command will report what CSS is not being used in your terminal.
*/
gulp.task('purify', function() {
return gulp.src('assets/dist/css/styles.css')
.pipe($.purifycss(['index.html'], { output: false, info: true, rejected: true }));
});
/**
* 8) Clean
*
* Clean the dist folder prior to moving to production.
*/
gulp.task('clean', function() {
return del(['assets/dist/css', 'assets/dist/js', 'assets/dist/img']);
});
/**
* 9) Clear
*
* Clear the cache.
*/
gulp.task('clear', function (done) {
return $.cache.clearAll(done);
});
/**
* 10) Default
*
* When we type `$ gulp` in the command line,
* these default tasks will run.
*/
gulp.task('default', ['clean'], function() {
gulp.start('clean', 'styles', 'styles-ie', 'scripts', 'scripts-head', 'images', 'svg');
});
/**
* 11) Watch
*
* When we tye `$ gulp watch` in the command line,
* these files and folders will trigger Gulp tasks.
*/
gulp.task('watch', function() {
browserSync.init({ server: './' });
gulp.watch('assets/scss/**/*.scss', ['styles', 'styles-ie']);
gulp.watch('assets/js/**/*.js', ['scripts', 'scripts-head']);
gulp.watch('assets/img/**/*', ['images']);
gulp.watch('assets/img/**/*.svg', ['svg']);
gulp.watch(['*.html']).on('change', browserSync.reload);
});