From 8498f7092cbd2a8caa8b9284e775a59e3a091d7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Cie=C5=9Blak?= Date: Thu, 17 Oct 2019 18:52:33 +0000 Subject: [PATCH] Fix #2394: sourceMap option should have consistent behaviour render() and renderSync() should return "map" property in the results only if source map has been enabled. --- README.md | 6 ++++-- lib/index.js | 4 +++- test/api.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0e035bed5..c50097b1f 100644 --- a/README.md +++ b/README.md @@ -310,9 +310,11 @@ Used to determine how many digits after the decimal will be allowed. For instanc * Type: `Boolean | String | undefined` * Default: `undefined` -**Special:** Setting the `sourceMap` option requires also setting the `outFile` option +Enables source map generation during `render` and `renderSync`. -Enables the outputting of a source map during `render` and `renderSync`. When `sourceMap === true`, the value of `outFile` is used as the target output location for the source map. When `typeof sourceMap === "string"`, the value of `sourceMap` will be used as the writing location for the file. +When `sourceMap === true`, the value of `outFile` is used as the target output location for the source map with the suffix `.map` appended. If no `outFile` is set, `sourceMap` parameter is ignored. + +When `typeof sourceMap === "string"`, the value of `sourceMap` will be used as the writing location for the file. ### sourceMapContents diff --git a/lib/index.js b/lib/index.js index 1521ec503..3f20708f1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -300,9 +300,11 @@ module.exports.render = function(opts, cb) { var stats = endStats(result.stats); var payload = { css: result.css, - map: result.map, stats: stats }; + if (result.map) { + payload.map = result.map; + } if (cb) { options.context.callback.call(options.context, null, payload); diff --git a/test/api.js b/test/api.js index a26254ad3..5db31137d 100644 --- a/test/api.js +++ b/test/api.js @@ -63,6 +63,26 @@ describe('api', function() { }); }); + it('should not generate source map when not requested', function(done) { + sass.render({ + file: fixture('simple/index.scss'), + sourceMap: false + }, function(error, result) { + assert.strictEqual(result.hasOwnProperty('map'), false, 'result has a map property'); + done(); + }); + }); + + it('should not generate source map without outFile and no explicit path given', function(done) { + sass.render({ + file: fixture('simple/index.scss'), + sourceMap: true + }, function(error, result) { + assert.strictEqual(result.hasOwnProperty('map'), false, 'result has a map property'); + done(); + }); + }); + it('should compile generate map with sourceMapRoot pass-through option', function(done) { sass.render({ file: fixture('simple/index.scss'), @@ -1348,6 +1368,26 @@ describe('api', function() { done(); }); + it('should not generate source map when not requested', function(done) { + var result = sass.renderSync({ + file: fixture('simple/index.scss'), + sourceMap: false + }); + + assert.strictEqual(result.hasOwnProperty('map'), false, 'result has a map property'); + done(); + }); + + it('should not generate source map without outFile and no explicit path given', function(done) { + var result = sass.renderSync({ + file: fixture('simple/index.scss'), + sourceMap: true + }); + + assert.strictEqual(result.hasOwnProperty('map'), false, 'result has a map property'); + done(); + }); + it('should compile generate map with sourceMapRoot pass-through option', function(done) { var result = sass.renderSync({ file: fixture('simple/index.scss'),