mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-01-30 05:23:27 +00:00
inject included styles and preload images into the head
This commit is contained in:
parent
0ec085fbbd
commit
8d3c11ef15
5 changed files with 65 additions and 11 deletions
|
@ -81,7 +81,7 @@ toml_edit = "0.19.11"
|
||||||
tauri-bundler = { version = "1.2", features = ["native-tls-vendored"] }
|
tauri-bundler = { version = "1.2", features = ["native-tls-vendored"] }
|
||||||
tauri-utils = "1.3"
|
tauri-utils = "1.3"
|
||||||
|
|
||||||
collect-assets = { git = "https://github.com/DioxusLabs/collect-assets" }
|
assets-cli-support = { git = "https://github.com/DioxusLabs/collect-assets" }
|
||||||
|
|
||||||
dioxus-autofmt = { workspace = true }
|
dioxus-autofmt = { workspace = true }
|
||||||
dioxus-check = { workspace = true }
|
dioxus-check = { workspace = true }
|
||||||
|
|
|
@ -2,8 +2,8 @@ use crate::{
|
||||||
config::{CrateConfig, ExecutableType},
|
config::{CrateConfig, ExecutableType},
|
||||||
error::{Error, Result},
|
error::{Error, Result},
|
||||||
tools::Tool,
|
tools::Tool,
|
||||||
DioxusConfig,
|
|
||||||
};
|
};
|
||||||
|
use assets_cli_support::AssetManifestExt;
|
||||||
use cargo_metadata::{diagnostic::Diagnostic, Message};
|
use cargo_metadata::{diagnostic::Diagnostic, Message};
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
@ -239,6 +239,8 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result<BuildResult> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process_assets(config)?;
|
||||||
|
|
||||||
Ok(BuildResult {
|
Ok(BuildResult {
|
||||||
warnings: warning_messages,
|
warnings: warning_messages,
|
||||||
elapsed_time: t_start.elapsed().as_millis(),
|
elapsed_time: t_start.elapsed().as_millis(),
|
||||||
|
@ -428,7 +430,7 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result<Vec<Diagnostic>> {
|
||||||
Ok(warning_messages)
|
Ok(warning_messages)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
|
pub fn gen_page(config: &CrateConfig, serve: bool) -> String {
|
||||||
let crate_root = crate::cargo::crate_root().unwrap();
|
let crate_root = crate::cargo::crate_root().unwrap();
|
||||||
let custom_html_file = crate_root.join("index.html");
|
let custom_html_file = crate_root.join("index.html");
|
||||||
let mut html = if custom_html_file.is_file() {
|
let mut html = if custom_html_file.is_file() {
|
||||||
|
@ -443,7 +445,7 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
|
||||||
String::from(include_str!("./assets/index.html"))
|
String::from(include_str!("./assets/index.html"))
|
||||||
};
|
};
|
||||||
|
|
||||||
let resouces = config.web.resource.clone();
|
let resouces = config.dioxus_config.web.resource.clone();
|
||||||
|
|
||||||
let mut style_list = resouces.style.unwrap_or_default();
|
let mut style_list = resouces.style.unwrap_or_default();
|
||||||
let mut script_list = resouces.script.unwrap_or_default();
|
let mut script_list = resouces.script.unwrap_or_default();
|
||||||
|
@ -462,7 +464,7 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
|
||||||
&style.to_str().unwrap(),
|
&style.to_str().unwrap(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
if config
|
if config.dioxus_config
|
||||||
.application
|
.application
|
||||||
.tools
|
.tools
|
||||||
.clone()
|
.clone()
|
||||||
|
@ -471,6 +473,26 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
|
||||||
{
|
{
|
||||||
style_str.push_str("<link rel=\"stylesheet\" href=\"tailwind.css\">\n");
|
style_str.push_str("<link rel=\"stylesheet\" href=\"tailwind.css\">\n");
|
||||||
}
|
}
|
||||||
|
let manifest = config.asset_manifest();
|
||||||
|
for package in manifest.packages() {
|
||||||
|
for asset in package.assets(){
|
||||||
|
if let assets_cli_support::AssetType::File(file) = asset {
|
||||||
|
match file.options(){
|
||||||
|
assets_cli_support::FileOptions::Css(_) => {
|
||||||
|
let asset_path = file.served_location();
|
||||||
|
style_str.push_str(&format!("<link rel=\"stylesheet\" href=\"{asset_path}\">\n"))
|
||||||
|
}
|
||||||
|
assets_cli_support::FileOptions::Image(image_options) => {
|
||||||
|
if image_options.preload(){
|
||||||
|
let asset_path = file.served_location();
|
||||||
|
style_str.push_str(&format!("<link rel=\"preload\" as=\"image\" href=\"{asset_path}\">\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
replace_or_insert_before("{style_include}", &style_str, "</head", &mut html);
|
replace_or_insert_before("{style_include}", &style_str, "</head", &mut html);
|
||||||
|
|
||||||
|
@ -491,11 +513,11 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let base_path = match &config.web.app.base_path {
|
let base_path = match &config.dioxus_config.web.app.base_path {
|
||||||
Some(path) => path,
|
Some(path) => path,
|
||||||
None => ".",
|
None => ".",
|
||||||
};
|
};
|
||||||
let app_name = &config.application.name;
|
let app_name = &config.dioxus_config.application.name;
|
||||||
// Check if a script already exists
|
// Check if a script already exists
|
||||||
if html.contains("{app_name}") && html.contains("{base_path}") {
|
if html.contains("{app_name}") && html.contains("{base_path}") {
|
||||||
html = html.replace("{app_name}", app_name);
|
html = html.replace("{app_name}", app_name);
|
||||||
|
@ -520,7 +542,7 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
let title = config
|
let title = config
|
||||||
.web
|
.dioxus_config.web
|
||||||
.app
|
.app
|
||||||
.title
|
.title
|
||||||
.clone()
|
.clone()
|
||||||
|
@ -687,9 +709,33 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
|
||||||
}
|
}
|
||||||
// SASS END
|
// SASS END
|
||||||
|
|
||||||
|
// Set up the collect asset config
|
||||||
|
let config = assets_cli_support::Config::default().with_assets_serve_location("/");
|
||||||
|
config.save();
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Process any assets collected from the binary
|
||||||
|
fn process_assets(config: &CrateConfig) -> anyhow::Result<()> {
|
||||||
|
let manifest = config.asset_manifest();
|
||||||
|
|
||||||
|
let static_asset_output_dir = PathBuf::from(
|
||||||
|
config
|
||||||
|
.dioxus_config
|
||||||
|
.web
|
||||||
|
.app
|
||||||
|
.base_path
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_default(),
|
||||||
|
);
|
||||||
|
let static_asset_output_dir = config.out_dir.join(static_asset_output_dir);
|
||||||
|
|
||||||
|
manifest.copy_static_assets_to(&static_asset_output_dir)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// use binary_install::{Cache, Download};
|
// use binary_install::{Cache, Download};
|
||||||
|
|
||||||
// /// Attempts to find `wasm-opt` in `PATH` locally, or failing that downloads a
|
// /// Attempts to find `wasm-opt` in `PATH` locally, or failing that downloads a
|
||||||
|
|
|
@ -49,7 +49,7 @@ impl Build {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let temp = gen_page(&crate_config.dioxus_config, false);
|
let temp = gen_page(&crate_config, false);
|
||||||
|
|
||||||
let mut file = std::fs::File::create(
|
let mut file = std::fs::File::create(
|
||||||
crate_config
|
crate_config
|
||||||
|
|
|
@ -56,7 +56,7 @@ impl Serve {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn regen_dev_page(crate_config: &CrateConfig) -> Result<()> {
|
pub fn regen_dev_page(crate_config: &CrateConfig) -> Result<()> {
|
||||||
let serve_html = gen_page(&crate_config.dioxus_config, true);
|
let serve_html = gen_page(&crate_config, true);
|
||||||
|
|
||||||
let dist_path = crate_config.crate_dir.join(
|
let dist_path = crate_config.crate_dir.join(
|
||||||
crate_config
|
crate_config
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::{cfg::Platform, error::Result};
|
use assets_cli_support::AssetManifest;use crate::{cfg::Platform, error::Result};
|
||||||
|
use assets_cli_support::AssetManifestExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
|
@ -297,6 +298,13 @@ impl CrateConfig {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn asset_manifest(&self) -> AssetManifest {
|
||||||
|
AssetManifest::load_from_path(
|
||||||
|
self.crate_dir.join("cargo.toml"),
|
||||||
|
self.workspace_dir.join("cargo.lock"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn as_example(&mut self, example_name: String) -> &mut Self {
|
pub fn as_example(&mut self, example_name: String) -> &mut Self {
|
||||||
self.executable = ExecutableType::Example(example_name);
|
self.executable = ExecutableType::Example(example_name);
|
||||||
self
|
self
|
||||||
|
|
Loading…
Reference in a new issue