Skip to content

Commit 4250129

Browse files
authored
Remove extra source map file + write source maps only if they are enabled (webpack-contrib#453)
* chore: Eliminate convert-source-map The needed functionality has been extracted to a function. * fix: Write sourcemaps only if `sourceMap` is enabled
1 parent ee7234b commit 4250129

File tree

4 files changed

+26
-152
lines changed

4 files changed

+26
-152
lines changed

lib/convert-source-map.js

-141
This file was deleted.

lib/css-base.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
Author Tobias Koppers @sokra
44
*/
55
// css base code, injected by the css-loader
6-
module.exports = function() {
6+
module.exports = function(useSourceMap) {
77
var list = [];
88

99
// return the list of modules as css string
1010
list.toString = function toString() {
1111
return this.map(function (item) {
12-
var content = cssWithMappingToString(item);
12+
var content = cssWithMappingToString(item, useSourceMap);
1313
if(item[2]) {
1414
return "@media " + item[2] + "{" + content + "}";
1515
} else {
@@ -47,16 +47,29 @@ module.exports = function() {
4747
return list;
4848
};
4949

50-
function cssWithMappingToString(item) {
50+
function cssWithMappingToString(item, useSourceMap) {
5151
var content = item[1] || '';
5252
var cssMapping = item[3];
5353
if (!cssMapping) {
5454
return content;
5555
}
56-
var convertSourceMap = require('./convert-source-map');
57-
var sourceMapping = convertSourceMap.fromObject(cssMapping).toComment({multiline: true});
58-
var sourceURLs = cssMapping.sources.map(function (source) {
59-
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'
60-
});
61-
return [content].concat(sourceURLs).concat([sourceMapping]).join('\n');
56+
57+
if (useSourceMap) {
58+
var sourceMapping = toComment(cssMapping);
59+
var sourceURLs = cssMapping.sources.map(function (source) {
60+
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'
61+
});
62+
63+
return [content].concat(sourceURLs).concat([sourceMapping]).join('\n');
64+
}
65+
66+
return [content].join('\n');
67+
}
68+
69+
// Adapted from convert-source-map (MIT)
70+
function toComment(sourceMap) {
71+
var base64 = new Buffer(JSON.stringify(sourceMap)).toString('base64');
72+
var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
73+
74+
return '/*# ' + data + ' */';
6275
}

lib/loader.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ module.exports = function(content, map) {
108108
}
109109

110110
// embed runtime
111-
callback(null, "exports = module.exports = require(" + loaderUtils.stringifyRequest(this, require.resolve("./css-base.js")) + ")();\n" +
111+
callback(null, "exports = module.exports = require(" +
112+
loaderUtils.stringifyRequest(this, require.resolve("./css-base.js")) +
113+
")(" + query.sourceMap + ");\n" +
112114
"// imports\n" +
113115
importJs + "\n\n" +
114116
"// module\n" +

test/cssBaseTest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("css-base", function() {
3535
"@media screen{body { a: 1; }}");
3636
});
3737
it("should toString with source mapping", function() {
38-
var m = base();
38+
var m = base(true);
3939
m.push([1, "body { a: 1; }", "", {
4040
file: "test.scss",
4141
sources: [

0 commit comments

Comments
 (0)