Buckets:
| // Copyright (c) 2026 the Zuke contributors | |
| // SPDX-License-Identifier: MIT | |
| /** | |
| * `ViteTasks` — typed task functions for the [Vite](https://vitejs.dev) CLI, in | |
| * the settings-lambda style: configure a fluent settings object in a lambda, | |
| * and the task function builds the command line and executes it. | |
| * | |
| * ```ts | |
| * import { ViteTasks } from "@zuke/vite"; | |
| * await ViteTasks.build((s) => s.outDir("dist").mode("production")); | |
| * await ViteTasks.preview((s) => s.port(4173)); | |
| * ``` | |
| * | |
| * Arguments stay a discrete argv array end-to-end — never a concatenated shell | |
| * string — so command construction is injection-free. | |
| * | |
| * @module | |
| */ | |
| import { | |
| type Configure, | |
| type PathLike, | |
| runSettings, | |
| type ToolResolution, | |
| ToolSettings, | |
| } from "@zuke/core/tooling"; | |
| import type { CommandOutput } from "@zuke/core/shell"; | |
| /** | |
| * Base for all `vite` subcommand settings: the binary is `vite`, with the | |
| * `--config` and `--mode` options shared by every subcommand. | |
| */ | |
| export abstract class ViteSettings extends ToolSettings { | |
| #config?: string; | |
| #mode?: string; | |
| /** The default binary this wrapper invokes: `vite`. */ | |
| protected override defaultTool(): string { | |
| return "vite"; | |
| } | |
| /** Resolve the binary from `node_modules/.bin` by default — vite is an npm-distributed tool. */ | |
| protected override defaultResolution(): ToolResolution { | |
| return "node_modules"; | |
| } | |
| /** Use an explicit config file (`--config`). */ | |
| config(path: PathLike): this { | |
| this.#config = String(path); | |
| return this; | |
| } | |
| /** Set the mode, e.g. `production` or `development` (`--mode`). */ | |
| mode(name: string): this { | |
| this.#mode = name; | |
| return this; | |
| } | |
| /** The shared option arguments. */ | |
| protected baseArgs(): string[] { | |
| const argv: string[] = []; | |
| if (this.#config !== undefined) argv.push("--config", this.#config); | |
| if (this.#mode !== undefined) argv.push("--mode", this.#mode); | |
| return argv; | |
| } | |
| } | |
| /** Settings for `vite dev` (the development server). */ | |
| export class ViteDevSettings extends ViteSettings { | |
| #host?: string; | |
| #port?: number; | |
| #strictPort = false; | |
| #open = false; | |
| /** Bind to a host/IP (`--host`). */ | |
| host(value: string): this { | |
| this.#host = value; | |
| return this; | |
| } | |
| /** Serve on a specific port (`--port`). */ | |
| port(value: number): this { | |
| this.#port = value; | |
| return this; | |
| } | |
| /** | |
| * Fail instead of trying the next free port when {@link port} is taken | |
| * (`--strictPort`). | |
| * | |
| * A build that hands the port to something else — an e2e runner, a health | |
| * check — needs the server to be on the port it asked for or not at all; | |
| * without this Vite silently moves on and the other side connects to nothing. | |
| */ | |
| strictPort(): this { | |
| this.#strictPort = true; | |
| return this; | |
| } | |
| /** Open the app in the browser on start (`--open`). */ | |
| open(): this { | |
| this.#open = true; | |
| return this; | |
| } | |
| /** Assemble the `vite dev` argv. */ | |
| protected override buildArgs(): string[] { | |
| const argv = ["dev", ...this.baseArgs()]; | |
| if (this.#host !== undefined) argv.push("--host", this.#host); | |
| if (this.#port !== undefined) argv.push("--port", String(this.#port)); | |
| if (this.#strictPort) argv.push("--strictPort"); | |
| if (this.#open) argv.push("--open"); | |
| return argv; | |
| } | |
| } | |
| /** Settings for `vite build`. */ | |
| export class ViteBuildSettings extends ViteSettings { | |
| #outDir?: string; | |
| #base?: string; | |
| #emptyOutDir = false; | |
| #sourcemap = false; | |
| #root?: string; | |
| /** Output directory (`--outDir`). */ | |
| outDir(path: PathLike): this { | |
| this.#outDir = String(path); | |
| return this; | |
| } | |
| /** Public base path (`--base`). */ | |
| base(path: string): this { | |
| this.#base = path; | |
| return this; | |
| } | |
| /** Empty the output directory before building (`--emptyOutDir`). */ | |
| emptyOutDir(): this { | |
| this.#emptyOutDir = true; | |
| return this; | |
| } | |
| /** Emit source maps (`--sourcemap`). */ | |
| sourcemap(): this { | |
| this.#sourcemap = true; | |
| return this; | |
| } | |
| /** The project root (positional). */ | |
| root(path: PathLike): this { | |
| this.#root = String(path); | |
| return this; | |
| } | |
| /** Assemble the `vite build` argv. */ | |
| protected override buildArgs(): string[] { | |
| const argv = ["build", ...this.baseArgs()]; | |
| if (this.#base !== undefined) argv.push("--base", this.#base); | |
| if (this.#outDir !== undefined) argv.push("--outDir", this.#outDir); | |
| if (this.#emptyOutDir) argv.push("--emptyOutDir"); | |
| if (this.#sourcemap) argv.push("--sourcemap"); | |
| if (this.#root !== undefined) argv.push(this.#root); | |
| return argv; | |
| } | |
| } | |
| /** Settings for `vite preview` (serve a production build locally). */ | |
| export class VitePreviewSettings extends ViteSettings { | |
| #host?: string; | |
| #port?: number; | |
| #strictPort = false; | |
| #open = false; | |
| /** Bind to a host/IP (`--host`). */ | |
| host(value: string): this { | |
| this.#host = value; | |
| return this; | |
| } | |
| /** Serve on a specific port (`--port`). */ | |
| port(value: number): this { | |
| this.#port = value; | |
| return this; | |
| } | |
| /** | |
| * Fail instead of trying the next free port when {@link port} is taken | |
| * (`--strictPort`). See {@link ViteDevSettings.strictPort}. | |
| */ | |
| strictPort(): this { | |
| this.#strictPort = true; | |
| return this; | |
| } | |
| /** Open the app in the browser on start (`--open`). */ | |
| open(): this { | |
| this.#open = true; | |
| return this; | |
| } | |
| /** Assemble the `vite preview` argv. */ | |
| protected override buildArgs(): string[] { | |
| const argv = ["preview", ...this.baseArgs()]; | |
| if (this.#host !== undefined) argv.push("--host", this.#host); | |
| if (this.#port !== undefined) argv.push("--port", String(this.#port)); | |
| if (this.#strictPort) argv.push("--strictPort"); | |
| if (this.#open) argv.push("--open"); | |
| return argv; | |
| } | |
| } | |
| /** The shape of {@link ViteTasks}. */ | |
| export interface ViteTasksApi { | |
| /** Start the dev server: `vite dev`. */ | |
| dev(configure?: Configure<ViteDevSettings>): Promise<CommandOutput>; | |
| /** Build for production: `vite build`. */ | |
| build(configure?: Configure<ViteBuildSettings>): Promise<CommandOutput>; | |
| /** Preview a production build: `vite preview`. */ | |
| preview(configure?: Configure<VitePreviewSettings>): Promise<CommandOutput>; | |
| } | |
| /** Typed task functions for the `vite` CLI. */ | |
| export const ViteTasks: ViteTasksApi = { | |
| dev(configure?: Configure<ViteDevSettings>): Promise<CommandOutput> { | |
| return runSettings(new ViteDevSettings(), configure); | |
| }, | |
| build(configure?: Configure<ViteBuildSettings>): Promise<CommandOutput> { | |
| return runSettings(new ViteBuildSettings(), configure); | |
| }, | |
| preview(configure?: Configure<VitePreviewSettings>): Promise<CommandOutput> { | |
| return runSettings(new VitePreviewSettings(), configure); | |
| }, | |
| }; | |
Xet Storage Details
- Size:
- 6.72 kB
- Xet hash:
- f92059d75d5fc337126049598edb37534548c96481f76130e53b30c96f49b569
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.