Hashless assets (#4312)
* add support for external assets * fix playwright tests * add more documentation to with_hash_suffix * restore external asset hashing tests * Update gtk workflow dependencies * fix doc test * multiple versions of libgtk * clear dx in playwright tests * fix fullstack caching test * try without apt cache * cache everything except libxdo-dev * revert ci changes * fix wasm asset options * fix merge conflict * recommend the used attribute instead * Move options to AssetOptionsBuilder * Don't deprecate non-canonical new asset options functions --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
1 parent
0bd376b873
commit
e74c838441
22 files changed
+569
-316
No files matched your search
@@ -66,37 +66,6 @@ impl BundledAsset {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
/// This should only be called from the macro
|
||||
/// Create a new asset but with a relative path
|
||||
///
|
||||
/// This method is deprecated and will be removed in a future release.
|
||||
#[deprecated(
|
||||
note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`"
|
||||
)]
|
||||
pub const fn new_relative(
|
||||
absolute_source_path: &'static str,
|
||||
bundled_path: &'static str,
|
||||
options: AssetOptions,
|
||||
) -> Self {
|
||||
Self::new(absolute_source_path, bundled_path, options)
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
/// This should only be called from the macro
|
||||
/// Create a new asset from const paths
|
||||
pub const fn new_from_const(
|
||||
absolute_source_path: ConstStr,
|
||||
bundled_path: ConstStr,
|
||||
options: AssetOptions,
|
||||
) -> Self {
|
||||
Self {
|
||||
absolute_source_path,
|
||||
bundled_path,
|
||||
options,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the bundled name of the asset. This identifier cannot be used to read the asset directly
|
||||
pub fn bundled_path(&self) -> &str {
|
||||
self.bundled_path.as_str()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::AssetOptions;
|
||||
use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
|
||||
use const_serialize::SerializeConst;
|
||||
|
||||
/// Options for a css asset
|
||||
@@ -21,35 +21,59 @@ pub struct CssAssetOptions {
|
||||
|
||||
impl Default for CssAssetOptions {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl CssAssetOptions {
|
||||
/// Create a new css asset using the builder
|
||||
pub const fn new() -> Self {
|
||||
pub const fn new() -> AssetOptionsBuilder<CssAssetOptions> {
|
||||
AssetOptions::css()
|
||||
}
|
||||
|
||||
/// Create a default css asset options
|
||||
pub const fn default() -> Self {
|
||||
Self {
|
||||
preload: false,
|
||||
minify: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the asset is preloaded
|
||||
pub const fn preloaded(&self) -> bool {
|
||||
self.preload
|
||||
}
|
||||
|
||||
/// Check if the asset is minified
|
||||
pub const fn minified(&self) -> bool {
|
||||
self.minify
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptions {
|
||||
/// Create a new css asset builder
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, CssAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css());
|
||||
/// ```
|
||||
pub const fn css() -> AssetOptionsBuilder<CssAssetOptions> {
|
||||
AssetOptionsBuilder::variant(CssAssetOptions::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptionsBuilder<CssAssetOptions> {
|
||||
/// Sets whether the css should be minified (default: true)
|
||||
///
|
||||
/// Minifying the css can make your site load faster by loading less data
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, CssAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_minify(false));
|
||||
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_minify(false));
|
||||
/// ```
|
||||
#[allow(unused)]
|
||||
pub const fn with_minify(self, minify: bool) -> Self {
|
||||
Self { minify, ..self }
|
||||
}
|
||||
|
||||
/// Check if the asset is minified
|
||||
pub const fn minified(&self) -> bool {
|
||||
self.minify
|
||||
pub const fn with_minify(mut self, minify: bool) -> Self {
|
||||
self.variant.minify = minify;
|
||||
self
|
||||
}
|
||||
|
||||
/// Make the asset preloaded
|
||||
@@ -58,20 +82,18 @@ impl CssAssetOptions {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, CssAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_preload(true));
|
||||
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_preload(true));
|
||||
/// ```
|
||||
#[allow(unused)]
|
||||
pub const fn with_preload(self, preload: bool) -> Self {
|
||||
Self { preload, ..self }
|
||||
}
|
||||
|
||||
/// Check if the asset is preloaded
|
||||
pub const fn preloaded(&self) -> bool {
|
||||
self.preload
|
||||
pub const fn with_preload(mut self, preload: bool) -> Self {
|
||||
self.variant.preload = preload;
|
||||
self
|
||||
}
|
||||
|
||||
/// Convert the options into options for a generic asset
|
||||
pub const fn into_asset_options(self) -> AssetOptions {
|
||||
AssetOptions::Css(self)
|
||||
AssetOptions {
|
||||
add_hash: true,
|
||||
variant: AssetVariant::Css(self.variant),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::AssetOptions;
|
||||
use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
|
||||
use const_serialize::SerializeConst;
|
||||
use std::collections::HashSet;
|
||||
|
||||
@@ -24,48 +24,70 @@ pub struct CssModuleAssetOptions {
|
||||
|
||||
impl Default for CssModuleAssetOptions {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl CssModuleAssetOptions {
|
||||
/// Create a new css asset using the builder
|
||||
pub const fn new() -> Self {
|
||||
pub const fn new() -> AssetOptionsBuilder<CssModuleAssetOptions> {
|
||||
AssetOptions::css_module()
|
||||
}
|
||||
|
||||
/// Create a default css module asset options
|
||||
pub const fn default() -> Self {
|
||||
Self {
|
||||
preload: false,
|
||||
minify: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets whether the css should be minified (default: true)
|
||||
///
|
||||
/// Minifying the css can make your site load faster by loading less data
|
||||
#[allow(unused)]
|
||||
pub const fn with_minify(self, minify: bool) -> Self {
|
||||
Self { minify, ..self }
|
||||
}
|
||||
|
||||
/// Check if the asset is minified
|
||||
pub const fn minified(&self) -> bool {
|
||||
self.minify
|
||||
}
|
||||
|
||||
/// Make the asset preloaded
|
||||
///
|
||||
/// Preloading css will make the image start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner
|
||||
#[allow(unused)]
|
||||
pub const fn with_preload(self, preload: bool) -> Self {
|
||||
Self { preload, ..self }
|
||||
}
|
||||
|
||||
/// Check if the asset is preloaded
|
||||
pub const fn preloaded(&self) -> bool {
|
||||
self.preload
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptions {
|
||||
/// Create a new css module asset builder
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, CssModuleAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css_module());
|
||||
/// ```
|
||||
pub const fn css_module() -> AssetOptionsBuilder<CssModuleAssetOptions> {
|
||||
AssetOptionsBuilder::variant(CssModuleAssetOptions::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptionsBuilder<CssModuleAssetOptions> {
|
||||
/// Sets whether the css should be minified (default: true)
|
||||
///
|
||||
/// Minifying the css can make your site load faster by loading less data
|
||||
pub const fn with_minify(mut self, minify: bool) -> Self {
|
||||
self.variant.minify = minify;
|
||||
self
|
||||
}
|
||||
|
||||
/// Make the asset preloaded
|
||||
///
|
||||
/// Preloading css will make the image start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner
|
||||
pub const fn with_preload(mut self, preload: bool) -> Self {
|
||||
self.variant.preload = preload;
|
||||
self
|
||||
}
|
||||
|
||||
/// Convert the options into options for a generic asset
|
||||
pub const fn into_asset_options(self) -> AssetOptions {
|
||||
AssetOptions::CssModule(self)
|
||||
AssetOptions {
|
||||
add_hash: self.add_hash,
|
||||
variant: AssetVariant::CssModule(self.variant),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use const_serialize::SerializeConst;
|
||||
|
||||
use crate::AssetOptions;
|
||||
use crate::{AssetOptions, AssetOptionsBuilder};
|
||||
|
||||
/// The builder for a folder asset.
|
||||
#[derive(
|
||||
@@ -19,18 +19,40 @@ pub struct FolderAssetOptions {}
|
||||
|
||||
impl Default for FolderAssetOptions {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl FolderAssetOptions {
|
||||
/// Create a new folder asset using the builder
|
||||
pub const fn new() -> Self {
|
||||
Self {}
|
||||
/// Create a new folder asset builder
|
||||
pub const fn new() -> AssetOptionsBuilder<FolderAssetOptions> {
|
||||
AssetOptions::folder()
|
||||
}
|
||||
|
||||
/// Create a default folder asset options
|
||||
pub const fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptions {
|
||||
/// Create a new folder asset builder
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, AssetOptions};
|
||||
/// const _: Asset = asset!("/assets", AssetOptions::folder());
|
||||
/// ```
|
||||
pub const fn folder() -> AssetOptionsBuilder<FolderAssetOptions> {
|
||||
AssetOptionsBuilder::variant(FolderAssetOptions::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptionsBuilder<FolderAssetOptions> {
|
||||
/// Convert the options into options for a generic asset
|
||||
pub const fn into_asset_options(self) -> AssetOptions {
|
||||
AssetOptions::Folder(self)
|
||||
AssetOptions {
|
||||
add_hash: false,
|
||||
variant: crate::AssetVariant::Folder(self.variant),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use const_serialize::SerializeConst;
|
||||
|
||||
use crate::AssetOptions;
|
||||
use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
|
||||
|
||||
/// The type of an image. You can read more about the tradeoffs between image formats [here](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types)
|
||||
#[derive(
|
||||
@@ -77,13 +77,18 @@ pub struct ImageAssetOptions {
|
||||
|
||||
impl Default for ImageAssetOptions {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl ImageAssetOptions {
|
||||
/// Create a new image asset options
|
||||
pub const fn new() -> Self {
|
||||
/// Create a new builder for image asset options
|
||||
pub const fn new() -> AssetOptionsBuilder<ImageAssetOptions> {
|
||||
AssetOptions::image()
|
||||
}
|
||||
|
||||
/// Create a default image asset options
|
||||
pub const fn default() -> Self {
|
||||
Self {
|
||||
ty: ImageFormat::Unknown,
|
||||
low_quality_preview: false,
|
||||
@@ -92,129 +97,21 @@ impl ImageAssetOptions {
|
||||
}
|
||||
}
|
||||
|
||||
/// Make the asset preloaded
|
||||
///
|
||||
/// Preloading an image will make the image start to load as soon as possible. This is useful for images that will be displayed soon after the page loads or images that may not be visible immediately, but should start loading sooner
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true));
|
||||
/// ```
|
||||
pub const fn with_preload(self, preload: bool) -> Self {
|
||||
Self { preload, ..self }
|
||||
}
|
||||
|
||||
/// Check if the asset is preloaded
|
||||
pub const fn preloaded(&self) -> bool {
|
||||
self.preload
|
||||
}
|
||||
|
||||
/// Sets the format of the image
|
||||
///
|
||||
/// Choosing the right format can make your site load much faster. Webp and avif images tend to be a good default for most images
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Webp));
|
||||
/// ```
|
||||
pub const fn with_format(self, format: ImageFormat) -> Self {
|
||||
Self { ty: format, ..self }
|
||||
}
|
||||
|
||||
/// Sets the format of the image to [`ImageFormat::Avif`]
|
||||
///
|
||||
/// Avif images tend to be a good default for most images rendered in browser because
|
||||
/// they compress images well
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_avif());
|
||||
/// ```
|
||||
pub const fn with_avif(self) -> Self {
|
||||
self.with_format(ImageFormat::Avif)
|
||||
}
|
||||
|
||||
/// Sets the format of the image to [`ImageFormat::Webp`]
|
||||
///
|
||||
/// Webp images tend to be a good default for most images rendered in browser because
|
||||
/// they compress images well
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_webp());
|
||||
/// ```
|
||||
pub const fn with_webp(self) -> Self {
|
||||
self.with_format(ImageFormat::Webp)
|
||||
}
|
||||
|
||||
/// Sets the format of the image to [`ImageFormat::Jpg`]
|
||||
///
|
||||
/// Jpeg images compress much better than [`ImageFormat::Png`], but worse than [`ImageFormat::Webp`] or [`ImageFormat::Avif`]
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_jpg());
|
||||
/// ```
|
||||
pub const fn with_jpg(self) -> Self {
|
||||
self.with_format(ImageFormat::Jpg)
|
||||
}
|
||||
|
||||
/// Sets the format of the image to [`ImageFormat::Png`]
|
||||
///
|
||||
/// Png images don't compress very well, so they are not recommended for large images
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_png());
|
||||
/// ```
|
||||
pub const fn with_png(self) -> Self {
|
||||
self.with_format(ImageFormat::Png)
|
||||
}
|
||||
|
||||
/// Get the format of the image
|
||||
pub const fn format(&self) -> ImageFormat {
|
||||
self.ty
|
||||
}
|
||||
|
||||
/// Sets the size of the image
|
||||
///
|
||||
/// If you only use the image in one place, you can set the size of the image to the size it will be displayed at. This will make the image load faster
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 512, height: 512 }));
|
||||
/// ```
|
||||
pub const fn with_size(self, size: ImageSize) -> Self {
|
||||
Self { size, ..self }
|
||||
}
|
||||
|
||||
/// Get the size of the image
|
||||
pub const fn size(&self) -> ImageSize {
|
||||
self.size
|
||||
}
|
||||
|
||||
// LQIP is currently disabled until we have the CLI set up to inject the low quality image preview after the crate is built through the linker
|
||||
// /// Make the image use a low quality preview
|
||||
// ///
|
||||
// /// A low quality preview is a small version of the image that will load faster. This is useful for large images on mobile devices that may take longer to load
|
||||
// ///
|
||||
// /// ```rust
|
||||
// /// # use manganis::{asset, Asset, ImageAssetOptions};
|
||||
// /// const _: Asset = manganis::asset!("/assets/image.png", ImageAssetOptions::new().with_low_quality_image_preview());
|
||||
// /// ```
|
||||
//
|
||||
// pub const fn with_low_quality_image_preview(self, low_quality_preview: bool) -> Self {
|
||||
// Self {
|
||||
// low_quality_preview,
|
||||
// ..self
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Convert the options into options for a generic asset
|
||||
pub const fn into_asset_options(self) -> AssetOptions {
|
||||
AssetOptions::Image(self)
|
||||
}
|
||||
|
||||
pub(crate) const fn extension(&self) -> Option<&'static str> {
|
||||
match self.ty {
|
||||
ImageFormat::Png => Some("png"),
|
||||
@@ -225,3 +122,114 @@ impl ImageAssetOptions {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptions {
|
||||
/// Create a new image asset builder
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image());
|
||||
/// ```
|
||||
pub const fn image() -> AssetOptionsBuilder<ImageAssetOptions> {
|
||||
AssetOptionsBuilder::variant(ImageAssetOptions::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptionsBuilder<ImageAssetOptions> {
|
||||
/// Make the asset preloaded
|
||||
///
|
||||
/// Preloading an image will make the image start to load as soon as possible. This is useful for images that will be displayed soon after the page loads or images that may not be visible immediately, but should start loading sooner
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_preload(true));
|
||||
/// ```
|
||||
pub const fn with_preload(mut self, preload: bool) -> Self {
|
||||
self.variant.preload = preload;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the format of the image
|
||||
///
|
||||
/// Choosing the right format can make your site load much faster. Webp and avif images tend to be a good default for most images
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_format(ImageFormat::Webp));
|
||||
/// ```
|
||||
pub const fn with_format(mut self, format: ImageFormat) -> Self {
|
||||
self.variant.ty = format;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the format of the image to [`ImageFormat::Avif`]
|
||||
///
|
||||
/// Avif images tend to be a good default for most images rendered in browser because
|
||||
/// they compress images well
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_avif());
|
||||
/// ```
|
||||
pub const fn with_avif(self) -> Self {
|
||||
self.with_format(ImageFormat::Avif)
|
||||
}
|
||||
|
||||
/// Sets the format of the image to [`ImageFormat::Webp`]
|
||||
///
|
||||
/// Webp images tend to be a good default for most images rendered in browser because
|
||||
/// they compress images well
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_webp());
|
||||
/// ```
|
||||
pub const fn with_webp(self) -> Self {
|
||||
self.with_format(ImageFormat::Webp)
|
||||
}
|
||||
|
||||
/// Sets the format of the image to [`ImageFormat::Jpg`]
|
||||
///
|
||||
/// Jpeg images compress much better than [`ImageFormat::Png`], but worse than [`ImageFormat::Webp`] or [`ImageFormat::Avif`]
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_jpg());
|
||||
/// ```
|
||||
pub const fn with_jpg(self) -> Self {
|
||||
self.with_format(ImageFormat::Jpg)
|
||||
}
|
||||
|
||||
/// Sets the format of the image to [`ImageFormat::Png`]
|
||||
///
|
||||
/// Png images don't compress very well, so they are not recommended for large images
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_png());
|
||||
/// ```
|
||||
pub const fn with_png(self) -> Self {
|
||||
self.with_format(ImageFormat::Png)
|
||||
}
|
||||
|
||||
/// Sets the size of the image
|
||||
///
|
||||
/// If you only use the image in one place, you can set the size of the image to the size it will be displayed at. This will make the image load faster
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize};
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_size(ImageSize::Manual { width: 512, height: 512 }));
|
||||
/// ```
|
||||
pub const fn with_size(mut self, size: ImageSize) -> Self {
|
||||
self.variant.size = size;
|
||||
self
|
||||
}
|
||||
|
||||
/// Convert the options into options for a generic asset
|
||||
pub const fn into_asset_options(self) -> AssetOptions {
|
||||
AssetOptions {
|
||||
add_hash: self.add_hash,
|
||||
variant: AssetVariant::Image(self.variant),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use const_serialize::SerializeConst;
|
||||
|
||||
use crate::AssetOptions;
|
||||
use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
|
||||
|
||||
/// Options for a javascript asset
|
||||
#[derive(
|
||||
@@ -22,35 +22,60 @@ pub struct JsAssetOptions {
|
||||
|
||||
impl Default for JsAssetOptions {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl JsAssetOptions {
|
||||
/// Create a new js asset builder
|
||||
pub const fn new() -> Self {
|
||||
/// Create a new js asset options builder
|
||||
pub const fn new() -> AssetOptionsBuilder<JsAssetOptions> {
|
||||
AssetOptions::js()
|
||||
}
|
||||
|
||||
/// Create a default js asset options
|
||||
pub const fn default() -> Self {
|
||||
Self {
|
||||
minify: true,
|
||||
preload: false,
|
||||
minify: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the asset is preloaded
|
||||
pub const fn preloaded(&self) -> bool {
|
||||
self.preload
|
||||
}
|
||||
|
||||
/// Check if the asset is minified
|
||||
pub const fn minified(&self) -> bool {
|
||||
self.minify
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptions {
|
||||
/// Create a new js asset builder
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, JsAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/script.js", AssetOptions::js());
|
||||
/// ```
|
||||
pub const fn js() -> AssetOptionsBuilder<JsAssetOptions> {
|
||||
AssetOptionsBuilder::variant(JsAssetOptions::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptionsBuilder<JsAssetOptions> {
|
||||
/// Sets whether the js should be minified (default: true)
|
||||
///
|
||||
/// Minifying the js can make your site load faster by loading less data
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, JsAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_minify(false));
|
||||
/// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_minify(false));
|
||||
/// ```
|
||||
#[allow(unused)]
|
||||
pub const fn with_minify(self, minify: bool) -> Self {
|
||||
Self { minify, ..self }
|
||||
}
|
||||
|
||||
/// Check if the asset is minified
|
||||
pub const fn minified(&self) -> bool {
|
||||
self.minify
|
||||
pub const fn with_minify(mut self, minify: bool) -> Self {
|
||||
self.variant.minify = minify;
|
||||
self
|
||||
}
|
||||
|
||||
/// Make the asset preloaded
|
||||
@@ -59,20 +84,19 @@ impl JsAssetOptions {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, JsAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_preload(true));
|
||||
/// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_preload(true));
|
||||
/// ```
|
||||
#[allow(unused)]
|
||||
pub const fn with_preload(self, preload: bool) -> Self {
|
||||
Self { preload, ..self }
|
||||
pub const fn with_preload(mut self, preload: bool) -> Self {
|
||||
self.variant.preload = preload;
|
||||
self
|
||||
}
|
||||
|
||||
/// Check if the asset is preloaded
|
||||
pub const fn preloaded(&self) -> bool {
|
||||
self.preload
|
||||
}
|
||||
|
||||
/// Convert the options into options for a generic asset
|
||||
/// Convert the builder into asset options with the given variant
|
||||
pub const fn into_asset_options(self) -> AssetOptions {
|
||||
AssetOptions::Js(self)
|
||||
AssetOptions {
|
||||
add_hash: self.add_hash,
|
||||
variant: AssetVariant::Js(self.variant),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,148 @@ use crate::{
|
||||
serde::Serialize,
|
||||
serde::Deserialize,
|
||||
)]
|
||||
#[non_exhaustive]
|
||||
pub struct AssetOptions {
|
||||
/// If a hash should be added to the asset path
|
||||
pub(crate) add_hash: bool,
|
||||
/// The variant of the asset
|
||||
pub(crate) variant: AssetVariant,
|
||||
}
|
||||
|
||||
impl AssetOptions {
|
||||
/// Create a new asset options builder
|
||||
pub const fn builder() -> AssetOptionsBuilder<()> {
|
||||
AssetOptionsBuilder::new()
|
||||
}
|
||||
|
||||
/// Get the variant of the asset
|
||||
pub const fn variant(&self) -> &AssetVariant {
|
||||
&self.variant
|
||||
}
|
||||
|
||||
/// Check if a hash should be added to the asset path
|
||||
pub const fn hash_suffix(&self) -> bool {
|
||||
self.add_hash
|
||||
}
|
||||
|
||||
/// Try to get the extension for the asset. If the asset options don't define an extension, this will return None
|
||||
pub const fn extension(&self) -> Option<&'static str> {
|
||||
match self.variant {
|
||||
AssetVariant::Image(image) => image.extension(),
|
||||
AssetVariant::Css(_) => Some("css"),
|
||||
AssetVariant::CssModule(_) => Some("css"),
|
||||
AssetVariant::Js(_) => Some("js"),
|
||||
AssetVariant::Folder(_) => None,
|
||||
AssetVariant::Unknown => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert the options into options for a generic asset
|
||||
pub const fn into_asset_options(self) -> AssetOptions {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// A builder for [`AssetOptions`]
|
||||
///
|
||||
/// ```rust
|
||||
/// # use manganis::AssetOptionsBuilder;
|
||||
/// static ASSET: Asset = asset!(
|
||||
/// "image.png",
|
||||
/// AssetOptionsBuilder::new()
|
||||
/// .with_hash_suffix(false)
|
||||
/// );
|
||||
/// ```
|
||||
pub struct AssetOptionsBuilder<T> {
|
||||
/// If a hash should be added to the asset path
|
||||
pub(crate) add_hash: bool,
|
||||
/// The variant of the asset
|
||||
pub(crate) variant: T,
|
||||
}
|
||||
|
||||
impl Default for AssetOptionsBuilder<()> {
|
||||
fn default() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetOptionsBuilder<()> {
|
||||
/// Create a new asset options builder with an unknown variant
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
add_hash: true,
|
||||
variant: (),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a default asset options builder
|
||||
pub const fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
|
||||
/// Convert the builder into asset options with the given variant
|
||||
pub const fn into_asset_options(self) -> AssetOptions {
|
||||
AssetOptions {
|
||||
add_hash: self.add_hash,
|
||||
variant: AssetVariant::Unknown,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AssetOptionsBuilder<T> {
|
||||
/// Create a new asset options builder with the given variant
|
||||
pub(crate) const fn variant(variant: T) -> Self {
|
||||
Self {
|
||||
add_hash: true,
|
||||
variant,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set whether a hash should be added to the asset path. Manganis adds hashes to asset paths by default
|
||||
/// for [cache busting](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#cache_busting).
|
||||
/// With hashed assets, you can serve the asset with a long expiration time, and when the asset changes,
|
||||
/// the hash in the path will change, causing the browser to fetch the new version.
|
||||
///
|
||||
/// This method will only effect if the hash is added to the bundled asset path. If you are using the asset
|
||||
/// macro, the asset struct still needs to be used in your rust code to ensure the asset is included in the binary.
|
||||
///
|
||||
/// <div class="warning">
|
||||
///
|
||||
/// If you are using an asset outside of rust code where you know what the asset hash will be, you must use the
|
||||
/// `#[used]` attribute to ensure the asset is included in the binary even if it is not referenced in the code.
|
||||
///
|
||||
/// ```rust
|
||||
/// #[used]
|
||||
/// static ASSET: manganis::Asset = manganis::asset!(
|
||||
/// "path/to/asset.png",
|
||||
/// AssetVariant::Unknown.into_asset_options()
|
||||
/// .with_hash_suffix(false)
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// </div>
|
||||
pub const fn with_hash_suffix(mut self, add_hash: bool) -> Self {
|
||||
self.add_hash = add_hash;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings for a specific type of asset
|
||||
#[derive(
|
||||
Debug,
|
||||
Eq,
|
||||
PartialEq,
|
||||
PartialOrd,
|
||||
Clone,
|
||||
Copy,
|
||||
Hash,
|
||||
SerializeConst,
|
||||
serde::Serialize,
|
||||
serde::Deserialize,
|
||||
)]
|
||||
#[repr(C, u8)]
|
||||
#[non_exhaustive]
|
||||
pub enum AssetOptions {
|
||||
pub enum AssetVariant {
|
||||
/// An image asset
|
||||
Image(ImageAssetOptions),
|
||||
/// A folder asset
|
||||
@@ -33,22 +172,3 @@ pub enum AssetOptions {
|
||||
/// An unknown asset
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl AssetOptions {
|
||||
/// Try to get the extension for the asset. If the asset options don't define an extension, this will return None
|
||||
pub const fn extension(&self) -> Option<&'static str> {
|
||||
match self {
|
||||
AssetOptions::Image(image) => image.extension(),
|
||||
AssetOptions::Css(_) => Some("css"),
|
||||
AssetOptions::CssModule(_) => Some("css"),
|
||||
AssetOptions::Js(_) => Some("js"),
|
||||
AssetOptions::Folder(_) => None,
|
||||
AssetOptions::Unknown => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert the options into options for a generic asset
|
||||
pub const fn into_asset_options(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ impl Parse for AssetParser {
|
||||
// ```
|
||||
// asset!(
|
||||
// "/assets/myfile.png",
|
||||
// ImageAssetOptions::new()
|
||||
// AssetOptions::image()
|
||||
// .format(ImageFormat::Jpg)
|
||||
// .size(512, 512)
|
||||
// )
|
||||
@@ -84,8 +84,9 @@ impl ToTokens for AssetParser {
|
||||
asset_string.hash(&mut hash);
|
||||
let asset_hash = format!("{:016x}", hash.finish());
|
||||
|
||||
// Generate the link section for the asset
|
||||
// The link section includes the source path and the output path of the asset
|
||||
// Generate the link section for the asset. The link section includes the source path and the
|
||||
// output path of the asset. We force the asset to be included in the binary even if it is unused
|
||||
// if the asset is unhashed
|
||||
let link_section = crate::generate_link_section(quote!(__ASSET), &asset_hash);
|
||||
|
||||
// generate the asset::new method to deprecate the `./assets/blah.css` syntax
|
||||
@@ -96,7 +97,7 @@ impl ToTokens for AssetParser {
|
||||
};
|
||||
|
||||
let options = if self.options.is_empty() {
|
||||
quote! { manganis::AssetOptions::Unknown }
|
||||
quote! { manganis::AssetOptions::builder() }
|
||||
} else {
|
||||
self.options.clone()
|
||||
};
|
||||
|
||||
@@ -34,7 +34,7 @@ impl Parse for CssModuleParser {
|
||||
// Optional options
|
||||
let mut options = input.parse::<TokenStream>()?;
|
||||
if options.is_empty() {
|
||||
options = quote! { manganis::CssModuleAssetOptions::new() }
|
||||
options = quote! { manganis::AssetOptions::css_module() }
|
||||
}
|
||||
|
||||
let asset_parser = AssetParser {
|
||||
|
||||
@@ -46,17 +46,17 @@ use linker::generate_link_section;
|
||||
/// Resize the image at compile time to make the assets file size smaller:
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 52, height: 52 }));
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_size(ImageSize::Manual { width: 52, height: 52 }));
|
||||
/// ```
|
||||
/// Or convert the image at compile time to a web friendly format:
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize, ImageFormat};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Avif));
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_format(ImageFormat::Avif));
|
||||
/// ```
|
||||
/// You can mark images as preloaded to make them load faster in your app
|
||||
/// ```rust
|
||||
/// # use manganis::{asset, Asset, ImageAssetOptions};
|
||||
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true));
|
||||
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_preload(true));
|
||||
/// ```
|
||||
#[proc_macro]
|
||||
pub fn asset(input: TokenStream) -> TokenStream {
|
||||
@@ -78,7 +78,7 @@ pub fn asset(input: TokenStream) -> TokenStream {
|
||||
/// - An optional `CssModuleAssetOptions` struct to configure the processing of your CSS module.
|
||||
///
|
||||
/// ```rust
|
||||
/// css_module!(StylesIdent = "/my.module.css", CssModuleAssetOptions::new());
|
||||
/// css_module!(StylesIdent = "/my.module.css", AssetOptions::css_module());
|
||||
/// ```
|
||||
///
|
||||
/// The styles struct can be made public by appending `pub` before the identifier.
|
||||
@@ -131,7 +131,7 @@ pub fn asset(input: TokenStream) -> TokenStream {
|
||||
/// use manganis::CssModuleAssetOptions;
|
||||
///
|
||||
/// css_module!(Styles = "/mycss.module.css",
|
||||
/// CssModuleAssetOptions::new()
|
||||
/// AssetOptions::css_module()
|
||||
/// .with_minify(true)
|
||||
/// .with_preload(false),
|
||||
/// );
|
||||
|
||||
@@ -21,9 +21,9 @@ pub const PNG_ASSET: Asset =
|
||||
asset!("/assets/image.png");
|
||||
// Resize the image at compile time to make the assets smaller
|
||||
pub const RESIZED_PNG_ASSET: Asset =
|
||||
asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 52, height: 52 }));
|
||||
asset!("/assets/image.png", AssetOptions::image().with_size(ImageSize::Manual { width: 52, height: 52 }));
|
||||
// Or convert the image at compile time to a web friendly format
|
||||
pub const AVIF_ASSET: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Avif));
|
||||
pub const AVIF_ASSET: Asset = asset!("/assets/image.png", AssetOptions::image().with_format(ImageFormat::Avif));
|
||||
```
|
||||
|
||||
## Adding Support to Your CLI
|
||||
|
||||
@@ -9,6 +9,6 @@ pub use manganis_macro::asset;
|
||||
pub use manganis_macro::css_module;
|
||||
|
||||
pub use manganis_core::{
|
||||
Asset, AssetOptions, BundledAsset, CssAssetOptions, CssModuleAssetOptions, FolderAssetOptions,
|
||||
ImageAssetOptions, ImageFormat, ImageSize, JsAssetOptions,
|
||||
Asset, AssetOptions, AssetVariant, BundledAsset, CssAssetOptions, CssModuleAssetOptions,
|
||||
FolderAssetOptions, ImageAssetOptions, ImageFormat, ImageSize, JsAssetOptions,
|
||||
};
|
||||
@@ -1,13 +1,12 @@
|
||||
pub use const_serialize;
|
||||
use const_serialize::{serialize_const, ConstStr, ConstVec, SerializeConst};
|
||||
use const_serialize::{serialize_const, ConstVec, SerializeConst};
|
||||
use manganis_core::{AssetOptions, BundledAsset};
|
||||
|
||||
const PLACEHOLDER_HASH: ConstStr =
|
||||
ConstStr::new("this is a placeholder path which will be replaced by the linker");
|
||||
const PLACEHOLDER_HASH: &str = "this is a placeholder path which will be replaced by the linker";
|
||||
|
||||
/// Create a bundled asset from the input path, the content hash, and the asset options
|
||||
pub const fn create_bundled_asset(input_path: &str, asset_config: AssetOptions) -> BundledAsset {
|
||||
BundledAsset::new_from_const(ConstStr::new(input_path), PLACEHOLDER_HASH, asset_config)
|
||||
BundledAsset::new(input_path, PLACEHOLDER_HASH, asset_config)
|
||||
}
|
||||
|
||||
/// Create a bundled asset from the input path, the content hash, and the asset options with a relative asset deprecation warning
|
||||
|
||||
Reference in new issue
Block a user