Added an optional per-asset folder to the Asset protocol and the AssetExtension extension in the Infrastructure package.

This commit is contained in:
2026-08-15 23:44:06 +02:00
parent 7d53be539e
commit 091c0f0686
5 changed files with 32 additions and 12 deletions
@@ -40,8 +40,8 @@ public extension AssetExtension {
}
}
/// The sub-directory within the static root that holds files with this extension, if any.
var subdirectory: String? {
/// The folder within the static root that holds files with this extension, if any.
var folder: String? {
switch self {
case .css: "css"
case .js: "js"
@@ -1,11 +1,15 @@
/// An asset shipped with a website: a file stored under the static files root and served by Hummingbird's `FileMiddleware` middleware.
///
/// A conforming asset supplies its file name and the extensions it is available with, each resolving to its own file; the protocol derives the paths from them:
/// the file's path within the static files root and the URL path it is served at, optionally versioned to bust caches.
/// the file's path within the static files root and the URL path it is served at, optionally versioned to bust caches. Each file lands in its extension's own
/// folder unless the asset names a ``folder`` of its own.
public protocol Asset: Sendable {
// MARK: Properties
/// The folder within the static files root that holds the asset's files, or `nil` (the default) to use each extension's own folder.
var folder: String? { get }
/// The file extensions the asset is available with.
var fileExtensions: [AssetExtension] { get }
@@ -18,6 +22,11 @@ public protocol Asset: Sendable {
public extension Asset {
// MARK: Computed
/// The asset's files live in each extension's own folder by default.
var folder: String? { nil }
// MARK: Methods
/// Resolves the asset's path against the given base directory.
@@ -50,7 +59,7 @@ public extension Asset {
) -> String {
let file = "\(fileName).\(fileExtension.rawValue)"
return fileExtension.subdirectory
return (folder ?? fileExtension.folder)
.map { "\($0)/\(file)" } ?? file
}