Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.

Commit

Permalink
(fix) changed to chain the process() promises
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tholewebgods committed Apr 10, 2019
1 parent afc581c commit 613984f
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions tasks/postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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() {
Expand All @@ -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.');
Expand All @@ -158,24 +167,28 @@ 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).
} else if (src.length > 1 && f.dest) {
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.');
Expand Down

0 comments on commit 613984f

Please sign in to comment.