# `@hamur/vite-compat`

Runs the Rollup-hook part of Vite plugins in Hamur's build and `hamur dev` (guide: [Vite plugins](/vite-plugins)). The package has one entry and no dependency; it does not import `vite`, `rollup` or `rolldown`, and its Vite types are copied by hand.

```ts
// hamur.config.ts
import { defineConfig } from "hamur"
import { fromVite } from "@hamur/vite-compat"
import svgr from "vite-plugin-svgr"

export default defineConfig({ modules: [fromVite(svgr())] })
```

### `fromVite`

`fromVite(plugin: VitePluginOption): HamurModule`. The module is named `vite:<first plugin's name>`, or has no `meta.name` when a promise comes first. Its `setup` awaits and flattens the plugins, drops the ones whose `apply` does not match the command, orders them by `enforce`, runs every `config` and then every `configResolved`, logs each hook it does not run, and maps the rest:

| Hook                      | Becomes                                                                                                                                                                                                                                                                       |
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `resolveId`, `load`       | one Bun plugin `vite:<name>` (`addBunPlugin`): `onResolve` for every `resolveId` in order, `onLoad` in namespace `vite` for every `load`, and in the file namespace for a `load` with a hook filter. A resolved `\0x` is path `x` of namespace `vite`; `load` gets `\0x` back |
| `transform`               | `addTransform({ name: "vite:<name>", filter, enforce })`; a non-`pre` plugin is given the module transpiled with `Bun.Transpiler` and its result is `js`                                                                                                                      |
| `buildStart`              | `build:before`; in `hamur dev` at the end of `setup`                                                                                                                                                                                                                          |
| `buildEnd`, `closeBundle` | `build:done` and `close`; in `hamur dev` both at `close`                                                                                                                                                                                                                      |

Errors and warnings:

| When                                                                                                                            | Message                                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| a hook of [What does not run](/vite-plugins#what-does-not-run) is defined                                                       | `[hamur] vite plugin "<name>": <hook> is not run by @hamur/vite-compat`, once per plugin and hook                                                       |
| `config()` returns a key other than `define`, `resolve.alias` included                                                          | `[hamur] vite plugin "<name>": config() returned <key>, which Hamur does not read` (`resolve.<key>` for a key under `resolve`)                          |
| `resolveId` without a hook filter, in `hamur dev`                                                                               | `[hamur] vite plugin "<name>" resolves every import; Bun's dev bundler then reloads every module on each hot update. A resolveId hook filter avoids it` |
| `this.getModuleInfo`, `this.getModuleIds`, `this.parse`, `this.load`, `this.setAssetSource`, `this.emitFile({ type: "chunk" })` | throws `vite plugin "<name>": this.parse is not available in @hamur/vite-compat`                                                                        |
| `this.error(message)`                                                                                                           | throws `vite plugin "<name>": <message>`                                                                                                                |
| `this.emitFile({ type: "asset" })` with a `name` without an extension                                                           | throws `vite plugin "<name>": this.emitFile name "logo" needs an extension`                                                                             |

### Imports a plugin claims

Hamur refuses an import of a file without a loader (`./post.mdx`) or with a query it does not know (`./logo.svg?react`). When a plugin's `load` returns code for such a relative or absolute import, or, without a query, a `transform` does, the import is the plugin's and passes. That `load` or `transform` runs once more at resolve time. An import no plugin claims is refused as before.

### `VitePluginLike`

The plugin shape `fromVite` reads, structural so that Vite's own `Plugin` is assignable: `name: string`, `enforce?: "pre" | "post"`, `apply?: "serve" | "build" | ((config, env: { command: "serve" | "build"; mode: string }) => boolean)`, and the hooks `config`, `configResolved`, `buildStart`, `resolveId`, `load`, `transform`, `buildEnd`, `closeBundle` and the unmapped ones listed under [What does not run](/vite-plugins#what-does-not-run), each typed `unknown`. A hook is a function or `{ handler, order?, filter? }`.

### `VitePluginOption`

`VitePluginLike | false | null | undefined | VitePluginOption[] | Promise<VitePluginOption>`: what a Vite plugin factory returns. Lists are flattened, `false`, `null` and `undefined` skipped, promises awaited.

### Compatibility

Each row is a fixture in `tests/fixtures/vite-compat-*`, built with the bun preset and run under `hamur dev`. The plugins are devDependencies of their fixture only.

| Plugin                       | Result | What limits it                                                                                                                                                                                                                                             |
| ---------------------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `unplugin-icons/vite` 24.0.0 | works  | nothing; the app installs an icon collection such as `@iconify-json/lucide` and, for `compiler: "jsx"`, `@svgr/core` and `@svgr/plugin-jsx`                                                                                                                |
| `vite-plugin-svgr` 5.2.0     | fails  | the plugin imports `vite`, which throws under Bun 1.4.4 (`First argument must be an Error object`, from `Error.captureStackTrace`). The error names the plugin. Waits on Bun                                                                               |
| `@unocss/vite` 66.13.0       | fails  | the build stops in `configResolved`, which reads `config.build.rollupOptions`, and the CSS is generated in `renderChunk`, which is not run. Under `hamur dev`, `configureServer` and `transformIndexHtml` are logged as not run, so CSS gets no hot update |
| `@mdx-js/rollup` 3.1.1       | works  | nothing                                                                                                                                                                                                                                                    |
