Update build task

This commit is contained in:
Dan Mindru 2019-08-31 16:53:34 +02:00
parent 2f56f93d79
commit 6c30066e4d
1 changed files with 96 additions and 87 deletions

View File

@ -1,92 +1,99 @@
'use strict'; 'use strict';
var gulp = require('gulp'), const gulp = require('gulp'),
inlineCss = require('gulp-inline-css'), inlineCss = require('gulp-inline-css'),
minifyHTML = require('gulp-minify-html'), minifyHTML = require('gulp-minify-html'),
minifyInline = require('gulp-minify-inline'), minifyInline = require('gulp-minify-inline'),
preprocess = require('gulp-preprocess'), preprocess = require('gulp-preprocess'),
rename = require('gulp-rename'), rename = require('gulp-rename'),
klaw = require('klaw'), klaw = require('klaw'),
fs = require('fs'), fs = require('fs'),
Q = require('q'), Q = require('q'),
del = require('del'), del = require('del'),
jsonlint = require('jsonlint'), jsonlint = require('jsonlint'),
inlineimg = require('gulp-inline-images-no-http'), inlineimg = require('gulp-inline-images-no-http'),
path = require('path'); path = require('path');
function buildTask(options){ function buildTask(options) {
gulp.task('build', ['dupe', 'less', 'sass', 'postcss', 'lint'], function build() { // Requires: 'dupe', 'less', 'sass', 'postcss', 'lint'.
/** Makes templates for a given directory & its configurations. gulp.task(
* @function makeTemplates 'build',
* @param {String} dir Directory to make templates from. function build(done) {
* @param {Array} confItems A list of configurations objects (usually persons) to make templates from. /** Makes templates for a given directory & its configurations.
*/ * @function makeTemplates
function makeTemplates(dir, confItems){ * @param {String} dir Directory to make templates from.
confItems.forEach(function handleConf(conf){ * @param {Array} confItems A list of configurations objects (usually persons) to make templates from.
var cwd = options.workingDir + '/' + dir; */
var stylesheets = []; function makeTemplates(dir, confItems) {
confItems.forEach(function handleConf(conf) {
var cwd = options.workingDir + '/' + dir;
var stylesheets = [];
/** /**
* Find stylesheets relative to the CWD & generate <link> tags. * Find stylesheets relative to the CWD & generate <link> tags.
* This way we can automagically inject them into <head>. * This way we can automagically inject them into <head>.
*/ */
klaw(cwd) klaw(cwd)
.on('readable', function walkTemplateDir() { .on('readable', function walkTemplateDir() {
var stylesheet; var stylesheet;
while (stylesheet = this.read()) { while ((stylesheet = this.read())) {
var relativePath = __dirname.substring(0, __dirname.lastIndexOf('/')) + '/tmp/' + dir; var relativePath = __dirname.substring(0, __dirname.lastIndexOf('/')) + '/tmp/' + dir;
stylesheets.push(stylesheet.path.replace(relativePath, '')); stylesheets.push(stylesheet.path.replace(relativePath, ''));
} }
}) })
.on('end', function finishedTemplateDirWalk() { .on('end', function finishedTemplateDirWalk() {
conf.stylesheets = stylesheets conf.stylesheets = stylesheets
.filter(function filterFiles(file) { .filter(function filterFiles(file) {
/* Read only CSS files. */ /* Read only CSS files. */
return (file.match(/.*\.css/)) ? true : false; return file.match(/.*\.css/) ? true : false;
}) })
.reduce(function(prev, current, index, acc) { .reduce(function(prev, current, index, acc) {
var cssPath = path.win32.basename(current) var cssPath = path.win32.basename(current);
return prev += '<link rel="stylesheet" href="' + cssPath + '">'; return (prev += '<link rel="stylesheet" href="' + cssPath + '">');
}, ''); }, '');
options options
.src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html']) .src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html'])
.pipe(preprocess({ .pipe(
context: conf preprocess({
})) context: conf
.pipe(inlineimg()) })
.pipe(inlineCss({ )
applyTableAttributes: true, .pipe(inlineimg())
applyWidthAttributes: true, .pipe(
preserveMediaQueries: true, inlineCss({
removeStyleTags: false applyTableAttributes: true,
})) applyWidthAttributes: true,
.pipe(minifyHTML({quotes: true})) preserveMediaQueries: true,
.pipe(minifyInline()) removeStyleTags: false
.pipe(rename(function rename(path){ })
path.dirname = dir; )
path.basename += '-' + conf.id; .pipe(minifyHTML({ quotes: true }))
return path; .pipe(minifyInline())
})) .pipe(
.pipe(gulp.dest(options.dist)); rename(function rename(path) {
}) path.dirname = dir;
}); path.basename += '-' + conf.id;
} return path;
})
)
.pipe(gulp.dest(options.dist));
});
});
}
/** Clean up & then read 'src' to generate templates (build entry point). */ /** Clean up & then read 'src' to generate templates (build entry point). */
del(options.dist) del(options.dist)
.then(function buildStart(){ .then(function buildStart() {
/** /**
* Loop through dirs and load their conf files. * 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. * Promisify all 'makeTemplate' calls and when resolved, make a call to the task (`cb`) to let gulp know we're done.
*/ */
var files = []; var files = [];
var promises = []; var promises = [];
fs fs.readdirSync('./' + options.workingDir).forEach(function readConfigurations(dir) {
.readdirSync('./' + options.workingDir)
.forEach(function readConfigurations(dir){
/** NB: For 'watch' to properly work, the cache needs to be deleted before each require. */ /** NB: For 'watch' to properly work, the cache needs to be deleted before each require. */
var confPath = '../tmp/' + dir + '/conf.json'; var confPath = '../tmp/' + dir + '/conf.json';
var current = null; var current = null;
@ -95,18 +102,20 @@ function buildTask(options){
delete require.cache[require.resolve(confPath)]; delete require.cache[require.resolve(confPath)];
current = require(confPath); current = require(confPath);
if (current && current.length) { if (current && current.length) {
confItems = [...current] confItems = [...current];
} else { } else {
confItems = [current] confItems = [current];
} }
promises.push(makeTemplates(dir, confItems)); promises.push(makeTemplates(dir, confItems));
}); });
Q.all(promises); Q.all(promises);
}) })
.catch(function (err) { console.log(err) }) .then(() => done())
}); .catch((err) => console.log(err));
}
);
} }
module.exports = buildTask; module.exports = buildTask;