Move asset hashing into the CLI (#3988)
* Fix deduping assets * hash assets in the linker * fix extracting assets * Fix the hashed asset name * deserialize the asset back from the link section * prevent the read from being inlined * remove some extra logs * Fix liveview routing * Only look for direct dependencies on dioxus features * fix wasm split test * On wasm, create a new object file with the asset information * Fix multiple assets * add a regression test for 3467 * fix image link * fix linker out file * fix linux linker args * add windows linker command * fix linux link args * fix windows target triple * Expose an option to build assets in the linker intercept * Fix the standalone optimizer and document it in the dx help command * include the cli opt version in the hash * fix clippy * fix ssg port * add a safety note to the read_volatile call * move found assets log * fix some merge conflicts * fix build * make most env var for the linker optional * guess the target triple when dx acts as the linker intercept * automatically find the linker for normal builds * use the right linker in wasm mode * fix mac linker * look inside archive files (rlibs) * remove out.txt * fix clippy * fix typo * fix clippy * fix build * fix dx link name in docs * ignore libcompiler_builtins warnings * fix playwright * pass typocheck * fix printing sysroot * Modify final binary * fix wasm assets * remove old link section logic * fix tests * forward manganis export args * fail to evaluate relocatable address * retain all exports in the same pattern as other platforms * Fix adding assets during a wasm hot patch * less nesting and remove logs * fix clippy * log pdb files * read from pdb * print data section contents * print with object * normal windows builds working * fix hot patch windows * clean up logs * fix clippy * clean up assets * use the most recent pdb * fix doc test * fix typo * tiny nits * make bundled a reference instead of a pointer * add helpful comment to impl * match on file format * Fix asset hashing comment --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
1 parent
f865e3f559
commit
58669f892d
45 files changed
+1174
-762
No files matched your search
+62
-15
@@ -1,3 +1,4 @@
|
||||
use std::hash::Hasher;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -24,6 +25,8 @@ use swc_ecma_codegen::text_writer::JsWriter;
|
||||
use swc_ecma_loader::{resolvers::node::NodeModulesResolver, TargetEnv};
|
||||
use swc_ecma_parser::{parse_file_as_module, Syntax};
|
||||
|
||||
use crate::hash::hash_file_contents;
|
||||
|
||||
struct TracingEmitter;
|
||||
|
||||
impl Emitter for TracingEmitter {
|
||||
@@ -43,30 +46,32 @@ impl Emitter for TracingEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
/// Run a closure with the swc globals and handler set up
|
||||
fn inside_handler<O>(f: impl FnOnce(&Globals, Lrc<SourceMap>) -> O) -> O {
|
||||
let globals = Globals::new();
|
||||
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
||||
let handler = Handler::with_emitter_and_flags(Box::new(TracingEmitter), Default::default());
|
||||
GLOBALS.set(&globals, || HANDLER.set(&handler, || f(&globals, cm)))
|
||||
}
|
||||
|
||||
fn bundle_js_to_writer(
|
||||
file: PathBuf,
|
||||
bundle: bool,
|
||||
minify: bool,
|
||||
write_to: &mut impl std::io::Write,
|
||||
) -> anyhow::Result<()> {
|
||||
let globals = Globals::new();
|
||||
let handler = Handler::with_emitter_and_flags(Box::new(TracingEmitter), Default::default());
|
||||
GLOBALS.set(&globals, || {
|
||||
HANDLER.set(&handler, || {
|
||||
bundle_js_to_writer_inside_handler(&globals, file, bundle, minify, write_to)
|
||||
})
|
||||
inside_handler(|globals, cm| {
|
||||
bundle_js_to_writer_inside_handler(globals, cm, file, bundle, minify, write_to)
|
||||
})
|
||||
}
|
||||
|
||||
fn bundle_js_to_writer_inside_handler(
|
||||
fn resolve_js_inside_handler(
|
||||
globals: &Globals,
|
||||
file: PathBuf,
|
||||
bundle: bool,
|
||||
minify: bool,
|
||||
write_to: &mut impl std::io::Write,
|
||||
) -> anyhow::Result<()> {
|
||||
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
||||
let mut module = if bundle {
|
||||
cm: &Lrc<SourceMap>,
|
||||
) -> anyhow::Result<Module> {
|
||||
if bundle {
|
||||
let node_resolver = NodeModulesResolver::new(TargetEnv::Browser, Default::default(), true);
|
||||
let mut bundler = Bundler::new(
|
||||
globals,
|
||||
@@ -89,7 +94,7 @@ fn bundle_js_to_writer_inside_handler(
|
||||
let bundle = bundles
|
||||
.pop()
|
||||
.ok_or_else(|| anyhow::anyhow!("swc did not output any bundles"))?;
|
||||
bundle.module
|
||||
Ok(bundle.module)
|
||||
} else {
|
||||
let fm = cm.load_file(Path::new(&file)).expect("Failed to load file");
|
||||
|
||||
@@ -108,8 +113,19 @@ fn bundle_js_to_writer_inside_handler(
|
||||
error.cancel();
|
||||
anyhow::anyhow!("{}", error.message())
|
||||
})
|
||||
})?
|
||||
};
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn bundle_js_to_writer_inside_handler(
|
||||
globals: &Globals,
|
||||
cm: Lrc<SourceMap>,
|
||||
file: PathBuf,
|
||||
bundle: bool,
|
||||
minify: bool,
|
||||
write_to: &mut impl std::io::Write,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut module = resolve_js_inside_handler(globals, file, bundle, &cm)?;
|
||||
|
||||
if minify {
|
||||
module = swc_ecma_minifier::optimize(
|
||||
@@ -246,3 +262,34 @@ pub(crate) fn process_js(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn hash_js_module(file: PathBuf, hasher: &mut impl Hasher, bundle: bool) -> anyhow::Result<()> {
|
||||
inside_handler(|globals, cm| {
|
||||
_ = resolve_js_inside_handler(globals, file, bundle, &cm)?;
|
||||
|
||||
for file in cm.files().iter() {
|
||||
let hash = file.src_hash;
|
||||
hasher.write(&hash.to_le_bytes());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn hash_js(
|
||||
js_options: &JsAssetOptions,
|
||||
source: &Path,
|
||||
hasher: &mut impl Hasher,
|
||||
bundle: bool,
|
||||
) -> anyhow::Result<()> {
|
||||
if js_options.minified() {
|
||||
if let Err(err) = hash_js_module(source.to_path_buf(), hasher, bundle) {
|
||||
tracing::error!("Failed to minify js. Falling back to non-minified: {err}");
|
||||
hash_file_contents(source, hasher)?;
|
||||
}
|
||||
} else {
|
||||
hash_file_contents(source, hasher)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user