-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
205 lines (176 loc) · 4.35 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
var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');
var shell = require('gulp-shell');
var watch = require('gulp-watch');
var gutil = require('gulp-util');
var plumber = require('gulp-plumber');
var connect = require('gulp-connect');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var cssnano = require('gulp-cssnano');
var imagemin = require('gulp-imagemin');
var rev = require('gulp-rev');
var buffer = require('gulp-buffer');
var revCollector = require('gulp-rev-collector');
var htmlmin = require('gulp-htmlmin');
var rename = require('gulp-rename');
var hugoTheme = null;
var sassInput = './stylesheets/**/*.scss';
var sassOutput = './public/css';
var jsInput = [
'./scripts/jquery-2.1.4.js',
'./scripts/bootstrap.js'
];
var jsOutput = './public/js';
var sassOptions = {
errLogToConsole: true,
outputStyle: 'expanded'
};
var hugoInput = [
'./archetypes/**/*.*',
'./content/**/*.*',
'./layouts/**/*.*',
'./static/**/*.*',
'./themes/**/*.*',
'./config.tml'
];
var imageInput = './static/img/**/*';
var imageOutput = './public/img';
var configDevFile = './config.dev.toml';
var configProdFile = './config.prod.toml';
gulp.task('default', ['serve']);
gulp.task('build', ['hugo', 'sass', 'js', 'img', 'html']);
gulp.task('hugo', function() {
return buildHugo();
});
gulp.task('sass', function() {
return buildSass();
});
gulp.task('js', function() {
return buildJs();
});
gulp.task('img', function() {
return buildImages();
});
gulp.task('html', function() {
return buildHtml();
});
gulp.task('config-dev', function() {
return changeConfigFile(configDevFile);
});
gulp.task('config-prod', function() {
return changeConfigFile(configProdFile);
});
gulp.task('watch', function() {
changeConfigFile(configDevFile);
buildHugo();
buildSass();
buildJs();
watch(sassInput, function (vinyl) {
gutil.log(gutil.colors.green(vinyl.relative), 'fired', gutil.colors.green(vinyl.event));
return buildSass().pipe(connect.reload());
});
watch(jsInput, function (vinyl) {
gutil.log(gutil.colors.green(vinyl.relative), 'fired', gutil.colors.green(vinyl.event));
return buildJs().pipe(connect.reload());
});
watch(hugoInput, function (vinyl) {
gutil.log(gutil.colors.green(vinyl.relative), 'fired', gutil.colors.green(vinyl.event));
return buildHugo().pipe(connect.reload());
});
});
gulp.task('serve', function() {
connect.server({
'root': 'public',
'livereload': true,
'port': 6789
});
gulp.start('watch');
});
function buildHugo() {
var hugoCommand = 'hugo --buildDrafts';
if (hugoTheme) {
hugoCommand += ' --theme=' + hugoTheme;
}
return gulp
.src('')
.pipe(plumber({
errorHandler: handleError
}))
.pipe(shell([
hugoCommand
]
));
}
function buildSass() {
return gulp
.src(sassInput)
.pipe(plumber({
errorHandler: handleError
}))
.pipe(sourcemaps.init())
.pipe(sass(sassOptions))
.pipe(sourcemaps.write())
.pipe(autoprefixer())
.pipe(cssnano())
.pipe(buffer())
.pipe(rev())
.pipe(gulp.dest(sassOutput))
.pipe(rev.manifest({
base: sassOutput,
merge: true
}));
}
function buildJs() {
return gulp.src(jsInput)
.pipe(plumber({
errorHandler: handleError
}))
.pipe(concat('site.js'))
.pipe(uglify())
.pipe(buffer())
.pipe(rev())
.pipe(gulp.dest(jsOutput))
.pipe(rev.manifest({
base: jsOutput,
merge: true
}));
}
function buildImages() {
return gulp.src(imageInput)
.pipe(imagemin({
progressive: true,
svgoPlugins: [
{removeViewBox: false},
{cleanupIDs: false}
]
}))
.pipe(gulp.dest(imageOutput));
}
function buildHtml() {
return gulp
.src(['./rev-manifest.json', './public/**/*.html'])
.pipe(revCollector({
replaceReved: true
}))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('public'));
}
function handleError(error) {
gutil.beep();
var message = null;
if (error.hasOwnProperty('formatted')) {
message = error.formatted;
} else {
message = error;
}
gutil.log(gutil.colors.red(message));
this.emit('end');
}
function changeConfigFile(newConfigFile) {
return gulp.src(newConfigFile)
.pipe(rename('config.toml'))
.pipe(gulp.dest('./'));
}