Repository navigation
Expand file tree
/
Copy pathgulpfile.js
More file actions
210 lines (181 loc) · 6.18 KB
/
Copy pathgulpfile.js
File metadata and controls
210 lines (181 loc) · 6.18 KB
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
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var cp = require('child_process');
var del = require('del');
var less = require('gulp-less');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var size = require('gulp-size');
var cssnano = require('gulp-cssnano');
var flatten = require('gulp-flatten');
var rev = require('gulp-rev');
var imagemin = require('gulp-imagemin');
var htmlhint = require('gulp-htmlhint');
var w3cjs = require('gulp-w3cjs');
var jshint = require('gulp-jshint');
//Paths to dependencies
var paths = {
js: [
//Note: order matters - the files will be concated in order of appearance
'./js/**/*.js'
],
less: [
//The main less folder (./less) is included by default
'./bower_components/normalize.css/',
'./bower_components/fontawesome/less/',
'./bower_components/animate.css/'
],
fonts: [
'./bower_components/fontawesome/fonts/*'
],
images: './images/*'
};
/*
Compress images - TODO: Watch image folder, inject and only compress changed files.
*/
gulp.task('image-min', function () {
return gulp.src(paths.images)
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
}))
.pipe(gulp.dest('./public/assets'));
});
/*
Compile js and less
*/
gulp.task('js',['clean-js'],function() {
return gulp.src(paths.js,{base:'./'})
.pipe(sourcemaps.init())
.pipe(concat('bundle.js'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./public/js'))
.pipe(gulp.dest('./_site/public/js'))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('js-production',['clean-js'],function() {
return gulp.src(paths.js,{base:'./'})
.pipe(concat({path:'bundle.js',cwd:''}))
.pipe(rev())
.pipe(size({title:'js-size before minification'}))
.pipe(uglify())
.pipe(size({title:'js-size after minification'}))
.pipe(gulp.dest('./public/js'))
.pipe(rev.manifest('js-manifest.json'))
.pipe(gulp.dest('./_data'));
});
gulp.task('fonts',['clean-fonts'],function () {
return gulp.src(paths.fonts,{base:'./'})
.pipe(flatten())
.pipe(gulp.dest('./public/fonts'));
});
gulp.task('less',['clean-css','fonts'],function() {
return gulp.src('./less/main.less')
.on('error',function(err) {
browserSync.notify('<span style="color: grey">Error:</span> Less compilation failed');
console.log('Less compilation failed:');
console.log(err);
})
.pipe(sourcemaps.init())
.pipe(less({paths:paths.less}))
.pipe(autoprefixer())
.pipe(sourcemaps.write())
.pipe(gulp.dest('./_site/public/css'))
.pipe(gulp.dest('./public/css'))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('less-production',['clean-css'],function() {
return gulp.src('./less/main.less')
.pipe(less({paths:paths.less}))
.pipe(autoprefixer())
.pipe(size({title:'css-size before minification'}))
.pipe(cssnano())
.pipe(size({title:'css-size after minification'}))
.pipe(rev())
.pipe(gulp.dest('./public/css'))
.pipe(rev.manifest('css-manifest.json'))
.pipe(gulp.dest('./_data'));
});
gulp.task('clean-js',function (cb) {
del(['./public/js/*'],cb);//Use callback, cb, to ensure its finished
});
gulp.task('clean-fonts',function (cb) {
del(['./public/fonts/*'],cb);//Use callback, cb, to ensure its finished
});
gulp.task('clean-css',function (cb) {
del(['./public/css/*'],cb);//Use callback, cb, to ensure its finished
});
/*
Jekyll build tasks
*/
gulp.task('jekyll',['jekyll-build']);
gulp.task('jekyll-build-production',['js-production','less-production','fonts','image-min'], function (done) {
return cp.spawn('bundle',['exec','jekyll','build'], {stdio: 'inherit'})
.on('close', done);
});
//This task is needed to avoid race condition
gulp.task('jekyll-initial-build',['js','less','fonts','image-min'], function (done) {
return cp.spawn('bundle',['exec','jekyll','build','--config','_config.yml,_config-dev.yml'], {stdio: 'inherit'})
.on('close', done);
});
gulp.task('jekyll-build', function (done) {
browserSync.notify('<span style="color: grey">Running:</span> jekyll build');
return cp.spawn('bundle',['exec','jekyll','build','--config','_config.yml,_config-dev.yml'], {stdio: 'inherit'})
.on('close', done);
});
gulp.task('jekyll-rebuild', ['jekyll-build'], function () {
browserSync.reload();
});
/*
Linter tasks
*/
gulp.task('lint',['html-lint','html-validate','js-lint']);
gulp.task('html-lint',function() {
return gulp.src('./_site/**/*.html')
.pipe(htmlhint())
.pipe(htmlhint.reporter());
});
gulp.task('html-validate', function () {
var through2 = require('through2');
return gulp.src('./_site/**/*.html')
.pipe(w3cjs())
.pipe(through2.obj(function(file, enc, cb){
cb(null, file);
if (!file.w3cjs.success){
throw new Error('HTML validation error(s) found');
}
}));
});
gulp.task('js-lint',function() {
return gulp.src('./js/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
/*
Start dev server with Browser sync
*/
//Wait for initial jekyll-build, then launch the Server
gulp.task('browser-sync', ['jekyll-initial-build'], function() {
browserSync.init({server: {baseDir: '_site'}});
});
gulp.task('browser-sync-production', ['jekyll-build-production'], function() {
browserSync.init({server: {baseDir: '_site'}});
});
//Watch and sync with browser
gulp.task('watch', function () {
gulp.watch('./less/*.less', ['less']);
gulp.watch('./js/*.js', ['js']);
gulp.watch(['index.html','_includes/*.html' ,'_layouts/*.html', '_posts/*','*.md'], ['jekyll-rebuild']);
});
/*
Main gulp entry points
*/
//Launch browser sync with minified assets - no watching
gulp.task('production-test',['browser-sync-production']);
//Run before pushing repo to gh-pages
gulp.task('publish',['jekyll-build-production']);
//Launch browsersync and watch
gulp.task('default', ['browser-sync', 'watch']);