diff options
Diffstat (limited to 'lib/minify.js')
-rw-r--r-- | lib/minify.js | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/lib/minify.js b/lib/minify.js index 0e117ed1..399b8615 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -7,15 +7,23 @@ var to_base64 = typeof btoa == "undefined" ? function(str) { return new Buffer(str).toString("base64"); } : btoa; -function read_source_map(code) { +function read_source_map(name, code) { var match = /\n\/\/# sourceMappingURL=data:application\/json(;.*?)?;base64,(.*)/.exec(code); if (!match) { - AST_Node.warn("inline source map not found"); + AST_Node.warn("inline source map not found: " + name); return null; } return to_ascii(match[2]); } +function parse_source_map(content) { + try { + return JSON.parse(content); + } catch (ex) { + throw new Error("invalid input source map: " + content); + } +} + function set_shorthand(name, options, keys) { if (options[name]) { keys.forEach(function(key) { @@ -113,7 +121,7 @@ function minify(files, options) { }; } if (timings) timings.parse = Date.now(); - var toplevel; + var source_maps, toplevel; if (files instanceof AST_Toplevel) { toplevel = files; } else { @@ -122,13 +130,23 @@ function minify(files, options) { } options.parse = options.parse || {}; options.parse.toplevel = null; + var source_map_content = options.sourceMap && options.sourceMap.content; + if (typeof source_map_content == "string" && source_map_content != "inline") { + source_map_content = parse_source_map(source_map_content); + } + 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); - if (options.sourceMap && options.sourceMap.content == "inline") { - if (Object.keys(files).length > 1) - throw new Error("inline source map only works with singular input"); - options.sourceMap.content = read_source_map(files[name]); + if (source_maps) { + if (source_map_content == "inline") { + var inlined_content = read_source_map(name, files[name]); + if (inlined_content) { + source_maps[name] = parse_source_map(inlined_content); + } + } else { + source_maps[name] = source_map_content; + } } } toplevel = options.parse.toplevel; @@ -164,16 +182,9 @@ function minify(files, options) { } if (!HOP(options.output, "code") || options.output.code) { if (options.sourceMap) { - if (typeof options.sourceMap.content == "string") { - try { - options.sourceMap.content = JSON.parse(options.sourceMap.content); - } catch (ex) { - throw new Error("invalid input source map: " + options.sourceMap.content); - } - } options.output.source_map = SourceMap({ file: options.sourceMap.filename, - orig: options.sourceMap.content, + orig: source_maps, root: options.sourceMap.root }); if (options.sourceMap.includeSources) { |