Merge pull request #228 from Snugug/2.x-datastream

2.x datastream
2.x
David Manning 2015-04-01 16:59:50 -07:00
commit 9b69aaa2dc
2 changed files with 82 additions and 3 deletions

View File

@ -31,7 +31,24 @@ var gulpSass = function gulpSass(options, sync) {
}
opts = assign({}, options);
opts.file = file.path;
opts.data = file.contents.toString();
// Ensure `indentedSyntax` is true if a `.sass` file
if (path.extname(file.path) === '.sass') {
opts.indentedSyntax = true;
}
// Ensure file's parent directory in the include path
if (opts.includePaths) {
if (typeof opts.includePaths === 'string') {
opts.includePaths = [opts.includePaths];
}
}
else {
opts.includePaths = [];
}
opts.includePaths.push(path.dirname(file.path));
// Generate Source Maps if plugin source-map present
if (file.sourceMap) {
@ -43,9 +60,24 @@ var gulpSass = function gulpSass(options, sync) {
// Handles returning the file to the stream
//////////////////////////////
filePush = function filePush(sassObj) {
var sassMap,
sassMapFile,
sassFileSrc;
// Build Source Maps!
if (sassObj.map) {
applySourceMap(file, JSON.parse(sassObj.map.toString()));
// Transform map into JSON
sassMap = JSON.parse(sassObj.map.toString());
// Grab the stdout and transform it into stdin
sassMapFile = sassMap.file.replace('stdout', 'stdin');
// Grab the base file name that's being worked on
sassFileSrc = file.path.split('/').pop();
// Replace the stdin with the original file name
sassMap.sources[sassMap.sources.indexOf(sassMapFile)] = sassFileSrc;
// Replace the map file with the original file name
sassMap.file = sassFileSrc;
// Apply the map
applySourceMap(file, sassMap);
}
file.contents = sassObj.css;
@ -58,9 +90,13 @@ var gulpSass = function gulpSass(options, sync) {
// Handles error message
//////////////////////////////
errorM = function errorM(error) {
var relativePath = path.relative(process.cwd(), error.file),
var relativePath = '',
filePath = error.file === 'stdin' ? file.path : error.file,
message = '';
filePath = filePath ? filePath : file.path;
relativePath = path.relative(process.cwd(), filePath);
message += gutil.colors.underline(relativePath) + '\n';
message += gutil.colors.gray(' ' + error.line + ':' + error.column) + ' ';
message += error.message;

View File

@ -124,6 +124,49 @@ describe('gulp-sass -- async compile', function() {
stream.write(errorFile);
});
it('should compile a single sass file if the file name has been changed in the stream', function(done) {
var sassFile = createVinyl('mixins.scss');
var stream;
// Transform file name
sassFile.path = path.join(path.join(__dirname, 'scss'), 'mixin--changed.scss');
stream = sass();
stream.on('data', function(cssFile) {
should.exist(cssFile);
should.exist(cssFile.path);
cssFile.path.split('/').pop().should.equal('mixin--changed.css');
should.exist(cssFile.relative);
should.exist(cssFile.contents);
String(cssFile.contents).should.equal(
fs.readFileSync(path.join(__dirname, 'expected/mixins.css'), 'utf8')
);
done();
});
stream.write(sassFile);
});
it('should preserve changes made in-stream to a Sass file', function(done) {
var sassFile = createVinyl('mixins.scss');
var stream;
// Transform file name
sassFile.contents = new Buffer('/* Added Dynamically */' + sassFile.contents.toString());
stream = sass();
stream.on('data', function(cssFile) {
should.exist(cssFile);
should.exist(cssFile.path);
should.exist(cssFile.relative);
should.exist(cssFile.contents);
String(cssFile.contents).should.equal('/* Added Dynamically */\n' +
fs.readFileSync(path.join(__dirname, 'expected/mixins.css'), 'utf8')
);
done();
});
stream.write(sassFile);
});
it('should work with gulp-sourcemaps', function(done) {
var sassFile = createVinyl('inheritance.scss');