From e03812ce3285270c8bad5a4e0350bab514eb6ad7 Mon Sep 17 00:00:00 2001 From: guybedford Date: Mon, 25 Jul 2016 13:54:24 +0200 Subject: [PATCH] provide source maps encoding in Node --- lib/global-eval.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/global-eval.js b/lib/global-eval.js index 523668d1..c060b435 100644 --- a/lib/global-eval.js +++ b/lib/global-eval.js @@ -4,7 +4,24 @@ var __exec; (function() { - var hasBtoa = typeof btoa != 'undefined'; + var hasBuffer = typeof Buffer != 'undefined'; + try { + if (new Buffer('a').toString('base64') != 'YQ==') + hasBuffer = false; + } + catch(e) { + hasBuffer = false; + } + + var sourceMapPrefix = '\n//# sourceMappingURL=data:application/json;base64,'; + function inlineSourceMap(sourceMapString) { + if (hasBuffer) + return sourceMapPrefix + new Buffer(sourceMapString).toString('base64'); + else if (typeof btoa != 'undefined') + return sourceMapPrefix + btoa(unescape(encodeURIComponent(sourceMap))); + else + return ''; + } function getSource(load, wrap) { var lastLineIndex = load.source.lastIndexOf('\n'); @@ -26,7 +43,7 @@ var __exec; + (load.source.substr(lastLineIndex, 15) != '\n//# sourceURL=' ? '\n//# sourceURL=' + load.address + (sourceMap ? '!transpiled' : '') : '') // add sourceMappingURL if load.metadata.sourceMap is set - + (sourceMap && hasBtoa && '\n//# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(sourceMap))) || ''); + + (sourceMap && inlineSourceMap(sourceMap)); } var curLoad;