📜 Docs for gulp tasks #12

This commit is contained in:
Dan Mindru 2016-05-12 19:00:59 +02:00
parent 5379298baf
commit d1ccb12079
3 changed files with 66 additions and 42 deletions

View File

@ -8,10 +8,18 @@ var options = {
dist: 'dist' dist: 'dist'
}; };
wrench.readdirSyncRecursive('./tasks').filter(function(file) { /** Load tasks from the '/tasks' directory.
return (/\.(js|coffee)$/i).test(file); * Look for .js & .coffee files.
}).map(function(file) { * Each file should correspond to a task.
require('./tasks/' + file)(options); */
}); wrench
.readdirSyncRecursive('./tasks')
.filter(function readJSFiles(file) {
return (/\.(js|coffee)$/i).test(file);
})
.map(function loadTasks(file) {
require('./tasks/' + file)(options);
});
/** By default templates will be built into '/dist', then gulp will watch for changes in '/src'. */
gulp.task('default', ['build', 'watch']); gulp.task('default', ['build', 'watch']);

View File

@ -12,47 +12,63 @@ var gulp = require('gulp'),
inlineimg = require('gulp-inline-image-html'); inlineimg = require('gulp-inline-image-html');
function buildTask(options){ function buildTask(options){
gulp.task('build', function(cb) { gulp.task('build', function build(cb) {
var promises = [] var promises = [];
del(options.dist).then(function(){ /** Makes templates for a given directory & its configurations.
wrench.readdirSyncRecursive(options.src).filter(function(file) { * @function makeTemplates
return (!file.match('/') && !file.match(/^\.+/g)) ? file : false * @param {String} dir Directory to make templates from.
}).forEach(function(dir){ * @param {Array} confItems A list of configurations objects (usually persons) to make templates from.
var confPath = './../' + options.src + '/' + dir + '/conf.js'; */
delete require.cache[require.resolve(confPath)] function makeTemplates(dir, confItems){
promises.push(makeTemplates(dir, require(confPath))); confItems
}); .forEach(function handleConf(conf){
function makeTemplates(dir, confItems){
confItems.forEach(function(item){
var cwd = options.src + '/' + dir; var cwd = options.src + '/' + dir;
gulp.src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html']) gulp
.pipe(preprocess({ .src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html'])
context: item .pipe(preprocess({
})) context: conf
.pipe(inlineimg(cwd)) }))
.pipe(inlineCss({ .pipe(inlineimg(cwd))
applyTableAttributes: true, .pipe(inlineCss({
applyWidthAttributes: true, applyTableAttributes: true,
preserveMediaQueries: true, applyWidthAttributes: true,
removeStyleTags: false preserveMediaQueries: true,
})) removeStyleTags: false
.pipe(minifyHTML({quotes: true})) }))
.pipe(minifyInline()) .pipe(minifyHTML({quotes: true}))
.pipe(rename(function rename(path){ .pipe(minifyInline())
path.dirname = dir; .pipe(rename(function rename(path){
path.basename += '-' + item.id; path.dirname = dir;
return path; path.basename += '-' + conf.id;
})) return path;
.pipe(gulp.dest(options.dist)); }))
.pipe(gulp.dest(options.dist));
}); });
} }
Q.all(promises).then(function(){ cb(); }); /** Clean up & then read 'src' to generate templates (build entry point). */
del(options.dist).then(function buildStart(){
/** Loop through dirs and load their conf files.
* Promisify all 'makeTemplate' calls and when resolved, make a call to the task `cb` to let gulp know we're done.
*/
wrench
.readdirSyncRecursive(options.src)
.filter(function filterFiles(file) {
/** Read only folders, skip files. */
return (!file.match('/') && !file.match(/^\.+/g)) ? file : false;
})
.forEach(function readConfigurations(dir){
/** NB: For 'watch' to properly work, the cache needs to be deleted before each require. */
var confPath = './../' + options.src + '/' + dir + '/conf.js';
delete require.cache[require.resolve(confPath)];
promises.push(makeTemplates(dir, require(confPath)));
});
Q.all(promises).then(function buildReady(){ cb(); });
}); });
}); });
} }
module.exports = buildTask module.exports = buildTask;

View File

@ -6,6 +6,6 @@ function watchTask(options){
gulp.task('watch', function(){ gulp.task('watch', function(){
gulp.watch([options.src + '/**/*.html', options.src + '/**/*.css', options.src + '/**/conf.js'], ['build']); gulp.watch([options.src + '/**/*.html', options.src + '/**/*.css', options.src + '/**/conf.js'], ['build']);
}); });
}; }
module.exports = watchTask module.exports = watchTask;