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:
Evan AlmloffandJonathan Kelley authored and GitHub committed 2025-05-31 21:45:12 +02:00
1 parent f865e3f559
commit 58669f892d
45 files changed
+1174 -762

No files matched your search

+35 -34
View File
@@ -567,16 +567,15 @@ impl AppBuilder {
let original_artifacts = self.artifacts.as_ref().unwrap();
let asset_dir = self.build.asset_dir();
for (k, bundled) in res.assets.assets.iter() {
let k = dunce::canonicalize(k)?;
if original_artifacts.assets.assets.contains_key(k.as_path()) {
for bundled in res.assets.assets() {
if original_artifacts.assets.contains(bundled) {
continue;
}
let from = dunce::canonicalize(PathBuf::from(bundled.absolute_source_path()))?;
let from = k.clone();
let to = asset_dir.join(bundled.bundled_path());
tracing::debug!("Copying asset from patch: {}", k.display());
tracing::debug!("Copying asset from patch: {}", from.display());
if let Err(e) = dioxus_cli_opt::process_file_to(bundled.options(), &from, &to) {
tracing::error!("Failed to copy asset: {e}");
continue;
@@ -584,13 +583,8 @@ impl AppBuilder {
// If the emulator is android, we need to copy the asset to the device with `adb push asset /data/local/tmp/dx/assets/filename.ext`
if self.build.platform == Platform::Android {
let changed_file = dunce::canonicalize(k).inspect_err(|e| {
tracing::debug!("Failed to canonicalize hotreloaded asset: {e}")
})?;
let bundled_name = PathBuf::from(bundled.bundled_path());
_ = self
.copy_file_to_android_tmp(&changed_file, &bundled_name)
.await;
_ = self.copy_file_to_android_tmp(&from, &bundled_name).await;
}
}
@@ -649,10 +643,13 @@ impl AppBuilder {
/// dir that the system simulator might be providing. We know this is the case for ios simulators
/// and haven't yet checked for android.
///
/// This will return the bundled name of the asset such that we can send it to the clients letting
/// This will return the bundled name of the assets such that we can send it to the clients letting
/// them know what to reload. It's not super important that this is robust since most clients will
/// kick all stylsheets without necessarily checking the name.
pub(crate) async fn hotreload_bundled_asset(&self, changed_file: &PathBuf) -> Option<PathBuf> {
pub(crate) async fn hotreload_bundled_assets(
&self,
changed_file: &PathBuf,
) -> Option<Vec<PathBuf>> {
let artifacts = self.artifacts.as_ref()?;
// Use the build dir if there's no runtime asset dir as the override. For the case of ios apps,
@@ -668,32 +665,36 @@ impl AppBuilder {
.ok()?;
// The asset might've been renamed thanks to the manifest, let's attempt to reload that too
let resource = artifacts.assets.assets.get(&changed_file)?;
let output_path = asset_dir.join(resource.bundled_path());
let resources = artifacts.assets.get_assets_for_source(&changed_file)?;
let mut bundled_names = Vec::new();
for resource in resources {
let output_path = asset_dir.join(resource.bundled_path());
tracing::debug!("Hotreloading asset {changed_file:?} in target {asset_dir:?}");
tracing::debug!("Hotreloading asset {changed_file:?} in target {asset_dir:?}");
// Remove the old asset if it exists
_ = std::fs::remove_file(&output_path);
// Remove the old asset if it exists
_ = std::fs::remove_file(&output_path);
// And then process the asset with the options into the **old** asset location. If we recompiled,
// the asset would be in a new location because the contents and hash have changed. Since we are
// hotreloading, we need to use the old asset location it was originally written to.
let options = *resource.options();
let res = process_file_to(&options, &changed_file, &output_path);
let bundled_name = PathBuf::from(resource.bundled_path());
if let Err(e) = res {
tracing::debug!("Failed to hotreload asset {e}");
// And then process the asset with the options into the **old** asset location. If we recompiled,
// the asset would be in a new location because the contents and hash have changed. Since we are
// hotreloading, we need to use the old asset location it was originally written to.
let options = *resource.options();
let res = process_file_to(&options, &changed_file, &output_path);
let bundled_name = PathBuf::from(resource.bundled_path());
if let Err(e) = res {
tracing::debug!("Failed to hotreload asset {e}");
}
// If the emulator is android, we need to copy the asset to the device with `adb push asset /data/local/tmp/dx/assets/filename.ext`
if self.build.platform == Platform::Android {
_ = self
.copy_file_to_android_tmp(&changed_file, &bundled_name)
.await;
}
bundled_names.push(bundled_name);
}
// If the emulator is android, we need to copy the asset to the device with `adb push asset /data/local/tmp/dx/assets/filename.ext`
if self.build.platform == Platform::Android {
_ = self
.copy_file_to_android_tmp(&changed_file, &bundled_name)
.await;
}
Some(bundled_name)
Some(bundled_names)
}
/// Copy this file to the tmp folder on the android device, returning the path to the copied file