Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra source map file + write source maps only if they are enabled #453

Merged
merged 2 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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