I am using Vapor and its FileMiddleware to host CSS files. It automatically hosts all files in my packages Public directory. I want to pre-process my CSS files using Tailwind CSS, so I am trying to create a build tool plugin to do so.
I've implemented my plugin like this:
import PackagePlugin
@main
struct TailwindCSSBuild: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
guard let target = target as? SourceModuleTarget else { return [] }
let inputFiles = target.sourceFiles.filter({ $0.path.extension == "css" })
return try inputFiles.map { inputFile in
let inputPath = inputFile.path
let outputName = inputPath.stem + ".css"
let outputPath = context.pluginWorkDirectory.appending(outputName)
return .buildCommand(
displayName: "Generating \(outputName) from \(inputPath.lastComponent)",
executable: try context.tool(named: "tailwindcss").path,
arguments: [
"-i", "\(inputPath)",
"-o", "\(outputPath)",
"-c", "\(context.package.directory.appending("tailwind.config.js"))"
],
inputFiles: [inputPath],
outputFiles: [outputPath]
)
}
}
}
Whenever I call swift run with my app package that uses the plugin, I get
error: unexpected input file: <path to my app package>/.build/plugins/outputs/app/App/TailwindCSSBuild/index.css
Is there a way to do custom processing of a resource like this without throwing this error?
PALKOVNIK
(Serhii Mumriak)
2
AFAIK there's no way to do it right now. In original proposal for swiftpm plugins there were mentions of such possibility in future, but it never materialized.
I'd like to have this feature too - in my case to compile shader files into binaries which will be shipped as resources
UPD. actually, looks like it's possible! it was not mentioned in changelog, but according to this commit it should work: Handle generated resources from plugins (#4306) · apple/swift-package-manager@0147f71 · GitHub
NeoNacho
(Boris Buegling)
3
This should work since Swift 5.7 or 5.8, I believe. Which version of Swift/Xcode are you using and what tools-version is your package using? IIRC, if your package uses older tools-versions, we preserve the behavior of not supporting resource generation which could produce a failure like this.