Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Lodder committed Dec 5, 2018
2 parents dbf643c + 253bb5e commit 23ffa22
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ module.exports = function(grunt) {
},
src: 'test/fixtures/a.scss',
dest: 'tmp/noWriteDest.scss'
},
sequential: {
options: {
syntax: require('postcss-scss'),
sequential: true
},
src: ['test/fixtures/a.scss', 'test/fixtures/a.css'],
dest: 'tmp/sequential.css'
}
},

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ options: {
```
You can also specify a path where you want the file to be saved.

#### options.sequential
Type: `Boolean`
Default value: `false`

By default grunt-postcss will load all passed CSS files and immediately process them. Set this to `true` if you want files to be processed one by one.
This can help in case when you have a lot of CSS files and processing them causes an `out of memory` error.

#### options.failOnError
Type: `Boolean`
Default value: `false`
Expand Down
63 changes: 58 additions & 5 deletions tasks/postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var maxmin = require('maxmin');
module.exports = function(grunt) {
var options;
var processor;
var tasks;

/**
* Returns an input map contents if a custom map path was specified
Expand Down Expand Up @@ -55,7 +56,7 @@ module.exports = function(grunt) {
* @param {string} input Input CSS contents
* @param {string} from Input CSS path
* @param {string} to Output CSS path
* @returns {{css: string, map: ?string}}
* @returns {LazyResult}
*/
function process(input, from, to) {
return processor.process(input, {
Expand All @@ -73,6 +74,57 @@ module.exports = function(grunt) {
});
}

/**
* Runs tasks sequentially
* @returns {Promise}
*/
function runSequence() {
if (!tasks.length) {
return Promise.resolve();
}

var currentTask = tasks.shift();

return process(currentTask.input, currentTask.from, currentTask.to).then(function(result) {
currentTask.cb(result);
currentTask = null;
return runSequence();
});
}

/**
* Creates a task to be processed
* @param {string} input
* @param {string} from
* @param {string} to
* @param {Function} cb
* @returns {Promise|Object}
*/
function createTask(input, from, to, cb) {
var newTask;

if (options.sequential) {
newTask = {
input: input,
from: from,
to: to,
cb: cb
};
} else {
newTask = process(input, from, to).then(cb);
}

return newTask;
}

/**
* Runs prepared tasks
* @returns {Promise}
*/
function runTasks() {
return options.sequential ? runSequence() : Promise.all(tasks);
}

/**
* @param {string} msg Log message
*/
Expand All @@ -87,8 +139,10 @@ module.exports = function(grunt) {
diff: false,
safe: false,
failOnError: false,
writeDest: true
writeDest: true,
sequential: false
});
tasks = [];

var tally = {
sheets: 0,
Expand All @@ -107,7 +161,6 @@ module.exports = function(grunt) {
}

var done = this.async();
var tasks = [];

this.files.forEach(function(f) {
var src = f.src.filter(function(filepath) {
Expand All @@ -130,7 +183,7 @@ module.exports = function(grunt) {
var dest = f.dest || filepath;
var input = grunt.file.read(filepath);

return process(input, filepath, dest).then(function(result) {
return createTask(input, filepath, dest, function(result) {
var warnings = result.warnings();

tally.issues += warnings.length;
Expand Down Expand Up @@ -172,7 +225,7 @@ module.exports = function(grunt) {
}));
});

Promise.all(tasks).then(function() {
runTasks().then(function() {
if (tally.sheets) {
if (options.writeDest) {
var size = chalk.dim(maxmin(tally.sizeBefore, tally.sizeAfter));
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,12 @@ exports.gruntPostcss = {
test.ok(!grunt.file.exists('tmp/noWriteDest.scss'));
test.done();
},

sequential: function(test) {
test.ok(grunt.file.exists('tmp/sequential.css'));
var actual = grunt.file.read('tmp/sequential.css');
var expected = grunt.file.read('test/fixtures/a.css');
test.strictEqual(actual, expected);
test.done();
},
};

0 comments on commit 23ffa22

Please sign in to comment.