# Alpine.js

`@hamur/alpine` starts [Alpine.js](https://alpinejs.dev/) in an html app. Your pages keep writing `x-data`, `x-on:click` and `x-text` in their markup, and Alpine makes them work after the page loads and after every client navigation. It is a module, not an adapter: it renders nothing, and it works with the html adapter only.

```sh
bun add @hamur/alpine alpinejs
```

````ts [alpine/config.ts]
import { defineConfig } from "hamur"
import alpine from "@hamur/alpine"

export default defineConfig({
  framework: "html",
  modules: [alpine],
  alpine: { entrypoint: "./app/alpine.ts" },
})
````

Add the module to `modules`; its option lives under the `alpine` key. Another adapter fails the build with `Module "@hamur/alpine" supports html; this app uses react`.

## A component in a page

````ts [alpine/html/counter.ts]
import { Link } from "@hamur/adapter-html"

export default function Counter(): string {
  return (
    '<button type="button" x-data="{ n: 0 }" x-on:click="n++" x-text="\'Count \' + n">Count 0</button>' +
    Link({ to: "/other", children: "Other" })
  )
}
````

The server renders the markup, so the button reads `Count 0` before any script runs. After hydration the module calls `Alpine.start()` once, and the counter counts.

## Navigations

`Alpine.start()` watches the document with a `MutationObserver`. When the html adapter swaps the page, Alpine cleans up the components of the page that left and starts those of the page that arrived, so the module calls neither `initTree` nor `destroyTree`. A back navigation renders the page's HTML again, so its `x-data` starts from its initial value like a first visit. State that must survive a navigation goes in `Alpine.store`, which lives as long as the document.

A [script-free page](/islands-and-streaming) has no client code, so it gets no Alpine and its `x-` attributes stay inert. An island page works like any other: Alpine sees the island's DOM.

## Plugins and stores: `alpine.entrypoint`

`entrypoint` is a file from the app root. Its default export is called with `Alpine` before `Alpine.start()`, and Hamur waits for it when it returns a promise. Use it for `Alpine.plugin`, `Alpine.store`, `Alpine.magic` and `Alpine.directive`, so they exist before the first component runs. `window.Alpine` is set after it returns and before `start()`.

````ts [alpine/entrypoint.ts]
import type { Alpine } from "alpinejs"

export default (Alpine: Alpine) => {
  Alpine.store("theme", { dark: false })
  Alpine.magic("greeting", () => "Hello")
}
````

A path that does not exist fails the build with `alpine.entrypoint "./app/alpine.ts" does not exist`. The reference is [`@hamur/alpine`](/api-reference/alpine).
