Skip to content

Commit

Permalink
feat: Add minify_js to decrease file sizes (#210)
Browse files Browse the repository at this point in the history
This PR uses `minify_js` to minify rendered js files before writing.
This should reduce file sizes slightly.
I probably haven't caught all places js files are written, feel free to
add any that I missed ;)

(It may also be interesting to minify the html files, haven't looked
into that, yet)

Co-authored-by: Felix Wiegand <fxwiegand@wgdnet.de>
  • Loading branch information
tedil and fxwiegand committed Oct 11, 2022
1 parent 17e4a04 commit 7515317
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -26,6 +26,7 @@ lazy_static = "1.4.0"
regex = "1.6.0"
log = "0.4.17"
simplelog = "0.12.0"
minify-js = "0.2.4"

[dev-dependencies]
dir-assert = "0.2.0"
6 changes: 4 additions & 2 deletions src/render/portable/mod.rs
Expand Up @@ -3,6 +3,7 @@ pub(crate) mod utils;

use crate::render::portable::plot::get_min_max;
use crate::render::portable::plot::render_plots;
use crate::render::portable::utils::minify_js;
use crate::render::Renderer;
use crate::spec::{
CustomPlot, DatasetSpecs, HeaderSpecs, Heatmap, ItemSpecs, ItemsSpec, LinkSpec,
Expand Down Expand Up @@ -533,8 +534,9 @@ fn render_table_javascript<P: AsRef<Path>>(

let js = templates.render("table.js.tera", &context)?;

let mut file = fs::File::create(file_path)?;
file.write_all(js.as_bytes())?;
let mut file = File::create(file_path)?;
let minified = minify_js(&js)?;
file.write_all(&minified)?;

Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion src/render/portable/plot.rs
@@ -1,3 +1,4 @@
use crate::render::portable::utils::minify_js;
use crate::utils::column_type::{classify_table, ColumnType};
use anyhow::{Context as AnyhowContext, Result};
use csv::Reader;
Expand Down Expand Up @@ -58,7 +59,8 @@ pub(crate) fn render_plots<P: AsRef<Path>>(
let js = templates.render("plot.js.tera", &context)?;
let file_path = path.join(Path::new(&format!("plot_{}", index)).with_extension("js"));
let mut file = fs::File::create(file_path)?;
file.write_all(js.as_bytes())?;
let minified = minify_js(&js)?;
file.write_all(&minified)?;
}
Ok(())
}
Expand Down
12 changes: 12 additions & 0 deletions src/render/portable/utils.rs
@@ -1,5 +1,6 @@
use crate::spec::ItemsSpec;
use anyhow::Result;
use minify_js::{minify, TopLevelMode};
use std::fs;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -78,6 +79,17 @@ pub(crate) fn render_static_files<P: AsRef<Path>>(path: P) -> Result<()> {
Ok(())
}

pub(crate) fn minify_js(file: &str) -> Result<Vec<u8>> {
let mut minified: Vec<u8> = Vec::new();
minify(
TopLevelMode::Global,
file.as_bytes().to_vec(),
&mut minified,
)
.expect("Failed minifying js");
Ok(minified)
}

pub(crate) fn render_index_file<P: AsRef<Path>>(path: P, specs: &ItemsSpec) -> Result<()> {
let table = if let Some(default_view) = &specs.default_view {
default_view
Expand Down

0 comments on commit 7515317

Please sign in to comment.