-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
executable file
·381 lines (318 loc) · 9.81 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var gulp = require('gulp');
var util = require('gulp-util');
var plumber = require('gulp-plumber');
var less = require('gulp-less');
var include = require("gulp-include");
var path = require('path');
var watch = require('gulp-watch');
var gulpNunjucks = require('gulp-nunjucks');
var nunjucks = require('nunjucks');
var moment = require('moment');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var connect = require('gulp-connect');
var cssnano = require('gulp-cssnano');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var sourcemaps = require('gulp-sourcemaps');
var del = require('del');
var open = require('gulp-open');
var corsAnywhere = require('cors-anywhere');
var LessPluginAutoPrefix = require('less-plugin-autoprefix');
var gulpData = require('gulp-data');
var fm = require('front-matter');
var _ = require('underscore');
var fs = require('fs');
var sitemap = require('gulp-sitemap');
var accounting = require('accounting');
var ngrok = require('ngrok');
var psi = require('psi');
var purify = require('gulp-purifycss');
// Configuration - Change to your needs...
var config = {
source_dir : './source/',
build_dir : './build/',
statics_dir : 'statics/',
templates_dir : 'templates/',
data_dir : 'data/',
styles_dir : 'styles/',
scripts_dir : 'scripts/',
images_dir : 'images/',
connect_port : 8080,
proxy_port : 9090,
proxyAuth : '', // 'user:passw'
autoprefix : ["> 1%","last 4 versions"], // https://github.com/ai/browserslist#queries
}
config.production = !!util.env.production; // do not change!
config.tunnel = !!util.env.tunnel; // do not change!
config.psi = !!util.env.psi; // do not change!
config.purify = !!util.env.purify; // do not change!
// Configure autoprefixing
var autoprefix = new LessPluginAutoPrefix({browsers: config.autoprefix});
// We will use our own nunjucks environment...
var nunjucksEnv = nunjucks.configure(config.source_dir + config.templates_dir, {
noCache : true,
});
// ... because we want to add globals etc.
nunjucksEnv.addGlobal('moment', moment);
nunjucksEnv.addGlobal('accounting', accounting)
// This is where the data for all pages gets indexed
var dataPages = {};
// Handle gulp errors
function handleError (err) {
console.log(err);
this.emit('end');
}
// Require modules without caching them
function requireUncached(module){
delete require.cache[require.resolve(module)]
return require(module)
}
// Are we in production or development?
console.log('Production: ' + !!util.env.production);
// GULP TASKS
// Clean build directory
gulp.task('clean', function () {
return del.sync([
config.build_dir + '**/*',
]);
});
// Statics
gulp.task('statics',function(){
gulp.src([config.source_dir + config.statics_dir + '**/*'])
.pipe(plumber({
handleError: handleError
}))
.pipe(gulp.dest(config.build_dir + config.statics_dir))
.pipe(connect.reload());
});
// Templates
gulp.task('templates',function(){
gulp.src([config.source_dir + config.templates_dir + '[^_]*.html'])
.pipe(plumber({
handleError: handleError
}))
.pipe(gulpData(function(file) {
var data = {};
var fileName = file.relative;
var fileContent = fm(String(file.contents));
//file.contents = new Buffer(fileContent.body);
// delete page-obj from pages-data if template gets deleted/renamed
// todo: check performance
_.each(dataPages, function (value, key, list) {
try {
fs.statSync(config.source_dir + config.templates_dir + key +'.html');
} catch(err) {
delete dataPages[key];
}
});
// cut off .html from filename for pages-data obj
if (fileName.indexOf('.html') >= 0) {
fileName = fileName.slice(0, -5);
}
// adding page-data to pages-data...
dataPages[fileName] = fileContent.attributes;
dataPages[fileName].path = file.relative;
// collecting data...
data.global = requireUncached(config.source_dir + config.data_dir + 'index.js');
data.pages = dataPages;
data.page = fileContent.attributes;
data.page.path = file.relative;
// siteUrl is used by sitemap-generator
siteUrl = data.global.siteUrl;
// global- and pages data added to nunjucks-environment = accessible in macros too
nunjucksEnv.addGlobal('global', data.global);
nunjucksEnv.addGlobal('pages', data.pages)
// write global and pages to json-file for ajax access
// todo: check performance
try {
fs.writeFileSync(config.build_dir + 'data.json', JSON.stringify({
global : data.global,
pages : data.pages
}, null, 2) , 'utf-8');
} catch(err) {
console.log(config.build_dir + 'data.json not writable')
}
// only page-data is directly returned to template
return {
page : data.page
};
}))
.pipe(gulpNunjucks.compile(null, {
env : nunjucksEnv
}))
.pipe(plumber({
handleError: handleError
}))
.pipe(config.production ? htmlmin({
collapseWhitespace: true
}) : util.noop())
.pipe(gulp.dest(config.build_dir))
.pipe(connect.reload());
});
// Sitemap
gulp.task('sitemap', function () {
gulp.src([config.source_dir + config.templates_dir + '[^_]*.html'], {
read: false
})
.pipe(sitemap({
siteUrl: require(config.source_dir + config.data_dir + 'index.js').siteUrl
}))
.pipe(gulp.dest(config.build_dir));
});
// Styles
gulp.task('styles',function(){
gulp.src([config.source_dir + config.styles_dir + '[^_]*.{less,css}'])
.pipe(plumber({
handleError: handleError
}))
.pipe(config.production ? util.noop() : sourcemaps.init())
.pipe(less({
plugins: [autoprefix]
}))
.pipe(config.production ? util.noop() : sourcemaps.write(''))
.pipe(plumber({
handleError: handleError
}))
.pipe(config.purify ? purify([config.source_dir + config.scripts_dir + '[^_]*.js', config.source_dir + config.templates_dir + '[^_]*.html']) : util.noop())
.pipe(config.production ? cssnano() : util.noop())
.pipe(gulp.dest(config.build_dir + config.styles_dir))
.pipe(connect.reload());
});
// Scripts
gulp.task('scripts',function(){
gulp.src([config.source_dir + config.scripts_dir + '[^_]*.js'])
.pipe(plumber({
handleError: handleError
}))
.pipe(config.production ? util.noop() : sourcemaps.init())
.pipe(include())
.pipe(config.production ? util.noop() : sourcemaps.write(''))
.pipe(plumber({
handleError: handleError
}))
.pipe(config.production ? uglify() : util.noop())
.pipe(gulp.dest(config.build_dir + config.scripts_dir))
.pipe(connect.reload());
});
// Images
gulp.task('images',function(){
gulp.src([config.source_dir + config.images_dir + '**/*.{jpg,png,gif,svg}'])
.pipe(plumber({
handleError: handleError
}))
.pipe(config.production ? imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}) : util.noop())
.pipe(gulp.dest(config.build_dir + config.images_dir))
.pipe(connect.reload());
});
// Connect
gulp.task('connect', function() {
var httpProxyOptions = {};
if(config.proxyAuth && config.proxyAuth.length > 0){
httpProxyOptions.auth = config.proxyAuth;
}
corsAnywhere.createServer({
originWhitelist: [],
httpProxyOptions : httpProxyOptions
}).listen(config.proxy_port, 'localhost', function() {
console.log('Running CORS Anywhere on ' + 'localhost' + ':' + config.proxy_port);
});
connect.server({
root: config.build_dir,
livereload: true,
port: config.connect_port
});
});
// Openuri
gulp.task('openuri', function(){
gulp.src(config.build_dir + '*',{
read: false
}).pipe(open({uri: 'http://localhost:' + config.connect_port}));
});
gulp.task('tunnel', function(cb) {
return ngrok.connect({
proto: 'http', // http|tcp|tls
addr: config.connect_port, // port or network address
//auth: 'yafbp:yafbp', // http basic authentication for tunnel
//subdomain: 'alex', // reserved tunnel name https://alex.ngrok.io
//authtoken: '12345', // your authtoken from ngrok.com
//region: 'eu' // one of ngrok regions (us, eu, au, ap), defaults to us
}, function (err, url) {
site = url;
console.log('Exposing localhost:'+ config.connect_port +' via public URL: ' + site);
cb();
if(config.psi){
_.each(dataPages, function (value, key, list) {
console.log('Awaiting psi-report for: ' + url + '/' + key +'.html');
psi.output(url + '/' + key +'.html',{
nokey: 'true', // or use key: ‘YOUR_API_KEY’
strategy: 'desktop',
}).then(() => {
psi.output(url + '/' + key +'.html',{
nokey: 'true', // or use key: ‘YOUR_API_KEY’
strategy: 'mobile',
});
});
});
}
});
});
// Default task
gulp.task(
'default',
['clean','templates','styles','scripts','images','statics','connect','sitemap'],
function(){
// WATCHERS
watch([config.source_dir + config.templates_dir + '**/*.html', config.source_dir + config.data_dir + '*.{js,json}'], {
read: false,
readDelay: 100
}, function() {
gulp.start('templates');
});
watch([config.source_dir + config.templates_dir + '**/*.html'], {
read: false,
readDelay: 100
}, function() {
gulp.start('sitemap');
});
watch(config.source_dir + config.styles_dir + '**/*.{less,css}', {
read: false,
readDelay: 100
}, function() {
gulp.start('styles');
});
watch(config.source_dir + config.scripts_dir + '**/*.js', {
read: false,
readDelay: 100
}, function() {
gulp.start('scripts');
});
watch(config.source_dir + config.images_dir + '**/*.{jpg,png,gif,svg}', {
read: false,
readDelay: 100
}, function() {
gulp.start('images');
});
watch(config.source_dir + config.statics_dir + '**/*', {
read: false,
readDelay: 100
}, function() {
gulp.start('statics');
});
// Workaround for indexing all pages data on first run(only)
setTimeout(function () {
console.log('Running templates again to index initial data.');
gulp.start('templates'); // templates must be triggert twice because not all pages are indexed in dataPages in the first run
setTimeout(function () {
gulp.start('openuri');
if(config.tunnel){
gulp.start('tunnel');
}
}, 1000);
}, 1000);
}
);