From c55ebd5c2c0327caf960e8ae98d70e2f00f145a0 Mon Sep 17 00:00:00 2001 From: Niels Doucet Date: Mon, 14 Jul 2014 15:21:31 +0200 Subject: [PATCH 1/3] Add option, so the user can choose to call libsass synchronously. This can alleviate some pressure from the cpu and the memory when rendering large or many files. --- README.md | 4 ++++ index.js | 23 ++++++++++++++++++----- test/test.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8a73f6f..3329055 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,10 @@ Pass in your own callback to be called upon successful compilaton by node-sass. Pass in your own callback to be called upon a sass error from node-sass. The callback has the form `callback(err)`, where err is the error string generated by libsass. Note: this *does* prevent an actual `gulpPluginError` object from being created. +#### `sync: true` + +If you pass `sync: true` into the options hash, sass.renderSync will be called, instead of sass.render. This should help when memory and/or cpu usage is getting very high when rendering many and/or big files. + ## Source Maps gulp-sass now generates *inline* source maps if you pass `sourceComments: 'map'` as an option. Note that gulp-sass won't actually do anything when passing `sourceMap: filepath`. Enjoy your source maps! diff --git a/index.js b/index.js index 6bc59cb..7f79ab5 100644 --- a/index.js +++ b/index.js @@ -47,10 +47,7 @@ module.exports = function (options) { "/*# sourceMappingURL=data:application/json;base64," + sourceMap + "*/"); } - - file.path = ext(file.path, '.css'); - file.contents = new Buffer(css); - cb(null, file); + handleOutput(css, file, cb); }; opts.error = function (err) { @@ -67,7 +64,17 @@ module.exports = function (options) { return cb(new gutil.PluginError('gulp-sass', err)); }; - sass.render(opts); + if ( opts.sync ) { + try { + var output = sass.renderSync(opts); + opts.success(output, null); + handleOutput(output, file, cb); + } catch(err) { + opts.error(err); + } + } else { + sass.render(opts); + } if (addedLocalDirPath) opts.includePaths.pop(); @@ -76,6 +83,12 @@ module.exports = function (options) { return map(nodeSass); }; +function handleOutput(output, file, cb) { + file.path = ext(file.path, '.css'); + file.contents = new Buffer(output); + cb(null, file); +} + function getSourcesContent (sources) { sourcesContent = []; diff --git a/test/test.js b/test/test.js index 978c0a5..63c2714 100644 --- a/test/test.js +++ b/test/test.js @@ -58,6 +58,26 @@ test('compile a single sass file', function (t) { stream.write(sassFile); }); +test('compile a single sass file synchronously', function (t) { + var sassFile = createVinyl('mixins.scss'); + + var stream = gsass({sync: true}); + stream.on('data', function (cssFile) { + t.ok(cssFile, 'cssFile should exist'); + t.ok(cssFile.path, 'cssFile.path should exist'); + t.ok(cssFile.relative, 'cssFile.relative should exist'); + t.ok(cssFile.contents, 'cssFile.contents should exist'); + t.equal(cssFile.path, path.join(__dirname, 'scss', 'mixins.css')); + t.equal( + fs.readFileSync(path.join(__dirname, 'ref/mixins.css'), 'utf8'), + cssFile.contents.toString(), + 'file compiles correctly to css' + ); + t.end(); + }) + stream.write(sassFile); +}); + test('compile multiple sass files', function (t) { var files = [ createVinyl('inheritance.scss'), @@ -94,6 +114,19 @@ test('emit error on sass errors', function (t) { stream.write(errorFile); }); +test('emit error on sass errors when using sync true', function (t) { + var stream = gsass({sync: true}); + var errorFile = createVinyl('somefile.sass', + new Buffer('body { font \'Comic Sans\'; }')); + stream.on('error', function (err) { + t.equal(err.message, + 'source string:1: error: property "font" must be followed by a \':\'\n' + ); + t.end(); + }); + stream.write(errorFile); +}); + test('call custom error callback when opts.onError is given', function (t) { var stream = gsass({ onError: function (err) { t.equal(err, From 2b96addeabe8018a01d805daf8521202330119cb Mon Sep 17 00:00:00 2001 From: Niels Doucet Date: Mon, 14 Jul 2014 15:23:52 +0200 Subject: [PATCH 2/3] conform to whitespace usage in forked repo --- index.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 7f79ab5..9567c38 100644 --- a/index.js +++ b/index.js @@ -65,15 +65,15 @@ module.exports = function (options) { }; if ( opts.sync ) { - try { - var output = sass.renderSync(opts); - opts.success(output, null); - handleOutput(output, file, cb); - } catch(err) { - opts.error(err); - } + try { + var output = sass.renderSync(opts); + opts.success(output, null); + handleOutput(output, file, cb); + } catch(err) { + opts.error(err); + } } else { - sass.render(opts); + sass.render(opts); } if (addedLocalDirPath) opts.includePaths.pop(); @@ -84,9 +84,9 @@ module.exports = function (options) { }; function handleOutput(output, file, cb) { - file.path = ext(file.path, '.css'); - file.contents = new Buffer(output); - cb(null, file); + file.path = ext(file.path, '.css'); + file.contents = new Buffer(output); + cb(null, file); } function getSourcesContent (sources) { From 6d36258b5c8471e7d7a980fc47814ff0a1025820 Mon Sep 17 00:00:00 2001 From: Niels Doucet Date: Mon, 14 Jul 2014 15:26:25 +0200 Subject: [PATCH 3/3] conform to whitespace usage in forked repo --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 9567c38..1062ef0 100644 --- a/index.js +++ b/index.js @@ -66,11 +66,11 @@ module.exports = function (options) { if ( opts.sync ) { try { - var output = sass.renderSync(opts); - opts.success(output, null); - handleOutput(output, file, cb); + var output = sass.renderSync(opts); + opts.success(output, null); + handleOutput(output, file, cb); } catch(err) { - opts.error(err); + opts.error(err); } } else { sass.render(opts);