From 3cdf1a325dca5f7ac7d9f6150d74d16dee780950 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Tue, 31 Mar 2015 15:55:26 -0400 Subject: [PATCH 1/7] Passing file as data Resolves dlmanning/gulp-sass#158 once I get it working --- index.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fd9cf8a..1e32f00 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ var PLUGIN_NAME = 'gulp-sass'; var gulpSass = function gulpSass(options, sync) { return through.obj(function(file, enc, cb) { var opts, + ip, filePush, errorM, callback, @@ -31,7 +32,20 @@ var gulpSass = function gulpSass(options, sync) { } opts = assign({}, options); - opts.file = file.path; + opts.data = file.contents.toString(); + + if (opts.includePaths) { + if (typeof opts.includePaths === 'string') { + ip = opts.includePaths; + opts.includePaths = []; + opts.includePaths.push(ip); + } + } + else { + opts.includePaths = []; + } + + opts.includePaths.push(path.dirname(file.path)); // Generate Source Maps if plugin source-map present if (file.sourceMap) { @@ -58,9 +72,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; From bea198e5451cd53eb16b5ccef736b108b32378e6 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Tue, 31 Mar 2015 15:56:18 -0400 Subject: [PATCH 2/7] Updated Tests --- test/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/main.js b/test/main.js index ebec4e1..b1435ff 100644 --- a/test/main.js +++ b/test/main.js @@ -131,7 +131,7 @@ describe('gulp-sass -- async compile', function() { // Expected sources are relative to file.base var expectedSources = [ 'includes/_cats.scss', - 'inheritance.scss' + '../../stdin' ]; var stream; From 4c4c3c125efdb3f646ca814541447c022bbd26e8 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Tue, 31 Mar 2015 16:26:29 -0400 Subject: [PATCH 3/7] A little bit of source map massaging --- index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1e32f00..f6e9b96 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,9 @@ var gulpSass = function gulpSass(options, sync) { var opts, ip, filePush, + sassMap, + sassMapFile, + sassFileSrc, errorM, callback, result; @@ -59,7 +62,18 @@ var gulpSass = function gulpSass(options, sync) { filePush = function filePush(sassObj) { // 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; From 5b8d4eb3197fc4bd52b765bb77222b92aada15d3 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Tue, 31 Mar 2015 16:26:47 -0400 Subject: [PATCH 4/7] Nope, shouldn't be , should be file name --- test/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/main.js b/test/main.js index b1435ff..ebec4e1 100644 --- a/test/main.js +++ b/test/main.js @@ -131,7 +131,7 @@ describe('gulp-sass -- async compile', function() { // Expected sources are relative to file.base var expectedSources = [ 'includes/_cats.scss', - '../../stdin' + 'inheritance.scss' ]; var stream; From 0fefd16607cdd77c101377e8468286e11c1b5880 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Wed, 1 Apr 2015 10:43:39 -0400 Subject: [PATCH 5/7] Updated vars and includePaths based on comments --- index.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index f6e9b96..2bcb78e 100644 --- a/index.js +++ b/index.js @@ -15,11 +15,7 @@ var PLUGIN_NAME = 'gulp-sass'; var gulpSass = function gulpSass(options, sync) { return through.obj(function(file, enc, cb) { var opts, - ip, filePush, - sassMap, - sassMapFile, - sassFileSrc, errorM, callback, result; @@ -39,9 +35,7 @@ var gulpSass = function gulpSass(options, sync) { if (opts.includePaths) { if (typeof opts.includePaths === 'string') { - ip = opts.includePaths; - opts.includePaths = []; - opts.includePaths.push(ip); + opts.includePaths = [opts.includePaths]; } } else { @@ -60,6 +54,10 @@ 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) { // Transform map into JSON From ad6e6e4ddd10cb2b1bf86f6b4676a1970a036b6f Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Wed, 1 Apr 2015 11:07:33 -0400 Subject: [PATCH 6/7] Tests for file rename and file contents change --- test/main.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/main.js b/test/main.js index ebec4e1..108e667 100644 --- a/test/main.js +++ b/test/main.js @@ -125,6 +125,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'); From b7ade97b7ab852e6f41f7d62c297d6cfcef11f67 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Wed, 1 Apr 2015 12:32:12 -0400 Subject: [PATCH 7/7] Indented Syntax support --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index 2bcb78e..b692fac 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,12 @@ var gulpSass = function gulpSass(options, sync) { opts = assign({}, options); 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];