mirror of
https://codeberg.org/demostf/frontend.git
synced 2026-06-03 10:14:13 +02:00
js bundle wip
This commit is contained in:
parent
dc80d715a6
commit
305e8ec8ed
11 changed files with 3090 additions and 130 deletions
1349
Cargo.lock
generated
1349
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
4
build.rs
4
build.rs
|
|
@ -1,9 +1,11 @@
|
||||||
use demostf_build::{bundle_style, save_asset};
|
use demostf_build::{bundle_script, bundle_style, save_asset};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("cargo:rerun-if-changed=build.rs");
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
println!("cargo:rerun-if-changed=style");
|
println!("cargo:rerun-if-changed=style");
|
||||||
println!("cargo:rerun-if-changed=images");
|
println!("cargo:rerun-if-changed=images");
|
||||||
|
println!("cargo:rerun-if-changed=script");
|
||||||
|
|
||||||
save_asset!("style.css", bundle_style("style/style.css"));
|
save_asset!("style.css", bundle_style("style/style.css"));
|
||||||
|
save_asset!("upload.js", bundle_script("script/upload.js"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
1571
build/Cargo.lock
generated
1571
build/Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -8,3 +8,5 @@ lightningcss = { version = "1.0.0-alpha.40", features = ["browserslist", "visito
|
||||||
base64 = "0.21.0"
|
base64 = "0.21.0"
|
||||||
urlencoding = "2.1.2"
|
urlencoding = "2.1.2"
|
||||||
const-fnv1a-hash = "1.1.0"
|
const-fnv1a-hash = "1.1.0"
|
||||||
|
swc = "0.259.6"
|
||||||
|
swc_common = { version = "0.30.5", features = ["tty-emitter"] }
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
|
mod script;
|
||||||
mod style;
|
mod style;
|
||||||
|
|
||||||
use const_fnv1a_hash::fnv1a_hash_str_32;
|
use const_fnv1a_hash::fnv1a_hash_str_32;
|
||||||
|
pub use script::bundle_script;
|
||||||
pub use style::bundle_style;
|
pub use style::bundle_style;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
|
@ -9,8 +11,8 @@ macro_rules! save_asset {
|
||||||
let val = $val;
|
let val = $val;
|
||||||
let out_dir = std::env::var("OUT_DIR").unwrap();
|
let out_dir = std::env::var("OUT_DIR").unwrap();
|
||||||
std::fs::write(format!("{out_dir}/{}", $name), &val).expect("failed to write asset");
|
std::fs::write(format!("{out_dir}/{}", $name), &val).expect("failed to write asset");
|
||||||
let hash = fnv1a_hash_str_32(&val);
|
let hash = demostf_build::hash(&val);
|
||||||
std::fs::write(format!("{out_dir}/{}.hash", $name), format!("{:x}", hash))
|
std::fs::write(format!("{out_dir}/{}.hash", $name), hash)
|
||||||
.expect("failed to write asset hash");
|
.expect("failed to write asset hash");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
40
build/src/script.rs
Normal file
40
build/src/script.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
use std::{path::Path, sync::Arc};
|
||||||
|
use swc::config::Config;
|
||||||
|
use swc::{self, config::Options, BoolConfig};
|
||||||
|
use swc_common::{
|
||||||
|
errors::{ColorConfig, Handler},
|
||||||
|
SourceMap, GLOBALS,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn bundle_script(script: &str) -> String {
|
||||||
|
let output = GLOBALS.set(&Default::default(), || {
|
||||||
|
let cm = Arc::<SourceMap>::default();
|
||||||
|
let handler = Arc::new(Handler::with_tty_emitter(
|
||||||
|
ColorConfig::Auto,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
Some(cm.clone()),
|
||||||
|
));
|
||||||
|
let compiler = swc::Compiler::new(cm.clone());
|
||||||
|
|
||||||
|
let fm = cm
|
||||||
|
// filepath that actually exists relative to the binary
|
||||||
|
.load_file(Path::new(script))
|
||||||
|
.expect("failed to load file");
|
||||||
|
|
||||||
|
compiler
|
||||||
|
.process_js_file(
|
||||||
|
fm,
|
||||||
|
&handler,
|
||||||
|
&Options {
|
||||||
|
config: Config {
|
||||||
|
minify: BoolConfig::new(Some(true)),
|
||||||
|
..Config::default()
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("failed to process file")
|
||||||
|
});
|
||||||
|
output.code
|
||||||
|
}
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
OPENSSL_NO_VENDOR = 1;
|
OPENSSL_NO_VENDOR = 1;
|
||||||
|
|
||||||
nativeBuildInputs = with pkgs; [
|
nativeBuildInputs = with pkgs; [
|
||||||
cargo
|
rust-bin.nightly."2023-03-31".default
|
||||||
bacon
|
bacon
|
||||||
cargo-edit
|
cargo-edit
|
||||||
cargo-outdated
|
cargo-outdated
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
function test() {
|
||||||
|
console.log("test");
|
||||||
|
}
|
||||||
|
document.addEventListener("DOMContentLoaded", test);
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use axum::http::StatusCode;
|
||||||
use axum::response::{IntoResponse, Response};
|
use axum::response::{IntoResponse, Response};
|
||||||
use config::ConfigError;
|
use config::ConfigError;
|
||||||
|
|
||||||
|
|
@ -25,7 +26,9 @@ pub enum Error {
|
||||||
|
|
||||||
impl IntoResponse for Error {
|
impl IntoResponse for Error {
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
dbg!(self);
|
match self {
|
||||||
todo!()
|
Error::NotFound => (StatusCode::NOT_FOUND, "not found").into_response(),
|
||||||
|
_ => todo!(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,7 @@ async fn main() -> Result<()> {
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(index))
|
.route("/", get(index))
|
||||||
.route("/style.css", get(serve_compiled!("style.css")))
|
.route("/style.css", get(serve_compiled!("style.css")))
|
||||||
|
.route("/upload.js", get(serve_compiled!("upload.js")))
|
||||||
.route("/images/logo.png", get(serve_static!("../images/logo.png")))
|
.route("/images/logo.png", get(serve_static!("../images/logo.png")))
|
||||||
.route("/images/logo.svg", get(serve_static!("../images/logo.svg")))
|
.route("/images/logo.svg", get(serve_static!("../images/logo.svg")))
|
||||||
.route("/about", get(about))
|
.route("/about", get(about))
|
||||||
|
|
@ -137,9 +138,10 @@ async fn about(State(_app): State<Arc<App>>, session: SessionData) -> Result<Mar
|
||||||
|
|
||||||
async fn demo(
|
async fn demo(
|
||||||
State(app): State<Arc<App>>,
|
State(app): State<Arc<App>>,
|
||||||
Path(id): Path<u32>,
|
Path(id): Path<String>,
|
||||||
session: SessionData,
|
session: SessionData,
|
||||||
) -> Result<Markup> {
|
) -> Result<Markup> {
|
||||||
|
let id = id.parse().map_err(|_| Error::NotFound)?;
|
||||||
let demo = Demo::by_id(&app.connection, id)
|
let demo = Demo::by_id(&app.connection, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or(Error::NotFound)?;
|
.ok_or(Error::NotFound)?;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::asset::saved_asset_url;
|
||||||
use crate::pages::plugin_section::PluginSection;
|
use crate::pages::plugin_section::PluginSection;
|
||||||
use crate::pages::Page;
|
use crate::pages::Page;
|
||||||
use maud::{html, Markup};
|
use maud::{html, Markup};
|
||||||
|
|
@ -21,6 +22,7 @@ impl Page for UploadPage {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(&self) -> Markup {
|
fn render(&self) -> Markup {
|
||||||
|
let script = saved_asset_url!("upload.js");
|
||||||
html! {
|
html! {
|
||||||
.upload-page {
|
.upload-page {
|
||||||
section.upload {
|
section.upload {
|
||||||
|
|
@ -50,6 +52,7 @@ impl Page for UploadPage {
|
||||||
}
|
}
|
||||||
(self.plugin_section())
|
(self.plugin_section())
|
||||||
}
|
}
|
||||||
|
script src = (script);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue