update to node-sass version 2 beta

Peg node-sass dependency at "2.0.0-beta". node-sass version 2 is still
in beta, be wary of this branch.

Handle object output from node-sass, read `css` and `map` properties,
consistent with node-sass v2 API.

Simplify handling of file/data and sourceComments/sourceMap options.
This commit is contained in:
Daphne Maddox 2014-12-24 21:14:44 -08:00
parent f390117421
commit 12f8db9863
3 changed files with 21 additions and 25 deletions

View file

@ -22,16 +22,11 @@ module.exports = function (options) {
}
if (file.sourceMap) {
opts.sourceComments = 'map';
opts.sourceMap = file.path;
}
if (opts.sourceComments === 'map' || opts.sourceComments === 'normal') {
opts.sourceMap = opts.sourceMap || '';
opts.file = file.path;
} else {
opts.data = file.contents.toString();
}
opts.data = file.contents.toString();
opts.file = file.path;
if (opts.includePaths && Array.isArray(opts.includePaths)) {
if (opts.includePaths.indexOf(fileDir) === -1) {
@ -41,26 +36,27 @@ module.exports = function (options) {
opts.includePaths = [fileDir];
}
opts.success = function (css, sourceMap) {
if (typeof opts.onSuccess === 'function') opts.onSuccess(css, sourceMap);
opts.success = function (obj) {
if (typeof opts.onSuccess === 'function') opts.onSuccess(obj);
if (sourceMap) {
if (obj.map && obj.map.length || obj.map.version) {
// hack to remove the already added sourceMappingURL from libsass
css = css.replace(/\/\*#\s*sourceMappingURL\=.*\*\//, '');
obj.css = obj.css.replace(/\/\*#\s*sourceMappingURL\=.*\*\//, '');
// libsass gives us sources' paths relative to file;
// gulp-sourcemaps needs sources' paths relative to file.base;
// so alter the sources' paths to please gulp-sourcemaps.
sourceMap = JSON.parse(sourceMap);
sourceMap.sources = sourceMap.sources.map(function(source) {
obj.map = obj.map.version ? obj.map : JSON.parse(sourceMap);
obj.map.sources = obj.map.sources.map(function(source) {
var abs = path.resolve(path.dirname(file.path), source);
return path.relative(file.base, abs);
});
sourceMap = JSON.stringify(sourceMap);
obj.map = JSON.stringify(obj.map);
applySourceMap(file, sourceMap);
applySourceMap(file, obj.map);
}
handleOutput(css, file, cb);
handleOutput(obj, file, cb);
};
opts.error = function (err) {
@ -80,7 +76,7 @@ module.exports = function (options) {
if ( opts.sync ) {
try {
var output = nodeSass.renderSync(opts);
opts.success(output, null);
opts.success(output);
handleOutput(output, file, cb);
} catch(err) {
opts.error(err);
@ -96,7 +92,7 @@ module.exports = function (options) {
function handleOutput(output, file, cb) {
file.path = ext(file.path, '.css');
file.contents = new Buffer(output);
file.contents = new Buffer(output.css);
cb(null, file);
}

View file

@ -21,11 +21,11 @@
"url": "https://github.com/dlmanning/gulp-sass/issues"
},
"dependencies": {
"node-sass": "^1.0",
"clone": "~0.1.18",
"gulp-util": "^3.0",
"map-stream": "~0.1",
"vinyl-sourcemaps-apply": "~0.1.1",
"clone": "~0.1.18"
"node-sass": "2.0.0-beta",
"vinyl-sourcemaps-apply": "~0.1.1"
},
"devDependencies": {
"tape": "~2.3",

View file

@ -131,7 +131,7 @@ test('emit error on sass errors', function (t) {
new Buffer('body { font \'Comic Sans\'; }'));
stream.on('error', function (err) {
t.equal(err.message,
'stdin:1: property "font" must be followed by a \':\'\n'
'property "font" must be followed by a \':\''
);
t.end();
});
@ -144,7 +144,7 @@ test('emit error on sass errors when using sync true', function (t) {
new Buffer('body { font \'Comic Sans\'; }'));
stream.on('error', function (err) {
t.equal(err.message,
'stdin:1: property "font" must be followed by a \':\'\n'
'property "font" must be followed by a \':\''
);
t.end();
});
@ -153,8 +153,8 @@ test('emit error on sass errors when using sync true', function (t) {
test('call custom error callback when opts.onError is given', function (t) {
var stream = gsass({ onError: function (err) {
t.equal(err,
'stdin:1: property "font" must be followed by a \':\'\n'
t.equal(err.message,
'property "font" must be followed by a \':\''
);
t.end();
}});