-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
149 lines (134 loc) · 4.57 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
var gulp = require('gulp');
var browserSync = require('browser-sync');
var sass = require('gulp-sass');
var minifyCSS = require('gulp-clean-css');
var minifyHTML = require('gulp-minify-html');
var uglify = require('gulp-uglify');
var uncss = require('gulp-uncss');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var replace = require('gulp-replace');
var notify = require('gulp-notify');
var prefix = require('gulp-autoprefixer');
var cp = require('child_process');
var fs = require('fs');
var ghPages = require('gulp-gh-pages');
var extract = require('gulp-html-extract');
var jekyll = process.platform === 'win32' ? 'jekyll.bat' : 'jekyll';
var messages = {
jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};
/**
* Build the Jekyll Site
*/
gulp.task('jekyll-build', function (done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn( jekyll , ['build', '--config', '_config.yml'], {stdio: 'inherit'})
.on('close', done);
});
/**
* Rebuild Jekyll & do page reload
*/
gulp.task('jekyll-rebuild', ['optimize-html'], function () {
browserSync.reload();
});
/**
* Wait for jekyll-build, then launch the Server
*/
gulp.task('browser-sync', ['optimize-html'], function() {
browserSync({
server: {
baseDir: '_site'
}
});
});
/**
* Compile files from _scss into both _site/css (for live injecting) and site (for future jekyll builds)
*/
gulp.task('optimize-css', ['jekyll-build'], function () {
return gulp.src('_site/css/main.css')
.pipe(prefix(['last 15 versions', '> 1%', 'ie 8', 'ie 7'], { cascade: true }))
.pipe(rename('all.min.css'))
.pipe(gulp.dest('_site/public/css'))
.pipe(browserSync.reload({stream:true}))
});
gulp.task('optimize-js', ['optimize-css'], function() {
return gulp;
});
gulp.task('optimize-html', ['optimize-js'], function() {
return gulp;
});
/**
* Watch html/md files, run jekyll & reload BrowserSync
*/
gulp.task('watch', function () {
gulp.watch(['_sass/*.scss',
'css/*.scss',
'js/*.js',
'*.html',
'_layouts/*.html',
'_includes/*.html',
'_includes/*.svg',
'_posts/*'], ['jekyll-rebuild']);
});
/**
* Default task, running just `gulp` will compile the sass,
* compile the jekyll site, launch BrowserSync & watch files.
*/
gulp.task('default', ['browser-sync', 'watch']);
/**
* Create production-ready website
*/
gulp.task('jekyll-build-prod', function (done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn( jekyll , ['build', '--config', '_config.yml,_config.prod.yml'], {stdio: 'inherit'})
.on('close', done);
});
gulp.task('optimize-css-prod', ['jekyll-build-prod'], function () {
return gulp.src('_site/css/main.css')
.pipe(prefix(['last 15 versions', '> 1%', 'ie 8', 'ie 7'], { cascade: true }))
.pipe(uncss({
html: ['_site/**/*.html'],
ignore: []
}))
.pipe(rename('all.min.css'))
.pipe(minifyCSS())
.pipe(gulp.dest('_site/public/css'))
.pipe(browserSync.reload({stream:true}))
.pipe(notify({ message: 'CSS-PROD task complete' }));
});
gulp.task('optimize-js-prod', ['optimize-css-prod'], function() {
return gulp
.src("_site/index.html")
.pipe(extract())
.pipe(concat('all.min.js'))
.pipe(uglify())
.pipe(gulp.dest('_site/public/js'))
.pipe(notify({ message: 'JS-PROD task complete' }));
});
gulp.task('optimize-html-prod', ['optimize-js-prod'], function() {
return gulp.src('_site/**/*.html')
.pipe(replace(/<link rel=\"stylesheet\" href=\"\/public\/css\/all.min.css\"[^>]*>/, function(s) {
var style = fs.readFileSync('_site/public/css/all.min.css', 'utf8');
return '<style>' + style + '</style>';
}))
.pipe(replace(/<!--startjs-->[^]+<!--endjs-->/, function(s) {
var js_script = fs.readFileSync('_site/public/js/all.min.js', 'utf8');
return '<script type="text/javascript">' + js_script + '</script>';
}))
.pipe(minifyHTML({
quotes: true
}))
.pipe(gulp.dest('_site/'))
.pipe(notify({ message: 'HTML-PROD task complete' }));
});
gulp.task('push-to-gh-pages', ['optimize-html-prod'], function() {
return gulp.src('./_site/**/*')
.pipe(ghPages());
});
gulp.task('deploy',
['jekyll-build-prod',
'optimize-css-prod',
'optimize-js-prod',
'optimize-html-prod',
'push-to-gh-pages']);