Skip to content
Vite package

Vite Plugin Enumify

Run Laravel Enumify during Vite dev and build so your TypeScript enums regenerate whenever a PHP enum changes.

pnpm add -D @devwizard/vite-plugin-enumify
View on GitHub
On this page

Overview

Vite Plugin Enumify is the Vite half of Laravel Enumify. The Laravel package generates TypeScript from your PHP enums; this plugin calls that generator for you, so the frontend never builds against stale enum files.

It runs php artisan enumify:sync --force --quiet when Vite starts, and during development it re-runs the sync when a file inside your enum directories changes. It reads config/enumify.php to learn where your enums and output live. MIT licensed, maintained by the DevWizard core team.

Features

  • Sync on build start, before your TypeScript is compiled.
  • Dev-server watching of the enum directories, debounced by 250 ms, with a rerun queued if a change arrives mid-sync.
  • Reads paths.enums, paths.output and runtime.watch from config/enumify.php.
  • Ignores changes inside the output directory so the generated files cannot trigger a loop.
  • Configurable PHP binary, artisan file, command, working directory and environment.
  • Runs as an enforce: 'pre' plugin; ships ESM and CJS builds with types.

Requirements

  • Vite 4, 5, 6, 7 or 8.
  • Node.js 18 or newer.
  • PHP on the path where Vite runs.
  • A Laravel app with Laravel Enumify installed.

Installation

Terminal
pnpm add -D @devwizard/vite-plugin-enumify

Other package managers:

Terminal
npm install --save-dev @devwizard/vite-plugin-enumify
yarn add -D @devwizard/vite-plugin-enumify

Install the Laravel side if you have not:

Terminal
composer require devwizardhq/laravel-enumify
php artisan enumify:install

Configuration

Add the plugin to vite.config.ts, ahead of the Laravel plugin:

TypeScript
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import enumify from '@devwizard/vite-plugin-enumify';

export default defineConfig({
  plugins: [
    enumify(),
    laravel({
      input: ['resources/js/app.ts'],
      refresh: true,
    }),
  ],
});

All options are optional:

Option Default Description
artisanBin 'php' PHP binary
artisanFile 'artisan' Path to the artisan file
syncCommand 'enumify:sync' Artisan command to run
cwd process.cwd() Working directory for the command and for locating the config
watch runtime.watch from config/enumify.php, else true Watch enum directories in dev
env {} Extra environment variables for the process

Usage

Start Vite as usual. The first build event runs the sync, and generated files appear under your output directory:

Terminal
pnpm dev

Edit a PHP enum and save. In dev, the plugin notices the change inside paths.enums, waits for the debounce window, runs the sync once and logs that the enum types were generated. On pnpm build the sync runs once before compilation, and a failure stops the build with an error.

API Reference

enumify(options)

Named export and default export of @devwizard/vite-plugin-enumify. Returns a Vite Plugin named @devwizard/vite-plugin-enumify.

Hooks used: configResolved (captures the Vite logger and config), buildStart (runs the sync; in serve mode only the first time), configureServer (adds the enum directories to the watcher when watch is on) and handleHotUpdate (re-syncs for files inside the enum directories, ignoring the output directory).

Types

EnumifyOptions describes the options object and ResolvedEnumifyOptions the options after defaults are applied. Both are exported.

Config discovery

The plugin parses config/enumify.php with simple pattern matching rather than running PHP. It looks for the enums, output and watch keys. Keep those as plain string, array and boolean literals. If the file is missing or unreadable, the defaults app/Enums, resources/js/enums and true apply.

Examples

Use a different PHP binary, for example one managed by a version tool:

TypeScript
enumify({ artisanBin: '/usr/local/bin/php8.3' });

Turn off watching but keep the build-time sync:

TypeScript
enumify({ watch: false });

Point at a Laravel app in a subfolder of a monorepo:

TypeScript
enumify({ cwd: new URL('../backend', import.meta.url).pathname });

Pass environment variables to the process:

TypeScript
enumify({ env: { APP_ENV: 'local' } });

FAQ & Troubleshooting

The build fails with an enumify sync error. The plugin runs the artisan command and fails on a non-zero exit code. Run php artisan enumify:sync directly to see the real error, and confirm php is on the path that Vite runs in.

Edits to an enum do not regenerate in dev. Check that runtime.watch is not set to false and that the enum sits under one of the paths.enums directories.

Do I need to run the sync by hand? No. Use php artisan enumify:sync only for a one-off run or in CI without Vite.

Why must the plugin come before the Laravel plugin? Generated enum files must exist before imports are resolved, and the plugin runs with pre ordering for that reason.

Which commands does the plugin run? Only the configured artisan command with --force --quiet. It does not install or modify anything.

Changelog & Support

Release history is in the CHANGELOG and on the GitHub releases page. Report bugs and request features in GitHub issues. Source: DevWizardHQ/vite-plugin-enumify. The PHP generator lives in Laravel Enumify. For help with Laravel and Vite products, the DevWizard team takes project contracts and monthly retainers through the contact page.