Allow relative assets in rust > 1.88 (#4658)

* Allow relative assets in rust > 1.88 and assets outside of the crate in
binary crates

* undo changes to assets outside of the crate

* don't use relative assets in examples
This commit is contained in:
Evan Almloff authored and GitHub committed 2025-09-15 10:43:30 -07:00
1 parent 707bde2986
commit 5104644b34
10 files changed
+28 -14

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@ keywords = ["web", "desktop", "mobile", "gui", "wasm"]
proc-macro = true
[dependencies]
proc-macro2 = { version = "1.0" }
proc-macro2 = { workspace = true }
quote = { workspace = true }
[features]
+1 -1
View File
@@ -15,7 +15,7 @@ proc-macro = true
# be careful with dependencies you add here - these need to get compiled for the proc macro and therefore
# prevent the main code from compiling!
[dependencies]
proc-macro2 = { workspace = true }
proc-macro2 = { workspace = true, features = ["span-locations"] }
quote = { workspace = true }
syn = { workspace = true, features = ["full", "extra-traits"] }
manganis-core = { workspace = true }
@@ -45,7 +45,7 @@ impl Parse for AssetParser {
fn parse(input: ParseStream) -> syn::Result<Self> {
// And then parse the options
let (MacroString(src), path_expr) = input.call(crate::parse_with_tokens)?;
let asset = resolve_path(&src);
let asset = resolve_path(&src, path_expr.span());
let _comma = input.parse::<Token![,]>();
let options = input.parse()?;
@@ -4,6 +4,7 @@ use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
use syn::{
parse::{Parse, ParseStream},
spanned::Spanned,
token::Comma,
Ident, Token, Visibility,
};
@@ -27,7 +28,7 @@ impl Parse for CssModuleParser {
// Asset path "/path.css"
let (MacroString(src), path_expr) = input.call(crate::parse_with_tokens)?;
let asset = resolve_path(&src);
let asset = resolve_path(&src, path_expr.span());
let _comma = input.parse::<Comma>();
+15 -2
View File
@@ -9,6 +9,7 @@ use std::{
use css_module::CssModuleParser;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
@@ -169,7 +170,7 @@ pub fn css_module(input: TokenStream) -> TokenStream {
quote! { #style }.into_token_stream().into()
}
fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> {
fn resolve_path(raw: &str, span: Span) -> Result<PathBuf, AssetParseError> {
// Get the location of the root of the crate which is where all assets are relative to
//
// IE
@@ -186,8 +187,18 @@ fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> {
// 1. the input file should be a pathbuf
let input = PathBuf::from(raw);
let path = if raw.starts_with('.') {
if let Some(local_folder) = span.local_file().as_ref().and_then(|f| f.parent()) {
local_folder.join(raw)
} else {
return Err(AssetParseError::RelativeAssetPath);
}
} else {
manifest_dir.join(raw.trim_start_matches('/'))
};
// 2. absolute path to the asset
let Ok(path) = std::path::absolute(manifest_dir.join(raw.trim_start_matches('/'))) else {
let Ok(path) = std::path::absolute(path) else {
return Err(AssetParseError::InvalidPath {
path: input.clone(),
});
@@ -275,6 +286,7 @@ enum AssetParseError {
IoError { err: std::io::Error, path: PathBuf },
InvalidPath { path: PathBuf },
FailedToReadAsset(std::io::Error),
RelativeAssetPath,
}
impl std::fmt::Display for AssetParseError {
@@ -294,6 +306,7 @@ impl std::fmt::Display for AssetParseError {
)
}
AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err),
AssetParseError::RelativeAssetPath => write!(f, "Failed to resolve relative asset path. Relative assets are only supported in rust 1.88+."),
}
}
}