forked from h5bp/html5boilerplate.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
181 lines (144 loc) · 4.74 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
'use strict';
var browserSync = require('browser-sync');
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var reworkSuit = require('rework-suit');
var runSequence = require('run-sequence'); // Temporary solution until Gulp 4
// https://github.com/gulpjs/gulp/issues/355
var pkg = require('./package.json');
var dirs = pkg['h5bp-configs'].directories;
var reload = browserSync.reload;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
var browserSyncOptions = {
// In-depth information about the options:
// http://www.browsersync.io/docs/options/
logPrefix: 'H5BP',
notify: false,
port: 8080
};
var supportedBrowsers = [
// In-depth information about the options:
// https://github.com/postcss/autoprefixer#browsers
'last 2 versions',
'ie > 8',
'> 1%'
];
// ---------------------------------------------------------------------
// | Helper tasks |
// ---------------------------------------------------------------------
gulp.task('clean:before', function (done) {
require('del')([dirs.dist], done);
});
gulp.task('clean:after', function (done) {
require('del')([
dirs.dist + '/{css,css/**}',
dirs.dist + '/{img,/img/**}',
dirs.dist + '/{js,/js/**}'
], done);
});
gulp.task('copy', [
'copy:html',
'copy:misc'
]);
gulp.task('copy:html', function () {
var assets = plugins.useref.assets();
return gulp.src(dirs.src + '/index.html')
.pipe(assets)
.pipe(assets.restore())
.pipe(plugins.useref())
.pipe(gulp.dest('dist'));
});
gulp.task('copy:misc', function () {
return gulp.src([
// Copy all files
dirs.src + '/**',
// Exclude the following files
// (other tasks will handle the copying of these files)
'!' + dirs.src + '/index.html',
'!' + dirs.src + '/{css,css/**}',
'!' + dirs.src + '/{js,/js/**}'
], {
// Include hidden files by default
dot: true
}).pipe(gulp.dest(dirs.dist));
});
gulp.task('generate:main.css', function () {
return gulp.src(dirs.src + '/css/index.css')
.pipe(plugins.rework(reworkSuit({
browsers: supportedBrowsers
})))
.pipe(plugins.cssBase64())
.pipe(plugins.uncss({
html: [dirs.src + '/index.html']
}))
.pipe(plugins.csso())
.pipe(plugins.rename('main.css'))
.pipe(gulp.dest(dirs.src + '/css/'))
.pipe(reload({ stream: true }));
});
gulp.task('lint:js', function () {
return gulp.src([
'gulpfile.js',
dirs.src + '/js/**/*.js',
]).pipe(plugins.jshint())
.pipe(plugins.jshint.reporter('jshint-stylish'))
.pipe(plugins.if(!browserSync.active, plugins.jshint.reporter('fail')));
});
gulp.task('minify:html', function () {
// In-depth information about the `htmlmin` options:
// https://github.com/kangax/html-minifier#options-quick-reference
var htmlminOptions = {
collapseBooleanAttributes: true,
collapseWhitespace: true,
minifyJS: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true
};
return gulp.src([
dirs.dist + '/index.html'
]).pipe(plugins.smoosher())
.pipe(plugins.htmlmin(htmlminOptions))
.pipe(gulp.dest(dirs.dist));
});
gulp.task('zopfli', function() {
gulp.src(dirs.dist + '/**/*.{css,html,ico,js,svg,txt,xml}')
.pipe(plugins.zopfli())
.pipe(gulp.dest(dirs.dist));
});
// ---------------------------------------------------------------------
// | Main tasks |
// ---------------------------------------------------------------------
gulp.task('build', function (done) {
runSequence(
['clean:before', 'lint:js'],
'generate:main.css',
'copy',
'minify:html',
'clean:after',
'zopfli',
done);
});
gulp.task('default', ['build']);
gulp.task('serve', ['generate:main.css'], function () {
browserSyncOptions.server = dirs.src;
browserSync(browserSyncOptions);
gulp.watch([
dirs.src + '/**/*.html'
], reload);
gulp.watch([
dirs.src + '/css/**/*.css',
dirs.src + '/img/**/*',
'!' + dirs.src + '/css/main.css'
], ['generate:main.css']);
gulp.watch([
dirs.src + '/js/**/*.js',
'gulpfile.js'
], ['lint:js', reload]);
});
gulp.task('serve:build', ['build'], function () {
browserSyncOptions.server = dirs.dist;
browserSync(browserSyncOptions);
});