This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: use `const` * feat: use `getElementById` * fix: `getElementById` * feat: async load javascript (wow!) * feat: local Chart.js * feat: minifier + `loader.js` * feat: use `exists` instead of `try_exists`
- Loading branch information
1 parent
1dcb2b6
commit a8b7118
Showing
9 changed files
with
273 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
**/*.rs.bk | ||
**/*.env | ||
**/*.db3 | ||
**/config.toml | ||
**/config.toml | ||
**/www_mini |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,57 @@ | ||
use anyhow::{anyhow, Result}; | ||
use minify_html_onepass as mini; | ||
use static_files::resource_dir; | ||
use std::{fs, path::Path}; | ||
|
||
fn main() -> std::io::Result<()> { | ||
resource_dir("www").build() | ||
fn minify_files( | ||
from_directory: &str, | ||
to_directory: &str, | ||
current_path: Option<&str>, | ||
) -> Result<()> { | ||
let current_path = current_path.unwrap_or(from_directory); | ||
|
||
if !Path::new(to_directory).exists() { | ||
fs::create_dir_all(to_directory)?; | ||
} | ||
|
||
if Path::new(current_path).is_dir() { | ||
// Recursivly call `minify_files` on directory content. | ||
for entry in fs::read_dir(current_path)? { | ||
let entry = entry?.path(); | ||
let new_current_path = entry | ||
.to_str() | ||
.ok_or_else(|| anyhow!("failed reading path"))?; | ||
minify_files(from_directory, to_directory, Some(new_current_path))?; | ||
} | ||
} else { | ||
let new_path = current_path.replacen(from_directory, to_directory, 1); | ||
let new_path = Path::new(&new_path); | ||
|
||
// Create subdirectories if needed. | ||
if let Some(parent) = new_path.parent() { | ||
if !parent.exists() { | ||
fs::create_dir_all(parent)?; | ||
} | ||
} | ||
|
||
// Minify. | ||
let out = match new_path.extension().and_then(|x| x.to_str()) { | ||
Some("html") | Some("css") => { | ||
let data = fs::read(current_path)?; | ||
mini::copy(&data, &mini::Cfg::new()).map_err(|e| anyhow!("{:?}", e))? | ||
} | ||
_ => fs::read(current_path)?, | ||
}; | ||
|
||
fs::write(new_path, out)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<()> { | ||
minify_files("www", "www_mini", None)?; | ||
resource_dir("www_mini").build()?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
window.onload = () => { | ||
load_scripts( | ||
"scripts/main.js", | ||
"scripts/common.js", | ||
"scripts/bindings.js", | ||
"scripts/charts.js", | ||
"scripts/vendored/Chart.js.4.1.1.js" | ||
).then(() => { | ||
load(true); | ||
}).catch((error) => { | ||
console.error(error); | ||
}); | ||
} | ||
|
||
/** | ||
* Load everything. | ||
* @param {String} sources - source to a javascript file. | ||
* @param {bool} async - whether to load script async. | ||
* @returns {Promise<void>} nothing. | ||
*/ | ||
function load_scripts(...sources) { | ||
let promises = []; | ||
|
||
sources.forEach((source) => { | ||
promises.push(new Promise((resolve, reject) => { | ||
const temp = document.createElement("script"); | ||
temp.src = source; | ||
temp.onload = resolve; | ||
temp.onerror = reject; | ||
document.head.appendChild(temp); | ||
})); | ||
}); | ||
|
||
return Promise.all(promises).catch((error) => console.error(error)); | ||
} |
Oops, something went wrong.