aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-10-11 03:52:33 +0800
committerGitHub <noreply@github.com>2019-10-11 03:52:33 +0800
commit6d57ca1a59449cd11de296d091a35551ce9cbee7 (patch)
tree60d947a5031e7b4db89d46f7720a2eab9c75fa0a /lib
parent3320251b4b5caafb2d28fa22b027c7f0916f6b1a (diff)
downloadtracifyjs-6d57ca1a59449cd11de296d091a35551ce9cbee7.tar.gz
tracifyjs-6d57ca1a59449cd11de296d091a35551ce9cbee7.zip
improve source map handling (#3464)
fixes #2947 fixes #3277 fixes #3411
Diffstat (limited to 'lib')
-rw-r--r--lib/minify.js49
1 files changed, 34 insertions, 15 deletions
diff --git a/lib/minify.js b/lib/minify.js
index 9eebc373..93f1295e 100644
--- a/lib/minify.js
+++ b/lib/minify.js
@@ -1,19 +1,39 @@
"use strict";
-var to_ascii = typeof atob == "undefined" ? function(b64) {
- return new Buffer(b64, "base64").toString();
-} : atob;
-var to_base64 = typeof btoa == "undefined" ? function(str) {
- return new Buffer(str).toString("base64");
-} : btoa;
+var to_ascii, to_base64;
+if (typeof Buffer == "undefined") {
+ to_ascii = atob;
+ to_base64 = btoa;
+} else if (typeof Buffer.alloc == "undefined") {
+ to_ascii = function(b64) {
+ return new Buffer(b64, "base64").toString();
+ };
+ to_base64 = function(str) {
+ return new Buffer(str).toString("base64");
+ };
+} else {
+ to_ascii = function(b64) {
+ return Buffer.from(b64, "base64").toString();
+ };
+ to_base64 = function(str) {
+ return Buffer.from(str).toString("base64");
+ };
+}
-function read_source_map(name, code) {
- var match = /\n\/\/# sourceMappingURL=data:application\/json(;.*?)?;base64,(\S+)\s*$/.exec(code);
- if (!match) {
- AST_Node.warn("inline source map not found: " + name);
- return null;
+function read_source_map(name, toplevel) {
+ var comments = toplevel.end.comments_after;
+ for (var i = comments.length; --i >= 0;) {
+ var comment = comments[i];
+ if (comment.type != "comment1") break;
+ var match = /^# ([^\s=]+)=(\S+)\s*$/.exec(comment.value);
+ if (!match) break;
+ if (match[1] == "sourceMappingURL") {
+ match = /^data:application\/json(;.*?)?;base64,(\S+)$/.exec(match[2]);
+ if (!match) break;
+ return to_ascii(match[2]);
+ }
}
- return to_ascii(match[2]);
+ AST_Node.warn("inline source map not found: " + name);
}
function parse_source_map(content) {
@@ -134,10 +154,10 @@ function minify(files, options) {
source_maps = source_map_content && Object.create(null);
for (var name in files) if (HOP(files, name)) {
options.parse.filename = name;
- options.parse.toplevel = parse(files[name], options.parse);
+ options.parse.toplevel = toplevel = parse(files[name], options.parse);
if (source_maps) {
if (source_map_content == "inline") {
- var inlined_content = read_source_map(name, files[name]);
+ var inlined_content = read_source_map(name, toplevel);
if (inlined_content) {
source_maps[name] = parse_source_map(inlined_content);
}
@@ -146,7 +166,6 @@ function minify(files, options) {
}
}
}
- toplevel = options.parse.toplevel;
}
if (quoted_props) {
reserve_quoted_keys(toplevel, quoted_props);