-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
224 lines (200 loc) · 5.23 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
'use strict';
/**
* Gulp modules
*/
var gulp = require('gulp');
var newer = require('gulp-newer');
var plumber = require('gulp-plumber');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var watch = require('gulp-watch');
var cp = require('child_process');
var argv = require('yargs').argv;
var jekyll = process.platform === 'win32' ? 'jekyll.bat' : 'jekyll';
// Load configurations & set variables
var config = require('./frascoconfig.json');
var tasks = [];
var build = [];
var paths = {};
var jsSrc = [];
/**
* All tasks
*/
Object.keys(config.tasks).forEach(function (key) {
if (config.tasks[key]) {
tasks.push(key);
}
});
/**
* Build tasks
*/
build = tasks.concat();
var index;
['server', 'watch'].forEach(function (value) {
index = build.indexOf(value);
if (index > -1) {
build.splice(index, 1);
}
});
/**
* Paths
*/
Object.keys(config.paths).forEach(function (key) {
if (key != 'assets') {
if (config.paths.assets === '') {
paths[key] = './' + config.paths[key];
} else {
paths[key] = config.paths.assets + '/' + config.paths[key];
}
}
});
for (var i = 0; i <= config.js.src.length - 1; i++) {
jsSrc.push(paths.jsSrc + '/' + config.js.src[i]);
}
/**
* Build the Jekyll Site
*/
gulp.task('jekyll-build', function (done) {
var jekyllConfig = config.jekyll.config.default;
if (argv.production) {
process.env.JEKYLL_ENV = 'production';
jekyllConfig += config.jekyll.config.production ? ',' + config.jekyll.config.production : '';
} else {
jekyllConfig += config.jekyll.config.development ? ',' + config.jekyll.config.development : '';
}
return cp.spawn(jekyll, ['build', '--config', jekyllConfig], {stdio: 'inherit', env: process.env})
.on('close', done);
});
/**
* Rebuild Jekyll & do page reload
*/
gulp.task('jekyll-rebuild', ['jekyll-build'], function () {
browserSync.notify('Rebuilded Jekyll');
browserSync.reload();
});
/**
* Wait for jekyll-build, then launch the Server
*/
gulp.task('server', ['jekyll-build'], function() {
return browserSync.init({
port: config.port,
server: {
baseDir: config.paths.dest,
}
});
});
/**
* Sass
*/
gulp.task('sass', function () {
return gulp.src(paths.sass + '/**/*')
.pipe(sass().on('error', sass.logError))
.pipe(sass({outputStyle: config.sass.outputStyle}))
.pipe(autoprefixer({ browsers: config.autoprefixer.browsers }))
.pipe(gulp.dest(paths.css));
});
/**
* Imagemin
*/
gulp.task('imagemin', function () {
return gulp.src(paths.imagesSrc + '/**/*')
.pipe(plumber())
.pipe(newer(paths.images))
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest(paths.images));
});
/**
* Browserify and Watchify
*/
gulp.task('browserify', function () {
return compile(false);
});
gulp.task('watchify', function () {
return compile(true);
});
function compile(watching) {
var b = browserify(jsSrc);
if (watching) {
b = watchify(b);
}
function bundle() {
return b.bundle()
.pipe(source(config.js.dist))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest(paths.js));
}
b.on('update', function () {
bundle();
});
return bundle();
}
/**
* Watch scss files for changes & recompile
* Watch html/md files, run jekyll & reload BrowserSync
*/
gulp.task('watch', ['watchify'], function () {
if (config.tasks.imagemin) {
watch(paths.imagesSrc + '/**/*', function () {
gulp.start('imagemin');
});
}
if (config.tasks.sass) {
watch(paths.sass + '/**/*', function () {
gulp.start('sass');
});
}
if (config.tasks['server']) {
watch([
'!./node_modules/**/*',
'!./README.md',
'!' + paths.dest + '/**/*',
'_includes/**/*',
'_layouts/**/*',
'*.html',
'./**/*.md',
'./**/*.markdown',
paths.posts + '/**/*',
paths.css + '/**/*',
paths.js + '/**/*',
paths.images + '/**/*'
], function () {
gulp.start('jekyll-rebuild');
});
}
});
/**
* Build for production
*/
gulp.task('build', build, function (done) {
var jekyllConfig = config.jekyll.config.default;
if (argv.production) {
process.env.JEKYLL_ENV = 'production';
jekyllConfig += config.jekyll.config.production ? ',' + config.jekyll.config.production : '';
} else {
jekyllConfig += config.jekyll.config.development ? ',' + config.jekyll.config.development : '';
}
return cp.spawn(jekyll, ['build', '--config', jekyllConfig], {stdio: 'inherit', env: process.env})
.on('close', done);
});
/**
* Default task, running just `gulp` will minify the images, compile the sass, js, and jekyll site,
* launch BrowserSync, and watch files. Tasks can be configured by frascoconfig.json.
*/
gulp.task('default', tasks);
/**
* Test
*/
gulp.task('test', ['build']);