From fbb07146c2ee4b4f55ae8088f2226619ab61ff62 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Thu, 20 Apr 2017 14:41:11 +0300 Subject: [PATCH] fix: use `btoa` instead `Buffer` (#501) --- lib/css-base.js | 9 +++++---- test/cssBaseTest.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/css-base.js b/lib/css-base.js index b67c1d74..59af87df 100644 --- a/lib/css-base.js +++ b/lib/css-base.js @@ -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 + ' */' @@ -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 + ' */'; } diff --git a/test/cssBaseTest.js b/test/cssBaseTest.js index de176a66..01b2a2ee 100644 --- a/test/cssBaseTest.js +++ b/test/cssBaseTest.js @@ -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; }", ""]); @@ -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; }"); + }); });