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.

release/2.0.1
Niels Doucet 2014-07-14 15:21:31 +02:00
parent 6e8894502b
commit c55ebd5c2c
3 changed files with 55 additions and 5 deletions

View File

@ -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!

View File

@ -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 = [];

View File

@ -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,