From 6c30066e4dd469311d2d2b54f24479d16e9cc555 Mon Sep 17 00:00:00 2001 From: Dan Mindru Date: Sat, 31 Aug 2019 16:53:34 +0200 Subject: [PATCH] Update build task --- tasks/build.js | 183 ++++++++++++++++++++++++++----------------------- 1 file changed, 96 insertions(+), 87 deletions(-) diff --git a/tasks/build.js b/tasks/build.js index 1309f15..adbcb8b 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -1,92 +1,99 @@ 'use strict'; -var gulp = require('gulp'), - inlineCss = require('gulp-inline-css'), - minifyHTML = require('gulp-minify-html'), - minifyInline = require('gulp-minify-inline'), - preprocess = require('gulp-preprocess'), - rename = require('gulp-rename'), - klaw = require('klaw'), - fs = require('fs'), - Q = require('q'), - del = require('del'), - jsonlint = require('jsonlint'), - inlineimg = require('gulp-inline-images-no-http'), - path = require('path'); +const gulp = require('gulp'), + inlineCss = require('gulp-inline-css'), + minifyHTML = require('gulp-minify-html'), + minifyInline = require('gulp-minify-inline'), + preprocess = require('gulp-preprocess'), + rename = require('gulp-rename'), + klaw = require('klaw'), + fs = require('fs'), + Q = require('q'), + del = require('del'), + jsonlint = require('jsonlint'), + inlineimg = require('gulp-inline-images-no-http'), + path = require('path'); -function buildTask(options){ - gulp.task('build', ['dupe', 'less', 'sass', 'postcss', 'lint'], function build() { - /** Makes templates for a given directory & its configurations. - * @function makeTemplates - * @param {String} dir Directory to make templates from. - * @param {Array} confItems A list of configurations objects (usually persons) to make templates from. - */ - function makeTemplates(dir, confItems){ - confItems.forEach(function handleConf(conf){ - var cwd = options.workingDir + '/' + dir; - var stylesheets = []; +function buildTask(options) { + // Requires: 'dupe', 'less', 'sass', 'postcss', 'lint'. + gulp.task( + 'build', + function build(done) { + /** Makes templates for a given directory & its configurations. + * @function makeTemplates + * @param {String} dir Directory to make templates from. + * @param {Array} confItems A list of configurations objects (usually persons) to make templates from. + */ + function makeTemplates(dir, confItems) { + confItems.forEach(function handleConf(conf) { + var cwd = options.workingDir + '/' + dir; + var stylesheets = []; - /** - * Find stylesheets relative to the CWD & generate tags. - * This way we can automagically inject them into . - */ - klaw(cwd) - .on('readable', function walkTemplateDir() { - var stylesheet; + /** + * Find stylesheets relative to the CWD & generate tags. + * This way we can automagically inject them into . + */ + klaw(cwd) + .on('readable', function walkTemplateDir() { + var stylesheet; - while (stylesheet = this.read()) { - var relativePath = __dirname.substring(0, __dirname.lastIndexOf('/')) + '/tmp/' + dir; - stylesheets.push(stylesheet.path.replace(relativePath, '')); - } - }) - .on('end', function finishedTemplateDirWalk() { - conf.stylesheets = stylesheets - .filter(function filterFiles(file) { - /* Read only CSS files. */ - return (file.match(/.*\.css/)) ? true : false; - }) - .reduce(function(prev, current, index, acc) { - var cssPath = path.win32.basename(current) - return prev += ''; - }, ''); + while ((stylesheet = this.read())) { + var relativePath = __dirname.substring(0, __dirname.lastIndexOf('/')) + '/tmp/' + dir; + stylesheets.push(stylesheet.path.replace(relativePath, '')); + } + }) + .on('end', function finishedTemplateDirWalk() { + conf.stylesheets = stylesheets + .filter(function filterFiles(file) { + /* Read only CSS files. */ + return file.match(/.*\.css/) ? true : false; + }) + .reduce(function(prev, current, index, acc) { + var cssPath = path.win32.basename(current); + return (prev += ''); + }, ''); - options - .src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html']) - .pipe(preprocess({ - context: conf - })) - .pipe(inlineimg()) - .pipe(inlineCss({ - applyTableAttributes: true, - applyWidthAttributes: true, - preserveMediaQueries: true, - removeStyleTags: false - })) - .pipe(minifyHTML({quotes: true})) - .pipe(minifyInline()) - .pipe(rename(function rename(path){ - path.dirname = dir; - path.basename += '-' + conf.id; - return path; - })) - .pipe(gulp.dest(options.dist)); - }) - }); - } + options + .src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html']) + .pipe( + preprocess({ + context: conf + }) + ) + .pipe(inlineimg()) + .pipe( + inlineCss({ + applyTableAttributes: true, + applyWidthAttributes: true, + preserveMediaQueries: true, + removeStyleTags: false + }) + ) + .pipe(minifyHTML({ quotes: true })) + .pipe(minifyInline()) + .pipe( + 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). */ - 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. - */ - var files = []; - var promises = []; + /** 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. + */ + var files = []; + var promises = []; - fs - .readdirSync('./' + options.workingDir) - .forEach(function readConfigurations(dir){ + fs.readdirSync('./' + options.workingDir).forEach(function readConfigurations(dir) { /** NB: For 'watch' to properly work, the cache needs to be deleted before each require. */ var confPath = '../tmp/' + dir + '/conf.json'; var current = null; @@ -95,18 +102,20 @@ function buildTask(options){ delete require.cache[require.resolve(confPath)]; current = require(confPath); if (current && current.length) { - confItems = [...current] + confItems = [...current]; } else { - confItems = [current] + confItems = [current]; } promises.push(makeTemplates(dir, confItems)); }); - Q.all(promises); - }) - .catch(function (err) { console.log(err) }) - }); + Q.all(promises); + }) + .then(() => done()) + .catch((err) => console.log(err)); + } + ); } module.exports = buildTask;