Skip to content

Commit b8f5c8f

Browse files
evilebottnawimichael-ciniawsky
authored andcommittedApr 26, 2017
perf: generate source maps only when explicitly set (#478)
1 parent 760eefa commit b8f5c8f

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed
 

‎lib/loader.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ module.exports = function(content, map) {
1616
var root = query.root;
1717
var moduleMode = query.modules || query.module;
1818
var camelCaseKeys = query.camelCase || query.camelcase;
19+
var sourceMap = query.sourceMap || false;
1920
var resolve = createResolver(query.alias);
2021

21-
if(map !== null && typeof map !== "string") {
22-
map = JSON.stringify(map);
22+
if(sourceMap) {
23+
if (map && typeof map !== "string") {
24+
map = JSON.stringify(map);
25+
}
26+
} else {
27+
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
28+
map = null;
2329
}
2430

2531
processCss(content, map, {
@@ -28,7 +34,8 @@ module.exports = function(content, map) {
2834
to: loaderUtils.getCurrentRequest(this),
2935
query: query,
3036
minimize: this.minimize,
31-
loaderContext: this
37+
loaderContext: this,
38+
sourceMap: sourceMap
3239
}, function(err, result) {
3340
if(err) return callback(err);
3441

‎lib/processCss.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
200200
// we need a prefix to avoid path rewriting of PostCSS
201201
from: "/css-loader!" + options.from,
202202
to: options.to,
203-
map: {
203+
map: options.sourceMap ? {
204204
prev: inputMap,
205205
sourcesContent: true,
206206
inline: false,
207207
annotation: false
208-
}
208+
} : null
209209
}).then(function(result) {
210210
callback(null, {
211211
source: result.css,

‎test/sourceMapTest.js

+27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ describe("source maps", function() {
1010
testWithMap("falsy: undefined map doesn't cause an error", ".class { a: b c d; }", undefined, [
1111
[1, ".class { a: b c d; }", ""]
1212
]);
13+
testWithMap("should don't generate sourceMap when `sourceMap: false` and map exist",
14+
".class { a: b c d; }",
15+
{
16+
file: 'test.css',
17+
mappings: 'AAAA,SAAS,SAAS,EAAE',
18+
names: [],
19+
sourceRoot: '',
20+
sources: [ '/folder/test.css' ],
21+
sourcesContent: [ '.class { a: b c d; }' ],
22+
version: 3
23+
},
24+
[
25+
[1, ".class { a: b c d; }", ""]
26+
],
27+
{
28+
query: "?sourceMap=false"
29+
}
30+
);
1331
testMap("generate sourceMap (1 loader)", ".class { a: b c d; }", undefined, {
1432
loaders: [{request: "/path/css-loader"}],
1533
options: { context: "/" },
@@ -95,4 +113,13 @@ describe("source maps", function() {
95113
version: 3
96114
}]
97115
]);
116+
testMap("don't generate sourceMap (1 loader)", ".class { a: b c d; }", undefined, {
117+
loaders: [{request: "/path/css-loader"}],
118+
options: { context: "/" },
119+
resource: "/folder/test.css",
120+
request: "/path/css-loader!/folder/test.css",
121+
query: "?sourceMap=false"
122+
}, [
123+
[1, ".class { a: b c d; }", ""]
124+
]);
98125
});

0 commit comments

Comments
 (0)
Please sign in to comment.