Skip to content

Commit fbb0714

Browse files
evilebottnawimichael-ciniawsky
authored andcommitted
fix: use btoa instead Buffer (#501)
1 parent 6ee2fc6 commit fbb0714

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/css-base.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function cssWithMappingToString(item, useSourceMap) {
5454
return content;
5555
}
5656

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

6969
// Adapted from convert-source-map (MIT)
7070
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;
71+
// eslint-disable-next-line no-undef
72+
var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
73+
var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
7374

74-
return '/*# ' + data + ' */';
75+
return '/*# ' + data + ' */';
7576
}

test/cssBaseTest.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1-
/*globals describe it*/
1+
/*eslint-env mocha*/
22

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

55
describe("css-base", function() {
6+
before(function() {
7+
global.btoa = function btoa(str) {
8+
var buffer = null;
9+
10+
if (str instanceof Buffer) {
11+
buffer = str;
12+
} else {
13+
buffer = new Buffer(str.toString(), 'binary');
14+
}
15+
16+
return buffer.toString('base64');
17+
}
18+
})
19+
20+
after(function () {
21+
global.btoa = null;
22+
})
23+
624
it("should toString a single module", function() {
725
var m = base();
826
m.push([1, "body { a: 1; }", ""]);
@@ -46,4 +64,17 @@ describe("css-base", function() {
4664
}]);
4765
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 */");
4866
});
67+
it("should toString without source mapping if btoa not avalibale", function() {
68+
global.btoa = null;
69+
var m = base(true);
70+
m.push([1, "body { a: 1; }", "", {
71+
file: "test.scss",
72+
sources: [
73+
'./path/to/test.scss'
74+
],
75+
mappings: "AAAA;",
76+
sourceRoot: "webpack://"
77+
}]);
78+
m.toString().should.be.eql("body { a: 1; }");
79+
});
4980
});

0 commit comments

Comments
 (0)