Skip to content

Commit

Permalink
Remove extra source map file + write source maps only if they are ena…
Browse files Browse the repository at this point in the history
…bled (#453)

* chore: Eliminate convert-source-map

The needed functionality has been extracted to a function.

* fix: Write sourcemaps only if `sourceMap` is enabled
  • Loading branch information
bebraw authored Mar 13, 2017
1 parent ee7234b commit 4250129
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 152 deletions.
141 changes: 0 additions & 141 deletions lib/convert-source-map.js

This file was deleted.

31 changes: 22 additions & 9 deletions lib/css-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Author Tobias Koppers @sokra
*/
// css base code, injected by the css-loader
module.exports = function() {
module.exports = function(useSourceMap) {
var list = [];

// return the list of modules as css string
list.toString = function toString() {
return this.map(function (item) {
var content = cssWithMappingToString(item);
var content = cssWithMappingToString(item, useSourceMap);
if(item[2]) {
return "@media " + item[2] + "{" + content + "}";
} else {
Expand Down Expand Up @@ -47,16 +47,29 @@ module.exports = function() {
return list;
};

function cssWithMappingToString(item) {
function cssWithMappingToString(item, useSourceMap) {
var content = item[1] || '';
var cssMapping = item[3];
if (!cssMapping) {
return content;
}
var convertSourceMap = require('./convert-source-map');
var sourceMapping = convertSourceMap.fromObject(cssMapping).toComment({multiline: true});
var sourceURLs = cssMapping.sources.map(function (source) {
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'
});
return [content].concat(sourceURLs).concat([sourceMapping]).join('\n');

if (useSourceMap) {
var sourceMapping = toComment(cssMapping);
var sourceURLs = cssMapping.sources.map(function (source) {
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'
});

return [content].concat(sourceURLs).concat([sourceMapping]).join('\n');
}

return [content].join('\n');
}

// Adapted from convert-source-map (MIT)
function toComment(sourceMap) {
var base64 = new Buffer(JSON.stringify(sourceMap)).toString('base64');
var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;

return '/*# ' + data + ' */';
}
4 changes: 3 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ module.exports = function(content, map) {
}

// embed runtime
callback(null, "exports = module.exports = require(" + loaderUtils.stringifyRequest(this, require.resolve("./css-base.js")) + ")();\n" +
callback(null, "exports = module.exports = require(" +
loaderUtils.stringifyRequest(this, require.resolve("./css-base.js")) +
")(" + query.sourceMap + ");\n" +
"// imports\n" +
importJs + "\n\n" +
"// module\n" +
Expand Down
2 changes: 1 addition & 1 deletion test/cssBaseTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("css-base", function() {
"@media screen{body { a: 1; }}");
});
it("should toString with source mapping", function() {
var m = base();
var m = base(true);
m.push([1, "body { a: 1; }", "", {
file: "test.scss",
sources: [
Expand Down

0 comments on commit 4250129

Please sign in to comment.