From 613984f7f902339f8b0a167a701b3eb7319ef3d0 Mon Sep 17 00:00:00 2001 From: Thomas Lehmann Date: Tue, 9 Apr 2019 16:52:55 +0200 Subject: [PATCH] (fix) changed to chain the process() promises The previous implementation ran process() parallel on one processor instance which messed up variables when plugins auto generated fallback styles for CSS variables for example. --- tasks/postcss.js | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/tasks/postcss.js b/tasks/postcss.js index ce00967..ee6f9cf 100644 --- a/tasks/postcss.js +++ b/tasks/postcss.js @@ -72,6 +72,20 @@ module.exports = function(grunt) { }); } + /** + * @param {string} msg Log message + */ + function log(msg) { + grunt.verbose.writeln(msg); + } + + /** + * Handle the process() promise result + * @param {Object} the tally status object + * @param {String} input the input CSS + * @param {String} dest the destination path + * @param {String} result the output CSS + */ function writeResult(tally, input, dest, result) { var warnings = result.warnings(); @@ -109,13 +123,8 @@ module.exports = function(grunt) { tally.diffs += 1; } - } - /** - * @param {string} msg Log message - */ - function log(msg) { - grunt.verbose.writeln(msg); + return Promise.resolve(); } grunt.registerMultiTask('postcss', 'Process CSS files.', function() { @@ -142,9 +151,9 @@ module.exports = function(grunt) { } var done = this.async(); - var tasks = []; + var taskChain; - this.files.forEach(function(f) { + taskChain = this.files.reduce(function(previousFileset, f) { var src = f.src.filter(function(filepath) { if (!grunt.file.exists(filepath)) { grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found.'); @@ -158,7 +167,8 @@ module.exports = function(grunt) { if (src.length === 0) { grunt.log.error('No source files were found.'); - return done(); + // Complete this file set, don't fail all file sets. + return Promise.resolve(); // This check shall not apply for configs without destination // (in place processsing). @@ -166,16 +176,19 @@ module.exports = function(grunt) { grunt.log.warn("Multiple source files with one destination file configured. All files would write to that *one* destination file.\nThe configured file set is: " + f.src.join(", ") + " => " + f.dest + "."); } - Array.prototype.push.apply(tasks, src.map(function(filepath) { - var dest = f.dest || filepath; - var input = grunt.file.read(filepath); + return previousFileset.then(function() { + return src.reduce(function(previousTask, filepath) { + var dest = f.dest || filepath; + var input = grunt.file.read(filepath); - return process(input, filepath, dest) - .then(writeResult.bind(null, tally, input, dest)); - })); - }); + return previousTask + .then(process.bind(null, input, filepath, dest)) + .then(writeResult.bind(null, tally, input, dest)); + }, Promise.resolve() /* to kick-off the chain */ ); + }); + }, Promise.resolve() /* to kick-off the chain */ ); - Promise.all(tasks).then(function() { + taskChain.then(function() { if (tally.sheets) { if (options.writeDest) { grunt.log.ok(tally.sheets + ' processed ' + grunt.util.pluralize(tally.sheets, 'stylesheet/stylesheets') + ' created.');