Guide / 15 — Ecosystem

Creating a Stew Library

Thanks to its native integration with the Go ecosystem, creating and publishing third-party libraries (UI, JS Wrappers, Utilities) is simple and doesn't require an additional package manager.

1. The Foundation: A Standard Go Module

Since Stew transpiles to Go code, a Stew library is, first and foremost, a classic Go module.

No need to invent a new publication system! Initialize a module, code, push to GitHub, and publish version tags just like any other Go library.
mkdir stew-awesome-ui && cd stew-awesome-ui
go mod init github.com/your_name/stew-awesome-ui

2. Creating Reusable Components

Create your .stew files as you normally would within your module's structure. For example, components/Button.stew:

<button class="bg-blue-500 rounded p-2">
    <slot />
</button>

Users will then import this component using its absolute path in their own projects:

<goscript>
    import "github.com/your_name/stew-awesome-ui/components/Button.stew"
</goscript>

<Button>My third-party button</Button>

How it works: The host project's Stew compiler automatically resolves these imports. If the module is a dependency (listed in go.mod), the compiler will read the .stew files directly from your GOMODCACHE!

3. Asynchronous JS Library Wrapping

If your component wraps an existing JavaScript library (Chart.js, Stripe, etc.), the best way to offer a Zero-Config experience is to load the CDN on the fly using stew/js.

<goscript client>
    import "stew/js"
    import "stew/data"

    func onMount() {
        // The script is loaded only if its URL is not already present in the document
        js.Load("https://cdn.jsdelivr.net/npm/chart.js", func() {
            // The following Wasm code will be executed AFTER the JS library is downloaded
            chart := js.Global("Chart").New(
                js.Global("document").Call("getElementById", data.Attr["id"]),
                // ... JSON configuration ...
            )
        })
    }
</goscript>

4. Additional Static Files

If your library requires images or specific embedded CSS, place them in a static/ folder at the root of your Go module.

The stew install command automatically scans your project's Go dependencies. If they contain a static/ folder, it will be fully copied into static/vendor/github.com/.../ in the user's project.

5. Mandatory Step Before Publishing

Stew is a Go-compiled environment. For users (and the go build command) to use your library, Stew must generate the .go files corresponding to your components before you push to GitHub.

# Compiles .stew files into .go files
stew compile

Your generated .go files must be committed to your remote repository. This ensures the library is usable even by developers who don't have the stew binary installed in their CI/CD pipeline.