Skip to content

Commit

Permalink
Appropriately encode source maps to base64. Closes #1018. Based on #1318
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Jan 29, 2023
1 parent c53b5bc commit 6c726e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
15 changes: 9 additions & 6 deletions lib/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import {
reserve_quoted_keys,
} from "./propmangle.js";

var to_ascii = typeof atob == "undefined" ? function(b64) {
return Buffer.from(b64, "base64").toString();
} : atob;
var to_base64 = typeof btoa == "undefined" ? function(str) {
return Buffer.from(str).toString("base64");
} : btoa;
// to/from base64 functions
// Prefer built-in Buffer, if available, then use hack
// https://developer.mozilla.org/en-US/docs/Glossary/Base64#The_Unicode_Problem
var to_ascii = typeof Buffer !== "undefined"
? (b64) => Buffer.from(b64, "base64").toString()
: (b64) => decodeURIComponent(escape(atob(b64)));
var to_base64 = typeof Buffer !== "undefined"
? (str) => Buffer.from(str).toString("base64")
: (str) => btoa(unescape(encodeURIComponent(str)));

function read_source_map(code) {
var match = /(?:^|[^.])\/\/# sourceMappingURL=data:application\/json(;[\w=-]*)?;base64,([+/0-9A-Za-z]*=*)\s*$/.exec(code);
Expand Down
10 changes: 4 additions & 6 deletions test/mocha/sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ describe("sourcemaps", function() {
if (result.error) throw result.error;
assertCodeWithInlineMapEquals(result.code, read("./test/input/issue-505/output.js"));
});
// TODO skipped for 2 reasons:
// - atob/btoa fail with unicode characters
// - names output has changed and excludes unicode names
it.skip("Should work with unicode characters", async function() {
it.only("Should work with unicode characters", async function() {
var code = [
"var tëst = '→unicøde←';",
"alert(tëst);",
Expand All @@ -292,8 +289,9 @@ describe("sourcemaps", function() {
});
if (result.error) throw result.error;
map = JSON.parse(result.map);
assert.strictEqual(map.names.length, 2);
assert.strictEqual(map.names[0], "tëst");
assert.strictEqual(map.names.length, 1);
// We don't add non-ascii-identifier names
// assert.strictEqual(map.names[0], "tëst");
assert.strictEqual(map.names[0], "alert");
});
it("Should append source map to file when asObject is present", async function() {
Expand Down

0 comments on commit 6c726e9

Please sign in to comment.