Skip to content

Commit 6c726e9

Browse files
Appropriately encode source maps to base64. Closes #1018. Based on #1318
1 parent c53b5bc commit 6c726e9

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

lib/minify.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ import {
1919
reserve_quoted_keys,
2020
} from "./propmangle.js";
2121

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

2932
function read_source_map(code) {
3033
var match = /(?:^|[^.])\/\/# sourceMappingURL=data:application\/json(;[\w=-]*)?;base64,([+/0-9A-Za-z]*=*)\s*$/.exec(code);

test/mocha/sourcemaps.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,7 @@ describe("sourcemaps", function() {
262262
if (result.error) throw result.error;
263263
assertCodeWithInlineMapEquals(result.code, read("./test/input/issue-505/output.js"));
264264
});
265-
// TODO skipped for 2 reasons:
266-
// - atob/btoa fail with unicode characters
267-
// - names output has changed and excludes unicode names
268-
it.skip("Should work with unicode characters", async function() {
265+
it.only("Should work with unicode characters", async function() {
269266
var code = [
270267
"var tëst = '→unicøde←';",
271268
"alert(tëst);",
@@ -292,8 +289,9 @@ describe("sourcemaps", function() {
292289
});
293290
if (result.error) throw result.error;
294291
map = JSON.parse(result.map);
295-
assert.strictEqual(map.names.length, 2);
296-
assert.strictEqual(map.names[0], "tëst");
292+
assert.strictEqual(map.names.length, 1);
293+
// We don't add non-ascii-identifier names
294+
// assert.strictEqual(map.names[0], "tëst");
297295
assert.strictEqual(map.names[0], "alert");
298296
});
299297
it("Should append source map to file when asObject is present", async function() {

0 commit comments

Comments
 (0)