Skip to content

Commit

Permalink
fix: use btoa instead Buffer (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Apr 20, 2017
1 parent 6ee2fc6 commit fbb0714
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/css-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function cssWithMappingToString(item, useSourceMap) {
return content;
}

if (useSourceMap) {
if (useSourceMap && typeof btoa === 'function') {
var sourceMapping = toComment(cssMapping);
var sourceURLs = cssMapping.sources.map(function (source) {
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'
Expand All @@ -68,8 +68,9 @@ function cssWithMappingToString(item, useSourceMap) {

// 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;
// eslint-disable-next-line no-undef
var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;

return '/*# ' + data + ' */';
return '/*# ' + data + ' */';
}
33 changes: 32 additions & 1 deletion test/cssBaseTest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
/*globals describe it*/
/*eslint-env mocha*/

var base = require("../lib/css-base");

describe("css-base", function() {
before(function() {
global.btoa = function btoa(str) {
var buffer = null;

if (str instanceof Buffer) {
buffer = str;
} else {
buffer = new Buffer(str.toString(), 'binary');
}

return buffer.toString('base64');
}
})

after(function () {
global.btoa = null;
})

it("should toString a single module", function() {
var m = base();
m.push([1, "body { a: 1; }", ""]);
Expand Down Expand Up @@ -46,4 +64,17 @@ describe("css-base", function() {
}]);
m.toString().should.be.eql("body { a: 1; }\n/*# sourceURL=webpack://./path/to/test.scss */\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC5zY3NzIiwic291cmNlcyI6WyIuL3BhdGgvdG8vdGVzdC5zY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTsiLCJzb3VyY2VSb290Ijoid2VicGFjazovLyJ9 */");
});
it("should toString without source mapping if btoa not avalibale", function() {
global.btoa = null;
var m = base(true);
m.push([1, "body { a: 1; }", "", {
file: "test.scss",
sources: [
'./path/to/test.scss'
],
mappings: "AAAA;",
sourceRoot: "webpack://"
}]);
m.toString().should.be.eql("body { a: 1; }");
});
});

0 comments on commit fbb0714

Please sign in to comment.