# Vite

Build, serve and preview every package with a Vite config, with the build output cached.

The Vite plugin gives every package with a Vite config a `build`, `dev` and `preview` task, so a repository of Vite apps builds, caches and serves them without a script or a line of `axle.toml` for each.

## Add it

```sh
axle add vite
```

This writes it into the root `axle.toml`:

```toml
[plugins.vite]
source = "npm:@axle-rs/plugin-vite@0.1.0"
hash = "sha256-…"
```

## Tasks

A package or project with a `vite.config.ts`, `.mts`, `.cts`, `.js`, `.mjs` or `.cjs` in its folder gets the tasks below. SvelteKit apps, with a `svelte.config` file and `@sveltejs/kit` among their dependencies, and React Router apps, with a `react-router.config` file, are left to the [SvelteKit](https://axlers.dev/docs/official-plugins/sveltekit.md) and [React Router](https://axlers.dev/docs/official-plugins/react-router.md) plugins, which know their outputs and commands:

| Task      | Command        | Settings                                                                    |
| --------- | -------------- | --------------------------------------------------------------------------- |
| `build`   | `vite build`   | `outputs` the build's output folder                                         |
| `dev`     | `vite`         | `persistent`, ready when Vite prints the `Local` address                    |
| `preview` | `vite preview` | `persistent`, ready when Vite prints the `Local` address, after the `build` |

Each command runs through the repository's package manager, such as `pnpm exec vite build`, so it uses the Vite the package installs. The tasks are ready by Vite's log rather than by its address, since Vite moves to the next port when its own is taken.

The output folder is read from the config without running it. `build.outDir`, `dist` by default, counts from `root` when the config sets one, as in Vite. A folder outside the package is stored with `{root}/` when it's inside the repository. When either option is set to something other than a plain string, such as `resolve(__dirname, 'out')`, or the folder is outside the repository, the plugin leaves `outputs` out and Axle [finds the outputs](https://axlers.dev/docs/reference/cache.md#outputs) the build writes itself.

## Scripts the repository already has

A package's own script wins. When its `package.json` has a script with a task's name that runs something else, such as `"build": "tsc && vite build"`, the plugin leaves that task alone and Axle runs the script. A script that only runs Vite the way the plugin would, `vite build`, `vite`, `vite dev`, `vite serve` or `vite preview`, is replaced by the plugin's task, which runs the same command and adds its settings. The repository's `axle.toml` changes a task as it would any other, and wins over the plugin.

## Settings

| Setting   | Default     | Meaning                                                |
| --------- | ----------- | ------------------------------------------------------ |
| `build`   | `"build"`   | the name of the build task, or `false` to leave it out |
| `dev`     | `"dev"`     | the name of the dev server task, or `false`            |
| `preview` | `"preview"` | the name of the preview task, or `false`               |

```toml
[plugins.vite]
source = "npm:@axle-rs/plugin-vite@0.1.0"
hash = "sha256-…"
build = "bundle"
preview = false
```

The plugin's answers are kept until a `vite.config.*`, `svelte.config.*`, `react-router.config.*`, a `package.json`, `pnpm-workspace.yaml` or a lockfile changes.
