# How can I use a Swift package build plugin to process a custom resource?

**URL:** <https://forums.swift.org/t/how-can-i-use-a-swift-package-build-plugin-to-process-a-custom-resource/64091>\
**Category:** Package Manager\
**Tags:** resources, packagemanager, plugins, buildtoolplugin\
**Created:** [March 30, 2023, 10:06pm UTC](https://forums.swift.org/t/how-can-i-use-a-swift-package-build-plugin-to-process-a-custom-resource/64091 "2023-03-30T22:06:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacecrafter3d](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spacecrafter3d/32/20929_2.png) [@spacecrafter3d](https://forums.swift.org/u/spacecrafter3d)\
**Post date:** [March 30, 2023, 10:06pm UTC](https://forums.swift.org/t/how-can-i-use-a-swift-package-build-plugin-to-process-a-custom-resource/64091/1 "2023-03-30T22:06:59Z")

</div>

I am using [Vapor](https://vapor.codes) 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:

```swift
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

```plaintext
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?

---

<div class="post-metadata">

**Author:** ![PALKOVNIK](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/palkovnik/32/30945_2.png) [@PALKOVNIK](https://forums.swift.org/u/PALKOVNIK)\
**Post date:** [July 1, 2023, 5:57am UTC](https://forums.swift.org/t/how-can-i-use-a-swift-package-build-plugin-to-process-a-custom-resource/64091/2 "2023-07-01T05:57:50Z")

</div>

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](https://github.com/apple/swift-package-manager/commit/0147f7122a2c66eef55dcf17a0e4812320d5c7e6)

---

<div class="post-metadata">

**Author:** ![NeoNacho](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/neonacho/32/1368_2.png) [@NeoNacho](https://forums.swift.org/u/NeoNacho)\
**Post date:** [July 3, 2023, 5:12pm UTC](https://forums.swift.org/t/how-can-i-use-a-swift-package-build-plugin-to-process-a-custom-resource/64091/3 "2023-07-03T17:12:13Z")

</div>

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.
