Skip to content

Commit febf8d2

Browse files
authored
Allow custom web assets to be defined at package level (#485)
# Objective Closes #440. We allow the user to provide custom web assets to fully control how the Bevy app is loaded. However, it's currently only possible to provide the `web` folder for the whole workspace, providing custom assets for an individual package doesn't work. # Solution Search for the web folder first at the package level, then the workspace level. # Testing The [bevy_complex_repo](https://github.com/TimJentzsch/bevy_complex_repo) `workspace` folder has a `custom_web_folder` package where this can be tested. Run `bevy run -p custom_web_folder web` in the `workspace` folder, with the CLI installed from this branch. In the logs you should see that it's using the package-level web assets. The app itself should have an added "bevy" test in the HTML # Future Work The user might also want to customize the name of the folder / the entire path. This will be tackled in <#484>
1 parent 8b764fe commit febf8d2

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

src/web/bundle.rs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66

77
use anyhow::Context;
88
use cargo_metadata::Metadata;
9-
use tracing::info;
9+
use tracing::{info, warn};
1010

1111
use crate::bin_target::BinTarget;
1212

@@ -79,13 +79,48 @@ pub fn create_web_bundle(
7979
let wasm_file_name = OsString::from(format!("{}_bg.wasm", bin_target.bin_name));
8080
let js_file_name = OsString::from(format!("{}.js", bin_target.bin_name));
8181

82-
let custom_web_folder = Path::new("web");
83-
let index_path = custom_web_folder.join("index.html");
82+
let web_assets_folder = Path::new("web");
8483

85-
let index = if index_path.exists() {
86-
Index::File(index_path)
84+
let package_web_assets = Path::new(
85+
bin_target
86+
.package
87+
.manifest_path
88+
.parent()
89+
.context("failed to find package root")?,
90+
)
91+
.join(web_assets_folder);
92+
let workspace_web_assets = Path::new(&metadata.workspace_root).join(web_assets_folder);
93+
94+
tracing::debug!(
95+
"package={:?}, workspace={:?}",
96+
&package_web_assets,
97+
&workspace_web_assets,
98+
);
99+
100+
// Search for custom web assets first in the package, then in the workspace
101+
let web_assets = if package_web_assets.exists() {
102+
info!("using custom package web assets.");
103+
Some(package_web_assets)
104+
} else if workspace_web_assets.exists() {
105+
info!("using custom workspace web assets.");
106+
Some(workspace_web_assets)
107+
} else {
108+
info!("no custom web assets found, using defaults.");
109+
None
110+
};
111+
112+
let index_path = web_assets
113+
.as_ref()
114+
.map(|web_assets| web_assets.join("index.html"));
115+
116+
let index = if let Some(index_path) = index_path {
117+
if index_path.exists() {
118+
Index::File(index_path)
119+
} else {
120+
warn!("custom web assets don't contain index.html, using default.");
121+
Index::Content(default_index(bin_target))
122+
}
87123
} else {
88-
info!("no custom `web` folder found, using defaults.");
89124
Index::Content(default_index(bin_target))
90125
};
91126

@@ -96,9 +131,7 @@ pub fn create_web_bundle(
96131
wasm_file_name,
97132
js_file_name,
98133
assets_path: assets_path.exists().then(|| assets_path.to_owned()),
99-
web_assets: custom_web_folder
100-
.exists()
101-
.then(|| custom_web_folder.to_owned()),
134+
web_assets,
102135
index: Index::Content(index.clone()),
103136
};
104137

@@ -156,13 +189,13 @@ pub fn create_web_bundle(
156189
}
157190

158191
// Custom web assets
159-
if custom_web_folder.exists() {
192+
if let Some(web_assets) = &linked.web_assets {
160193
tracing::debug!(
161194
"copying custom web assets from file://{}",
162-
custom_web_folder.to_string_lossy()
195+
web_assets.to_string_lossy()
163196
);
164197
fs_extra::dir::copy(
165-
custom_web_folder,
198+
web_assets,
166199
&base_path,
167200
&fs_extra::dir::CopyOptions {
168201
overwrite: true,

0 commit comments

Comments
 (0)