js bundle wip

This commit is contained in:
Robin Appelman 2023-04-09 17:03:46 +02:00
commit 305e8ec8ed
11 changed files with 3090 additions and 130 deletions

View file

@ -1,6 +1,8 @@
mod script;
mod style;
use const_fnv1a_hash::fnv1a_hash_str_32;
pub use script::bundle_script;
pub use style::bundle_style;
#[macro_export]
@ -9,8 +11,8 @@ macro_rules! save_asset {
let val = $val;
let out_dir = std::env::var("OUT_DIR").unwrap();
std::fs::write(format!("{out_dir}/{}", $name), &val).expect("failed to write asset");
let hash = fnv1a_hash_str_32(&val);
std::fs::write(format!("{out_dir}/{}.hash", $name), format!("{:x}", hash))
let hash = demostf_build::hash(&val);
std::fs::write(format!("{out_dir}/{}.hash", $name), hash)
.expect("failed to write asset hash");
};
}

40
build/src/script.rs Normal file
View 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
}