From 0a999d24af71034804cd02dfae67e9fdc289e28d Mon Sep 17 00:00:00 2001 From: "Grant J. Butler" Date: Mon, 28 Oct 2024 00:48:08 -0400 Subject: [PATCH] Add workaround for Leaf bug. See https://github.com/vapor/leaf/pull/235 for details. --- Sources/SwiftVite/ViteTag.swift | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftVite/ViteTag.swift b/Sources/SwiftVite/ViteTag.swift index 912b681..e55d77d 100644 --- a/Sources/SwiftVite/ViteTag.swift +++ b/Sources/SwiftVite/ViteTag.swift @@ -1,5 +1,6 @@ #if canImport(Leaf) import Leaf +import Vapor enum ViteTagError: Error { case invalidResourceParameter @@ -37,12 +38,30 @@ struct ViteTag: UnsafeUnescapedLeafTag { } private extension LeafContext { + // This is to work around this bug in Leaf: + // https://github.com/vapor/leaf/pull/235 + private var _application: Application? { + guard let value = userInfo["application"] else { + return nil + } + + if let app = value as? Application { + return app + } + + if let leaf = value as? Application.Leaf { + return leaf.application + } + + return nil + } + var viteManifest: ViteManifest { - return application?.vite.withManifest({ $0 }) ?? [:] + return _application?.vite.withManifest({ $0 }) ?? [:] } var viteEnvironment: Vite.Environment { - return application?.environment == .production + return _application?.environment == .production ? .production : .development }