### Basic Wayfinder Vite Plugin Setup Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/README.md Integrates the Wayfinder Vite plugin with default settings. Ensure Wayfinder is installed and configured in your project. ```typescript import { wayfinder } from "@laravel/vite-plugin-wayfinder"; export default defineConfig({ plugins: [ wayfinder(), // ... ], }); ``` -------------------------------- ### Install Vite Plugin Wayfinder Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Install the plugin as a development dependency using your preferred package manager. ```bash npm install --save-dev @laravel/vite-plugin-wayfinder ``` ```bash yarn add --dev @laravel/vite-plugin-wayfinder ``` ```bash pnpm add --save-dev @laravel/vite-plugin-wayfinder ``` -------------------------------- ### Minimal Vite Plugin Wayfinder Setup Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md This is the recommended minimal setup for the Vite Plugin Wayfinder. It uses all default settings, generates both routes and actions, and watches standard Laravel file locations. ```typescript wayfinder() ``` -------------------------------- ### Constructing Wayfinder Command with Custom Configuration Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Demonstrates how to construct the final command executed by the plugin based on specific configuration options. This example shows how `actions`, `routes`, `formVariants`, and `path` settings translate into command-line flags. ```typescript wayfinder({ actions: false, routes: true, formVariants: true, path: 'resources/js/generated', command: 'php artisan wayfinder:generate' }) ``` ```bash php artisan wayfinder:generate --skip-actions --with-form --path=resources/js/generated ``` -------------------------------- ### Custom Watcher Patterns, Path, and Sail Command Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md A comprehensive configuration example that watches custom action locations, outputs to a custom path, and uses the Laravel Sail command for generation. ```typescript wayfinder({ patterns: ['app/Domains/*/Actions/**/*.php'], path: 'types/generated', command: 'sail artisan wayfinder:generate' }) ``` -------------------------------- ### Vite Build Start Hook for Type Generation Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/plugin-lifecycle.md This hook runs at the beginning of the Vite build process. It captures the plugin context and initiates the type generation command. ```typescript buildStart() { context = this; return runCommand(); } ``` -------------------------------- ### Configure Wayfinder Plugin with Options Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/types.md Example of how to import and use the wayfinder plugin with custom configuration options. Ensure the plugin is imported from '@laravel/vite-plugin-wayfinder'. ```typescript import { wayfinder } from '@laravel/vite-plugin-wayfinder'; const options: WayfinderOptions = { patterns: ['routes/**/*.php', 'app/Actions/**/*.php'], actions: true, routes: true, formVariants: true, path: 'resources/js/types', command: 'php artisan wayfinder:generate', }; export default defineConfig({ plugins: [wayfinder(options)], }); ``` -------------------------------- ### Vite Build Start Trigger Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md Initiates type generation when a Vite build starts, either in development or production. It captures context and calls the runCommand function. ```text ┌──────────────────────────────────────────────────────────────┐ │ Vite Build Start (dev server or npm run build) │ └──────────────────┬───────────────────────────────────────────┘ │ ▼ ┌────────────────────────┐ │ buildStart() hook │ │ (1. Capture context │ │ 2. Call runCommand()) │ └────────────┬───────────┘ │ ▼ ┌────────────────────────────┐ │ Build command arguments │ │ based on options │ └────────────┬───────────────┘ │ ▼ ┌────────────────────────────────────────┐ │ Execute: php artisan wayfinder:generate│ │ + --skip-X flags + --path + --with-form│ └────────────┬───────────────┘ │ ▼ ┌────────────────────────┐ │ Log success/error │ └────────────────────────┘ ``` -------------------------------- ### wayfinder() Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/api-reference.md The main entry point for the Vite plugin. Creates and returns a Vite plugin instance configured to generate Wayfinder types during Vite builds and when watched files change. ```APIDOC ## wayfinder() ### Description The main entry point for the Vite plugin. Creates and returns a Vite plugin instance configured to generate Wayfinder types during Vite builds and when watched files change. ### Signature ```typescript function wayfinder(options?: WayfinderOptions): Plugin ``` ### Parameters #### options - **options** (`WayfinderOptions`) - Optional - Configuration object for the plugin behavior ### WayfinderOptions Type Definition ```typescript interface WayfinderOptions { patterns?: string[]; actions?: boolean; routes?: boolean; formVariants?: boolean; path?: string; command?: string; } ``` #### Options Details - **patterns** (`string[]`) - Optional - Glob patterns for files to watch. When any matching file changes, the plugin regenerates types. Backslashes are automatically normalized to forward slashes. Default: `["routes/**/*.php", "app/**/Http/**/*.php"]` - **actions** (`boolean`) - Optional - Whether to generate types for Wayfinder actions. Pass `false` to skip action type generation and add `--skip-actions` flag. Default: `true` - **routes** (`boolean`) - Optional - Whether to generate types for Wayfinder routes. Pass `false` to skip route type generation and add `--skip-routes` flag. Default: `true` - **formVariants** (`boolean`) - Optional - Whether to generate types for form variants. When `true`, adds `--with-form` flag to the command. Default: `false` - **path** (`string`) - Optional - Custom output path for generated types. When provided, adds `--path={value}` to the command. Default: `undefined` - **command** (`string`) - Optional - Custom command to execute for generating types. By default runs the Laravel Artisan command. Default: `"php artisan wayfinder:generate"` ### Return Type Returns a Vite `Plugin` object with: - `name`: `"@laravel/vite-plugin-wayfinder"` - `enforce`: `"pre"` — ensures this plugin runs before other plugins - `buildStart()` hook that executes the type generation command - `handleHotUpdate()` hook that re-executes the command when watched files change ### Behavior 1. **Initialization**: When Vite builds start (`buildStart` hook), the plugin executes the configured command with appropriate flags based on the options. 2. **Watch Mode**: During development, when any file matching the provided patterns changes, the plugin automatically re-runs the command. 3. **Logging**: On successful generation, logs `"Types generated for {features}"` where features are the enabled generators (routes, actions, form variants). 4. **Error Handling**: Execution errors are caught and logged via Vite's error context, preventing the build from failing. ### Usage Examples #### Basic Configuration ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder(), ], }); ``` #### Custom Watch Patterns ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder({ patterns: ['resources/**/myroutes/*.php', 'app/Actions/**/*.php'], }), ], }); ``` #### Selective Type Generation ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder({ routes: true, actions: false, formVariants: false, }), ], }); ``` #### Custom Command and Output Path ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder({ command: 'herd php artisan wayfinder:generate', path: 'resources/js/types', }), ], }); ``` ``` -------------------------------- ### Command Execution Flow Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md Details the execution of the Wayfinder generation command. It shows the command format, the use of execAsync to spawn a child process, and the handling of success or error outcomes. ```text runCommand() called │ ├─→ Build command: `${command} ${args.join(" ")}` │ Example: "php artisan wayfinder:generate --path=resources/js/types" │ ├─→ execAsync() spawns child process │ ├─→ Two possible outcomes: │ │ │ ├─→ Success (exit code 0) │ │ │ │ │ └─→ context.info() logs completion │ │ "Types generated for routes, actions" │ │ │ └─→ Error (exception thrown) │ │ │ └─→ catch block calls context.error() │ "Error generating types: [error message]" │ (does not rethrow, build continues) ``` -------------------------------- ### Initialize Wayfinder with Custom Output Path Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/README.md Specify a custom directory for generated type files using the 'path' option. This is useful for organizing generated code. ```typescript wayfinder({ path: 'resources/js/generated' }) ``` -------------------------------- ### Initialize Wayfinder Plugin Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/README.md Use this snippet to initialize the Wayfinder plugin with default settings. This is suitable for standard Laravel projects. ```typescript wayfinder() // Uses all defaults ``` -------------------------------- ### Basic Wayfinder Plugin Configuration Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/api-reference.md Configure the Wayfinder Vite plugin with default settings. Ensure the plugin is imported and included in the Vite configuration. ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder(), ], }); ``` -------------------------------- ### Watch Multiple Custom Locations Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to watch files across multiple custom directories. This allows for flexible project structures. ```typescript wayfinder({ patterns: [ 'routes/**/*.php', 'app/Http/**/*.php', 'app/Domains/*/Actions/**/*.php' ] }) ``` -------------------------------- ### Construct Command Arguments Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/plugin-lifecycle.md Dynamically builds command-line arguments based on feature flags and configuration options. Use this to customize the command execution. ```typescript const args: string[] = []; if (!actions) { args.push("--skip-actions"); } if (!routes) { args.push("--skip-routes"); } if (formVariants) { args.push("--with-form"); } if (path) { args.push(`--path=${path}`); } ``` -------------------------------- ### Plugin Implementation Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md Contains the core logic for the Vite plugin, including options, the plugin factory function, and utility functions for determining when to run type generation. It handles command construction and Vite/Rollup hook integration. ```typescript import { spawn } from "child_process"; import { promisify } from "util"; import * as path from "path"; import type { Plugin, HmrContext } from "vite"; import type { PluginContext } from "rollup"; import { minimatch } from "minimatch"; const exec = promisify(spawn); interface WayfinderOptions { /** * The path to the Laravel application. * @default process.cwd() */ appPath?: string; /** * The path to the Wayfinder executable. * @default "wayfinder" */ wayfinderPath?: string; /** * Arguments to pass to the Wayfinder executable. * @default [] */ args?: string[]; /** * Glob patterns to include for file watching. * @default ["app/**", "routes/**", "config/**"] */ watchGlobs?: string[]; /** * Glob patterns to exclude for file watching. * @default [] */ ignoreGlobs?: string[]; /** * Whether to enable hot module replacement. * @default true */ hmr?: boolean; } function wayfinder(options: Partial = {}): Plugin { const appPath = options.appPath ?? process.cwd(); const wayfinderPath = options.wayfinderPath ?? "wayfinder"; const args = options.args ?? []; const watchGlobs = options.watchGlobs ?? ["app/**", "routes/**", "config/**"]; const ignoreGlobs = options.ignoreGlobs ?? []; const hmr = options.hmr ?? true; let viteContext: PluginContext; const shouldRun = (filePath: string): boolean => { return ( !ignoreGlobs.some((glob) => minimatch(filePath, glob)) ); }; const runWayfinder = async (): Promise => { const command = `${wayfinderPath} ${args.join(" ")}`; console.log(`\nRunning Wayfinder: ${command}\n`); try { await exec(wayfinderPath, [...args, `--appPath=${appPath}`], { stdio: "inherit", }); console.log("\nWayfinder finished successfully.\n"); } catch (error) { console.error("\nWayfinder failed to execute:\n", error); // Optionally re-throw or handle the error differently } }; return { name: "vite-plugin-wayfinder", // Use 'configResolved' to ensure all options are resolved configResolved(resolvedConfig) { viteContext = this as unknown as PluginContext; // Ensure the plugin runs only in build mode or when HMR is enabled if (resolvedConfig.command === "build" || hmr) { runWayfinder(); } }, // Watch for changes and re-run Wayfinder if necessary handleHotUpdate(ctx: HmrContext) { if (!hmr) return; const isRelevantChange = ctx.modules.some((module) => { const filePath = module.file?.filepath; return filePath && shouldRun(filePath); }); if (isRelevantChange) { console.log("\nFile changed, re-running Wayfinder...\n"); runWayfinder(); } }, }; } export { wayfinder }; ``` -------------------------------- ### Vite Configuration with Custom Wayfinder Options Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/README.md Configure the Wayfinder plugin with custom options to control type generation, output path, and the artisan command. Set 'routes' and 'actions' to true to enable generation for both. ```typescript export default defineConfig({ plugins: [ wayfinder({ routes: true, actions: true, formVariants: false, path: 'resources/js/types', command: 'php artisan wayfinder:generate', }), ], }); ``` -------------------------------- ### Trigger Type Generation with Vite Build Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Run a Vite build command to generate TypeScript types for routes and actions. Verify the output for a success message. ```bash npm run build ``` -------------------------------- ### File Path Matching Algorithm Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/plugin-lifecycle.md Determines if a file should trigger an action based on a list of patterns. It normalizes paths and uses minimatch for glob matching, ensuring cross-platform compatibility. ```typescript const shouldRun = (patterns, { file, server }) => { const normalizedFile = file.replaceAll("\", "/"); return patterns.some((pattern) => { const normalizedPattern = osPath .resolve(server.config.root, pattern) .replaceAll("\", "/"); return minimatch(normalizedFile, normalizedPattern); }); }; ``` -------------------------------- ### Initialize Wayfinder with Custom Watch Patterns Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/README.md Customize the file patterns that the Wayfinder plugin watches for changes. This allows you to include specific directories or file types for type regeneration. ```typescript wayfinder({ patterns: ['app/Domains/*/Actions/**/*.php'] }) ``` -------------------------------- ### Type-Safe Route and Action Generation Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Import and use the `route` and `action` functions from 'wayfinder' to generate type-safe URLs and call actions. Ensure the import paths match your Wayfinder configuration. ```typescript // resources/js/app.ts import { route, action } from 'wayfinder'; // Type-safe route generation const url = route('posts.show', { id: 123 }); // Type-safe action calling const result = await action('send-email', { email: 'user@example.com' }); ``` -------------------------------- ### Configure for Laravel Herd Environment Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to use the 'herd' command for generation instead of 'php' directly. This is useful when working within a Laravel Herd environment. ```typescript wayfinder({ command: 'herd php artisan wayfinder:generate' }) ``` -------------------------------- ### Capture Vite PluginContext in buildStart Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/plugin-lifecycle.md Captures the Vite PluginContext during the buildStart hook to enable logging and access to build information throughout the plugin's lifecycle. ```typescript let context: PluginContext; buildStart() { context = this; return runCommand(); } ``` -------------------------------- ### Configure Wayfinder with All Features Enabled Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Enable all Wayfinder features, including routes, actions, form variants, and specify a custom output path for comprehensive type generation. ```typescript export default defineConfig({ plugins: [ wayfinder({ routes: true, actions: true, formVariants: true, path: 'resources/js/generated', }), ], }); ``` -------------------------------- ### Argument Builder Pattern for CLI Flags Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md Dynamically converts configuration options into command-line arguments for explicit and extensible flag generation. ```typescript const args: string[] = []; if (!actions) args.push("--skip-actions"); if (!routes) args.push("--skip-routes"); if (formVariants) args.push("--with-form"); if (path) args.push(`--path=${path}`); // Final command: command + " " + args.join(" ") ``` -------------------------------- ### Watch Action Classes in Custom Location Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to watch action classes located in a custom directory. Ensure the path accurately reflects your project structure. ```typescript wayfinder({ patterns: ['app/Actions/**/*.php'] }) ``` -------------------------------- ### Custom Command and Output Path for Wayfinder Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/api-reference.md Specify a custom command to execute for type generation and define a custom output path for the generated types. This is useful for non-standard project structures or build processes. ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder({ command: 'herd php artisan wayfinder:generate', path: 'resources/js/types', }), ], }); ``` -------------------------------- ### Plugin Factory Pattern in TypeScript Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md Standard pattern for Vite plugins, accepting options and returning a Plugin object. ```typescript export const wayfinder = (options?: WayfinderOptions = {}): Plugin => { // Configuration processing // ... setup code ... return { name: "@laravel/vite-plugin-wayfinder", enforce: "pre", buildStart() { ... }, handleHotUpdate({ file, server }) { ... }, }; }; ``` -------------------------------- ### Set Custom Output Path Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to output generated types to a custom directory. Specify the desired path using the 'path' option. ```typescript wayfinder({ path: 'resources/js/generated-types' }) ``` -------------------------------- ### Configure Wayfinder Custom Output Directory Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Specify a custom directory for generated types using the `path` option in the Wayfinder plugin configuration. ```typescript export default defineConfig({ plugins: [ wayfinder({ path: 'resources/js/types', }), ], }); ``` -------------------------------- ### Watch Root-Level Routes Directory Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to watch only PHP files directly within the root 'routes' directory. This is a more restrictive pattern. ```typescript wayfinder({ patterns: ['routes/*.php'] }) ``` -------------------------------- ### Public API Definition Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md This file serves as the public entry point for the plugin, re-exporting the main 'wayfinder' function. It ensures a clean API surface. ```typescript export { wayfinder } from "./vite-plugin-wayfinder"; ``` -------------------------------- ### File Change Trigger for Type Generation Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md Handles file changes during development by triggering the type generation process if the changed file matches configured patterns. It normalizes paths, resolves patterns, and uses minimatch for file matching. ```text ┌──────────────────────────────────────────────────────────────┐ │ File changes during development │ └──────────────────┬───────────────────────────────────────────┘ │ ▼ ┌────────────────────────────────┐ │ handleHotUpdate() hook called │ │ with changed file path │ └────────────┬───────────────────┘ │ ▼ ┌────────────────────────────────────────────┐ │ shouldRun(patterns, { file, server }) │ │ 1. Normalize paths (backslash → forward) │ │ 2. Resolve patterns relative to root │ │ 3. minimatch file against each pattern │ └────────────┬─────────────────────────────┘ │ ┌──────────┴──────────┐ │ │ ▼ ▼ ┌─────────────┐ ┌──────────────┐ │ Match found │ │ No match │ │ Call │ │ No action │ │ runCommand()│ └──────────────┘ └─────────────┘ ``` -------------------------------- ### Execute Generation Command Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/plugin-lifecycle.md Executes the type generation command using Node's child_process.exec. Ensure the 'command' and 'args' variables are correctly defined before execution. ```typescript await execAsync(`${command} ${args.join(" ")}`); ``` -------------------------------- ### Watch Specific Route Files Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to watch only specific route files. This is useful for projects with a small number of critical route definitions. ```typescript wayfinder({ patterns: ['routes/web.php', 'routes/api.php'] }) ``` -------------------------------- ### WayfinderOptions Interface Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/types.md The WayfinderOptions interface defines the configuration properties for the wayfinder() plugin. All properties are optional and have default values. ```APIDOC ## WayfinderOptions ### Description Configuration interface for the `wayfinder()` plugin function. All properties are optional and have sensible defaults. ### Type Definition ```typescript interface WayfinderOptions { patterns?: string[]; actions?: boolean; routes?: boolean; formVariants?: boolean; path?: string; command?: string; } ``` ### Field Reference | Field | Type | Required | Default | Description | |---|---|---|---|---| | `patterns` | `string[]` | No | `["routes/**/*.php", "app/**/Http/**/*.php"]` | Glob patterns specifying which files to watch for changes. Uses minimatch-compatible glob syntax. Backslashes are normalized to forward slashes automatically. Each pattern is resolved relative to Vite's configured root directory. | | `actions` | `boolean` | No | `true` | Flag to include action type generation. When `false`, the `--skip-actions` flag is passed to the Wayfinder command. | | `routes` | `boolean` | No | `true` | Flag to include route type generation. When `false`, the `--skip-routes` flag is passed to the Wayfinder command. | | `formVariants` | `boolean` | No | `false` | Flag to include form variant type generation. When `true`, the `--with-form` flag is passed to the Wayfinder command. | | `path` | `string` | No | `undefined` | Custom output path for generated type files. When provided, passed as `--path={value}` to the Wayfinder command. If undefined, uses Wayfinder's default output location. | | `command` | `string` | No | `"php artisan wayfinder:generate"` | The command to execute for generating types. Can be customized to use different PHP environments (e.g., `"herd php artisan wayfinder:generate"`). | ### Usage Context This type is passed to the `wayfinder()` function to configure plugin behavior: ```typescript import { wayfinder } from '@laravel/vite-plugin-wayfinder'; const options: WayfinderOptions = { patterns: ['routes/**/*.php', 'app/Actions/**/*.php'], actions: true, routes: true, formVariants: true, path: 'resources/js/types', command: 'php artisan wayfinder:generate', }; export default defineConfig({ plugins: [wayfinder(options)], }); ``` ### Related Functions - **`wayfinder(options?)`** in `api-reference.md` — accepts this type as its parameter ### Source Location - **File**: `src/vite-plugin-wayfinder.ts` - **Lines**: 10-17 - **Visibility**: Exported (available when importing from `@laravel/vite-plugin-wayfinder`) ``` -------------------------------- ### shouldRun() Function Signature Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/api-reference.md This is the TypeScript signature for the internal shouldRun() utility function. It takes an array of glob patterns and an object containing the file path and Vite server instance as input, returning a boolean. ```typescript function shouldRun( patterns: string[], opts: Pick ): boolean ``` -------------------------------- ### Initialize Wayfinder for Routes Only Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/README.md Configure the Wayfinder plugin to generate only route types. Set 'actions' to false to disable action type generation. ```typescript wayfinder({ actions: false }) ``` -------------------------------- ### Configure Wayfinder for Laravel Herd Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Use the `command` option to specify the Artisan command for type generation when using Laravel Herd. ```typescript export default defineConfig({ plugins: [ wayfinder({ command: 'herd php artisan wayfinder:generate', }), ], }); ``` -------------------------------- ### Configure Wayfinder for Laravel Sail Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Use the `command` option to specify the Sail Artisan command for type generation when using Laravel Sail (Docker). ```typescript export default defineConfig({ plugins: [ wayfinder({ command: 'sail artisan wayfinder:generate', }), ], }); ``` -------------------------------- ### Path Normalization for Cross-OS Compatibility Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md Normalizes paths to use forward slashes, ensuring consistent glob matching and compatibility across different operating systems. ```typescript // In factory patterns = patterns.map((pattern) => pattern.replace("\", "/")); // In shouldRun const file = opts.file.replaceAll("\", "/"); pattern = osPath.resolve(...).replaceAll("\", "/"); ``` -------------------------------- ### Configure Wayfinder Custom Watch Patterns Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Limit the files Wayfinder watches for type generation by providing an array of glob patterns to the `patterns` option. ```typescript export default defineConfig({ plugins: [ wayfinder({ patterns: [ 'routes/**/*.php', 'app/Actions/**/*.php', ], }), ], }); ``` -------------------------------- ### Configure Vite Plugin Wayfinder for Actions Only Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to generate only action types, skipping route types. Set 'actions' to true and 'routes' to false. ```typescript wayfinder({ actions: true, routes: false }) ``` -------------------------------- ### Configure Vite Plugin Wayfinder for Routes Only Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to generate only route types, skipping action types. Set 'actions' to false and 'routes' to true. ```typescript wayfinder({ actions: false, routes: true }) ``` -------------------------------- ### wayfinder(options?: WayfinderOptions): Plugin Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/README.md The main function exported by the package. It accepts optional configuration options to customize the type generation process and returns a Vite plugin instance. This plugin integrates with Vite's build pipeline to automatically generate or update TypeScript types for Laravel routes and actions. ```APIDOC ## wayfinder(options?: WayfinderOptions): Plugin ### Description This function is the primary export of the `@laravel/vite-plugin-wayfinder` package. It is responsible for creating a Vite plugin that generates TypeScript types for Laravel routes and actions. The function accepts an optional configuration object (`WayfinderOptions`) to tailor its behavior and returns a Vite `Plugin` instance. ### Signature ```typescript export function wayfinder(options?: WayfinderOptions): Plugin ``` ### Parameters #### Options (`WayfinderOptions`) - **routes** (boolean) - Optional - Whether to generate types for routes. Defaults to `true`. - **actions** (boolean) - Optional - Whether to generate types for actions. Defaults to `true`. - **formVariants** (boolean) - Optional - Whether to generate types for form variants. Defaults to `true`. - **path** (string) - Optional - The directory where TypeScript types will be generated. Defaults to `'resources/js/types'`. - **command** (string) - Optional - The Artisan command to run for generating types. Defaults to `'php artisan wayfinder:generate'`. ### Returns - **Plugin** - A Vite plugin instance that integrates with the build process. ``` -------------------------------- ### WayfinderOptions Type Definition Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/types.md Defines the configuration options for the wayfinder() plugin. All properties are optional and have sensible defaults. ```typescript interface WayfinderOptions { patterns?: string[]; actions?: boolean; routes?: boolean; formVariants?: boolean; path?: string; command?: string; } ``` -------------------------------- ### Custom Wayfinder Vite Plugin Configuration Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/README.md Configures the Wayfinder Vite plugin with custom options. Adjust path, command, and feature flags as needed for your project structure and requirements. ```typescript import { wayfinder } from "@laravel/vite-plugin-wayfinder"; export default defineConfig({ plugins: [ wayfinder({ path: "my/custom/path/to/js", command: "herd php artisan wayfinder:generate", routes: false, actions: true, formVariants: false, patterns: ["resources/**/myroutes/*.php"], }), // ... ], }); ``` -------------------------------- ### Default Configuration for Wayfinder Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Applies when `wayfinder()` is called with no arguments or an empty object. This configuration watches route and controller files, generates route and action types, and uses default output locations. ```typescript { patterns: ["routes/**/*.php", "app/**/Http/**/*.php"], actions: true, routes: true, formVariants: false, path: undefined, command: "php artisan wayfinder:generate" } ``` -------------------------------- ### Exported API Surface of Vite Plugin Wayfinder Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/INDEX.md This snippet illustrates the main exported function `wayfinder` and its associated `WayfinderOptions` interface, detailing the available configuration properties. Use this to understand the plugin's entry point and customization capabilities. ```typescript Module: @laravel/vite-plugin-wayfinder ├─ wayfinder(options?: WayfinderOptions): Plugin └─ WayfinderOptions (interface) ├─ patterns?: string[] ├─ actions?: boolean ├─ routes?: boolean ├─ formVariants?: boolean ├─ path?: string └─ command?: string ``` -------------------------------- ### Enable Form Variants Generation Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/configuration.md Configure the plugin to generate form variant types in addition to routes and actions. Set 'formVariants' to true. ```typescript wayfinder({ formVariants: true }) ``` -------------------------------- ### Log Successful Type Generation Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/plugin-lifecycle.md Logs a success message indicating which types were generated. The 'generating' array should contain the enabled features. ```typescript context.info(`Types generated for ${generating.join(", ")}`); ``` -------------------------------- ### Configure Wayfinder to Watch Only Routes Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Generate types only for routes and disable action type generation by setting `routes` to true and `actions` to false. ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder({ routes: true, actions: false, }), ], }); ``` -------------------------------- ### Closure Pattern for State Management in TypeScript Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/architecture.md Uses closure to manage plugin state and context across hooks, avoiding classes or global state. ```typescript let context: PluginContext; export const wayfinder = (options?: WayfinderOptions = {}) => { const args: string[] = []; const generating: string[] = []; const patterns = /* normalized patterns */; const runCommand = async () => { // Access: context, args, patterns, generating }; return { buildStart() { context = this; // Capture context return runCommand(); }, handleHotUpdate() { // Uses: context, patterns if (shouldRun(patterns, { file, server })) { await runCommand(); } }, }; }; ``` -------------------------------- ### Custom Watch Patterns for Wayfinder Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/api-reference.md Customize the file patterns that trigger Wayfinder type regeneration. This allows you to specify which files should be watched for changes. ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder({ patterns: ['resources/**/myroutes/*.php', 'app/Actions/**/*.php'], }), ], }); ``` -------------------------------- ### Integrate Wayfinder with Laravel Vite Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/integration-guide.md Add the Wayfinder plugin to your Vite configuration when using the official Laravel Vite plugin. The order of plugins does not affect functionality. ```typescript import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ laravel(['resources/js/app.ts']), wayfinder(), ], }); ``` -------------------------------- ### Selective Type Generation with Wayfinder Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/api-reference.md Control which types Wayfinder generates by setting boolean options. Disable specific generators like actions or form variants if not needed. ```typescript import { defineConfig } from 'vite'; import { wayfinder } from '@laravel/vite-plugin-wayfinder'; export default defineConfig({ plugins: [ wayfinder({ routes: true, actions: false, formVariants: false, }), ], }); ``` -------------------------------- ### Vite Hot Update Hook for Type Regeneration Source: https://github.com/laravel/vite-plugin-wayfinder/blob/main/_autodocs/plugin-lifecycle.md This hook is triggered during development when a file changes. It checks if the modified file matches configured patterns and regenerates types if necessary. ```typescript async handleHotUpdate({ file, server }) { if (shouldRun(patterns, { file, server })) { await runCommand(); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.