From e545d67f2a86b04e28e20624dc3f74e8b9bc0657 Mon Sep 17 00:00:00 2001 From: Vitaliy Ribachenko Date: Wed, 4 Jan 2017 15:17:15 +0200 Subject: [PATCH 1/2] Add sequential options which allows process files one after another --- README.md | 7 ++++++ tasks/postcss.js | 63 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ae6c6e4..6f07db7 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/tasks/postcss.js b/tasks/postcss.js index 09101f1..2ea5609 100644 --- a/tasks/postcss.js +++ b/tasks/postcss.js @@ -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 @@ -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, { @@ -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 */ @@ -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, @@ -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) { @@ -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; @@ -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)); From c2cbfe9e0fad8789339e46d807c11a927c1be47d Mon Sep 17 00:00:00 2001 From: Vitaliy Ribachenko Date: Wed, 5 Dec 2018 08:08:58 +0200 Subject: [PATCH 2/2] Add tests for sequential option --- Gruntfile.js | 8 ++++++++ test/test.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/Gruntfile.js b/Gruntfile.js index 5850b18..dfefa1e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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' } }, diff --git a/test/test.js b/test/test.js index 32fcbb1..b64ac10 100644 --- a/test/test.js +++ b/test/test.js @@ -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(); + }, };