Run SwiftUI preview from SPM Package

What you need is to add in your swiftpackage declaration a platform target such as iOS see example below (with iOS(.v13)):

let package = Package(
    name: "NAME",
    platforms: [.iOS(.v13)],
    products: [
        // Products 
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets
        // Test Targets
    ]
)

And now, make sure you use the same target for the preview. You can add more of course.

enter image description here


Xcode 12

With Xcode 12 the SwiftUI Preview just works 'as-should' in standalone Package

demo

Xcode 11+

Is it possible to run SwiftUI preview somehow directly from SPM Package without building the main target?

No, at least till now (Xcode 11.4beta3). Preview is a variant of Simulator and it needs UI executable to setup full-functional run-time context for your view preview.

Solution (from practice): setup SwiftUI executable target that in parallel contains all files from package (or package itself, depends) and perform all SwiftUI development it it, but package itself build during continuous integration process (including unit-testing).