Skip to content

Commit

Permalink
perf: generate source maps only when explicitly set (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Apr 26, 2017
1 parent 760eefa commit b8f5c8f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions test/sourceMapTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "/" },
Expand Down Expand Up @@ -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; }", ""]
]);
});

0 comments on commit b8f5c8f

Please sign in to comment.