From b8f5c8fc3fa146df01a9dca65c475c33cc679bd5 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 26 Apr 2017 22:27:59 +0300 Subject: [PATCH] perf: generate source maps only when explicitly set (#478) --- lib/loader.js | 13 ++++++++++--- lib/processCss.js | 4 ++-- test/sourceMapTest.js | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/loader.js b/lib/loader.js index b55b35f9..ea4d8372 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -16,10 +16,16 @@ module.exports = function(content, map) { var root = query.root; var moduleMode = query.modules || query.module; var camelCaseKeys = query.camelCase || query.camelcase; + var sourceMap = query.sourceMap || false; var resolve = createResolver(query.alias); - if(map !== null && typeof map !== "string") { - map = JSON.stringify(map); + if(sourceMap) { + if (map && typeof map !== "string") { + map = JSON.stringify(map); + } + } else { + // Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it + map = null; } processCss(content, map, { @@ -28,7 +34,8 @@ module.exports = function(content, map) { to: loaderUtils.getCurrentRequest(this), query: query, minimize: this.minimize, - loaderContext: this + loaderContext: this, + sourceMap: sourceMap }, function(err, result) { if(err) return callback(err); diff --git a/lib/processCss.js b/lib/processCss.js index 2c2fea6d..34017d5e 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -200,12 +200,12 @@ module.exports = function processCss(inputSource, inputMap, options, callback) { // we need a prefix to avoid path rewriting of PostCSS from: "/css-loader!" + options.from, to: options.to, - map: { + map: options.sourceMap ? { prev: inputMap, sourcesContent: true, inline: false, annotation: false - } + } : null }).then(function(result) { callback(null, { source: result.css, diff --git a/test/sourceMapTest.js b/test/sourceMapTest.js index 23ac5dc8..3bb84f66 100644 --- a/test/sourceMapTest.js +++ b/test/sourceMapTest.js @@ -10,6 +10,24 @@ describe("source maps", function() { testWithMap("falsy: undefined map doesn't cause an error", ".class { a: b c d; }", undefined, [ [1, ".class { a: b c d; }", ""] ]); + testWithMap("should don't generate sourceMap when `sourceMap: false` and map exist", + ".class { a: b c d; }", + { + file: 'test.css', + mappings: 'AAAA,SAAS,SAAS,EAAE', + names: [], + sourceRoot: '', + sources: [ '/folder/test.css' ], + sourcesContent: [ '.class { a: b c d; }' ], + version: 3 + }, + [ + [1, ".class { a: b c d; }", ""] + ], + { + query: "?sourceMap=false" + } + ); testMap("generate sourceMap (1 loader)", ".class { a: b c d; }", undefined, { loaders: [{request: "/path/css-loader"}], options: { context: "/" }, @@ -95,4 +113,13 @@ describe("source maps", function() { version: 3 }] ]); + testMap("don't generate sourceMap (1 loader)", ".class { a: b c d; }", undefined, { + loaders: [{request: "/path/css-loader"}], + options: { context: "/" }, + resource: "/folder/test.css", + request: "/path/css-loader!/folder/test.css", + query: "?sourceMap=false" + }, [ + [1, ".class { a: b c d; }", ""] + ]); });