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 ef9afc6
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 11 deletions.
25 changes: 25 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ module.exports = function(grunt) {
},
src: 'test/fixtures/a.scss',
dest: 'tmp/noWriteDest.scss'
},
ensureSequentialRun1: {
options: {
processors: [require("postcss-preset-env")]
},
files: [{
src: 'test/fixtures/b1.css',
dest: 'tmp/ensureSequentialRun1sequence1.css'
}, {
src: 'test/fixtures/b2.css',
dest: 'tmp/ensureSequentialRun2sequence1.css'
}]
},
// Same issue, different order. The result shall be the same
ensureSequentialRun2: {
options: {
processors: [require("postcss-preset-env")]
},
files: [{
src: 'test/fixtures/b2.css',
dest: 'tmp/ensureSequentialRun2sequence2.css'
}, {
src: 'test/fixtures/b1.css',
dest: 'tmp/ensureSequentialRun1sequence2.css'
}]
}
},

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-nodeunit": "^1.0.0",
"load-grunt-tasks": "^3.1.0",
"postcss-preset-env": "^6.6.0",
"postcss-scss": "^1.0.2",
"time-grunt": "^1.1.0"
},
Expand Down
28 changes: 17 additions & 11 deletions tasks/postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ module.exports = function(grunt) {

tally.diffs += 1;
}

return Promise.resolve();
}

/**
Expand Down Expand Up @@ -142,9 +144,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 +160,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
9 changes: 9 additions & 0 deletions test/expected/ensureSequentialRun1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

:root {
--foo: #333;
}

.moo {
color: #333;
color: var(--foo);
}
4 changes: 4 additions & 0 deletions test/expected/ensureSequentialRun2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

:root {
--foo: #666;
}
8 changes: 8 additions & 0 deletions test/fixtures/b1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

:root {
--foo: #333;
}

.moo {
color: var(--foo);
}
4 changes: 4 additions & 0 deletions test/fixtures/b2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

:root {
--foo: #666;
}
33 changes: 33 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,37 @@ exports.gruntPostcss = {
test.ok(!grunt.file.exists('tmp/noWriteDest.scss'));
test.done();
},

ensureSequentialRun1: function(test) {
var actual = {
css1: grunt.file.read('tmp/ensureSequentialRun1sequence1.css'),
css2: grunt.file.read('tmp/ensureSequentialRun2sequence1.css')
};

var expected = {
css1: grunt.file.read('test/expected/ensureSequentialRun1.css'),
css2: grunt.file.read('test/expected/ensureSequentialRun2.css'),
};

test.strictEqual(actual.css1, expected.css1);
test.strictEqual(actual.css2, expected.css2);
test.done();
},

// Identical validation
ensureSequentialRun2: function(test) {
var actual = {
css1: grunt.file.read('tmp/ensureSequentialRun1sequence2.css'),
css2: grunt.file.read('tmp/ensureSequentialRun2sequence2.css')
};

var expected = {
css1: grunt.file.read('test/expected/ensureSequentialRun1.css'),
css2: grunt.file.read('test/expected/ensureSequentialRun2.css'),
};

test.strictEqual(actual.css1, expected.css1);
test.strictEqual(actual.css2, expected.css2);
test.done();
},
};

0 comments on commit ef9afc6

Please sign in to comment.