Skip to content
Permalink
Browse files

Fix reading and writing source maps containing unicode characters

ae6f08b added the `'ascii'` parameter
to Buffer conversion functions `to_ascii` and `to_base64`. However
`to_ascii` didn't actually convert to ascii before, but to a utf8 string
(the default Buffer.toString behaviour). Passing in 'ascii' breaks
reading and writing valid source map with unicode characters.

This patch removes the 'ascii' parameter so that the conversion uses
utf8 again. I would've filed this upstream but it didn't get merged into
uglify-js yet (only uglify-es, and thus terser).

Ref mishoo/UglifyJS2#2947
  • Loading branch information...
goto-bus-stop authored and kzc committed May 23, 2018
1 parent 7d80b43 commit 9255757bcabbd35a8f69a4966e6a4f59b1927d36
Showing with 17 additions and 2 deletions.
  1. +2 −2 lib/minify.js
  2. +15 −0 test/sourcemaps.js
@@ -3,7 +3,7 @@
var to_ascii = typeof atob == "undefined" ? function(b64) {
if (Buffer.from && Buffer.from !== Uint8Array.from) {
// Node >= 4.5.0
return Buffer.from(b64, "base64").toString("ascii");
return Buffer.from(b64, "base64").toString();
} else {
// Node < 4.5.0, old API, manual safeguards
if (typeof b64 !== "string") throw new Errror("\"b64\" must be a string");
@@ -13,7 +13,7 @@ var to_ascii = typeof atob == "undefined" ? function(b64) {
var to_base64 = typeof btoa == "undefined" ? function(str) {
if (Buffer.from && Buffer.from !== Uint8Array.from) {
// Node >= 4.5.0
return Buffer.from(str, "ascii").toString("base64");
return Buffer.from(str).toString("base64");
} else {
// Node < 4.5.0, old API, manual safeguards
if (typeof str !== "string") throw new Errror("\"str\" must be a string");
@@ -23,6 +23,21 @@ module.exports = function () {
].join("\n"));

ok.deepEqual(issue836.names, ['enabled', 'x']);

// Test inline source maps with unicode characters
var with_inline_map = UglifyJS.minify([
"var tëst = '→unicøde←';",
"alert(tëst);",
].join("\n"), {
sourceMap: { url: 'inline', includeSources: true }
}).code;
var unicode_result = UglifyJS.minify(with_inline_map, {
sourceMap: { content: 'inline', includeSources: true }
});
ok.ifError(unicode_result.error);
var unicode_map = JSON.parse(unicode_result.map);

ok.deepEqual(unicode_map.names, ['tëst', 'alert']);
}

function source_map(js) {

0 comments on commit 9255757

Please sign in to comment.
You can’t perform that action at this time.