mod prettyprint;
mod source_map_generator;
pub use prettyprint::*;
pub use source_map_generator::*;
pub fn should_prettyprint(source_str: &str) -> bool {
if source_str.starts_with("//PRETTYPRINT") {
return true;
}
let mut lines = 0;
let mut line_lengths = 0;
for line in source_str.lines() {
lines += 1;
line_lengths += line.len();
}
if lines == 0 {
return false;
}
line_lengths / lines > 100
}
pub fn maybe_prettyprint<G>(script_name: &str, source_str: &mut String, mut generate_file: G)
where
G: FnMut(String, String) -> String,
{
if !should_prettyprint(source_str) {
return;
}
let (pretty_str, mappings) = prettyprint(&source_str);
let source_map_name = format!("{}.sourcemap", script_name);
let pretty_name = format!("{}.pretty", script_name);
let source_map = generate_source_map(pretty_name, pretty_str, mappings);
let url = generate_file(source_map_name, source_map);
source_str.push_str("\n//# sourceMappingURL=");
source_str.push_str(&url);
}