-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
300 lines (270 loc) · 9.37 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
'use strict';
var fs = require('fs');
var cp = require('child_process');
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var del = require('del');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var watchify = require('watchify');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');
var exit = require('gulp-exit');
var rev = require('gulp-rev');
var revReplace = require('gulp-rev-replace');
var SassString = require('node-sass').types.String;
var notifier = require('node-notifier');
var metalsmithTask = require('./metalsmith');
// /////////////////////////////////////////////////////////////////////////////
// --------------------------- Variables -------------------------------------//
// ---------------------------------------------------------------------------//
// The package.json
var pkg;
// Environment
// Set the correct environment, which controls what happens in config.js
if (!process.env.DS_ENV) {
if (!process.env.TRAVIS_BRANCH || process.env.TRAVIS_BRANCH !== process.env.DEPLOY_BRANCH) {
process.env.DS_ENV = 'staging';
} else {
process.env.DS_ENV = 'production';
}
}
var prodBuild = false;
// /////////////////////////////////////////////////////////////////////////////
// ------------------------- Helper functions --------------------------------//
// ---------------------------------------------------------------------------//
function readPackage () {
pkg = JSON.parse(fs.readFileSync('package.json'));
}
readPackage();
// /////////////////////////////////////////////////////////////////////////////
// ------------------------- Callable tasks ----------------------------------//
// ---------------------------------------------------------------------------//
gulp.task('default', ['clean'], function () {
prodBuild = true;
gulp.start('build');
});
gulp.task('serve', ['vendorScripts', 'javascript', 'styles', 'fonts', 'metalsmith'], function () {
browserSync({
port: 3000,
ghostMode: false,
server: {
baseDir: ['.tmp', 'build', 'app'],
routes: {
'/node_modules': './node_modules'
}
}
});
// watch for changes
gulp.watch([
'app/**/*.html',
'app/**/*.ejs',
'app/**/*.md'
]).on('change', function () {
gulp.start('metalsmith', function () { reload(); });
});
gulp.watch('app/assets/styles/**/*.scss', ['styles']);
gulp.watch('app/assets/fonts/**/*', ['fonts']);
gulp.watch('package.json', ['vendorScripts']);
gulp.watch('app/assets/graphics/collecticons/**', ['collecticons']);
});
gulp.task('clean', function () {
return del(['.tmp', 'build', 'assets/content'])
.then(function () {
$.cache.clearAll();
});
});
// /////////////////////////////////////////////////////////////////////////////
// ------------------------- Browserify tasks --------------------------------//
// ------------------- (Not to be called directly) ---------------------------//
// ---------------------------------------------------------------------------//
// Compiles the user's script files to bundle.js.
// When including the file in the index.html we need to refer to bundle.js not
// main.js
gulp.task('javascript', function () {
var watcher = watchify(browserify({
entries: ['./app/assets/scripts/main.js'],
debug: true,
cache: {},
packageCache: {},
fullPaths: true
}), {poll: true});
function bundler () {
if (pkg.dependencies) {
watcher.external(Object.keys(pkg.dependencies));
}
return watcher.bundle()
.on('error', function (e) {
notifier.notify({
title: 'Oops! Browserify errored:',
message: e.message
});
console.log('Javascript error:', e);
if (prodBuild) {
process.exit(1);
}
// Allows the watch to continue.
this.emit('end');
})
.pipe(source('bundle.js'))
.pipe(buffer())
// Source maps.
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('.tmp/assets/scripts'))
.pipe(reload({stream: true}));
}
watcher
.on('log', gutil.log)
.on('update', bundler);
return bundler();
});
// Vendor scripts. Basically all the dependencies in the package.js.
// Therefore be careful and keep the dependencies clean.
gulp.task('vendorScripts', function () {
// Ensure package is updated.
readPackage();
var vb = browserify({
debug: true,
require: pkg.dependencies ? Object.keys(pkg.dependencies) : []
});
return vb.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('vendor.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('.tmp/assets/scripts/'))
.pipe(reload({stream: true}));
});
// /////////////////////////////////////////////////////////////////////////////
// ------------------------- Collecticon tasks -------------------------------//
// --------------------- (Font generation related) ---------------------------//
// ---------------------------------------------------------------------------//
gulp.task('collecticons', function (done) {
var args = [
'node_modules/collecticons-processor/bin/collecticons.js',
'compile',
'app/assets/graphics/collecticons/',
'--font-embed',
'--font-dest', 'app/assets/fonts',
'--font-name', 'Collecticons',
'--font-types', 'woff',
'--style-format', 'sass',
'--style-dest', 'app/assets/styles/core/',
'--style-name', 'collecticons',
'--class-name', 'collecticon',
'--author-name', 'Development Seed',
'--author-url', 'https://developmentseed.org/',
'--no-preview'
];
return cp.spawn('node', args, {stdio: 'inherit'})
.on('close', done);
});
// /////////////////////////////////////////////////////////////////////////////
// ------------------------ Metalsmith tasks ---------------------------------//
// ---------------------------------------------------------------------------//
gulp.task('metalsmith', function (done) {
metalsmithTask({env: process.env.DS_ENV}).build(function (err, files) {
if (err) {
notifier.notify({
title: 'Oops! Browserify errored:',
message: err.message
});
console.log('Metalsmith error:', err);
if (prodBuild) {
process.exit(1);
}
}
done();
});
});
// //////////////////////////////////////////////////////////////////////////////
// --------------------------- Helper tasks -----------------------------------//
// ----------------------------------------------------------------------------//
gulp.task('build', ['vendorScripts', 'javascript', 'collecticons', 'metalsmith'], function () {
gulp.start(['html', 'images', 'fonts', 'extras'], function () {
return gulp.src('build/**/*')
.pipe($.size({title: 'build', gzip: true}))
.pipe(exit());
});
});
gulp.task('styles', function () {
return gulp.src('app/assets/styles/main.scss')
.pipe($.plumber(function (e) {
notifier.notify({
title: 'Oops! Sass errored:',
message: e.message
});
console.log('Sass error:', e.toString());
if (prodBuild) {
process.exit(1);
}
// Allows the watch to continue.
this.emit('end');
}))
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'expanded',
precision: 10,
functions: {
'urlencode($url)': function (url) {
var v = new SassString();
v.setValue(encodeURIComponent(url.getValue()));
return v;
}
},
includePaths: require('node-bourbon').with('node_modules/jeet')
}))
.pipe($.sourcemaps.write())
.pipe(gulp.dest('.tmp/assets/styles'))
.pipe(reload({stream: true}));
});
// After being rendered, process the html files. (merge css files, etc)
gulp.task('html', ['styles'], function () {
var conf = require('./_config');
return gulp.src('build/**/*.html')
.pipe($.useref({searchPath: ['.tmp', 'app', 'build']}))
// Do not compress comparisons, to avoid MapboxGLJS minification issue
// https://github.com/mapbox/mapbox-gl-js/issues/4359#issuecomment-286277540
.pipe($.if('*.js', $.uglify({compress: {comparisons: false}})))
.pipe($.if('*.css', $.csso()))
.pipe($.if(/\.(css|js)$/, rev()))
.pipe(revReplace({prefix: (conf.baseurl || '') + '/'}))
.pipe(gulp.dest('build'));
});
gulp.task('images', function () {
return gulp.src(['app/assets/graphics/**/*'])
.pipe($.cache($.imagemin([
$.imagemin.gifsicle({interlaced: true}),
$.imagemin.jpegtran({progressive: true}),
$.imagemin.optipng({optimizationLevel: 5}),
// don't remove IDs from SVGs, they are often used
// as hooks for embedding and styling
$.imagemin.svgo({plugins: [{cleanupIDs: false}]})
])))
.pipe(gulp.dest('build/assets/graphics'));
});
gulp.task('fonts', function () {
return gulp.src('app/assets/fonts/**/*')
.pipe(gulp.dest('.tmp/assets/fonts'))
.pipe(gulp.dest('build/assets/fonts'));
});
gulp.task('extras', function () {
return gulp.src([
'app/**/*',
'!app/*.html',
'!app/assets/graphics/**',
'!app/assets/vendor/**',
'!app/assets/styles/**',
'!app/assets/scripts/**',
'!app/posts/**',
'!app/layouts/**',
'!app/includes/**'
], {
dot: true
}).pipe(gulp.dest('build'));
});