Learn to configure Vite for WordPress to enable lightning-fast HMR, efficient asset management, and a streamlined production build pipeline for your plugins.
Previously in this course, we covered Auditing Plugin Security to ensure our code was battle-hardened. Now that our backend is secure, we need to modernize our frontend delivery.
While legacy setups often rely on Introduction to @wordpress/scripts: Modern WordPress Builds or older Managing Assets with Gulp/Webpack: Professional Build Workflows, Vite represents a paradigm shift. It leverages native ES modules and esbuild for pre-bundling, offering near-instant server starts and HMR that makes Webpack feel sluggish.
In traditional Webpack setups, the entire bundle is rebuilt on every change. Vite, however, serves source files over native ESM. When you save a file, Vite only invalidates the specific module, resulting in HMR speeds that remain constant regardless of application size.
For our Knowledge Base plugin, this means we can iterate on our React-based admin dashboards without waiting for the "recompile" spinner.
To integrate Vite, we need to bridge the gap between Vite's dev server (which runs on a separate port) and WordPress's asset loader.
Create a vite.config.js in your plugin root. We must define the entry point and ensure the output is compatible with our plugin directory.
JAVASCRIPTimport { defineConfig } from CE9178">'vite'; import react from CE9178">'@vitejs/plugin-react'; export default defineConfig({ plugins: [react()], build: { outDir: CE9178">'build', rollupOptions: { input: CE9178">'src/index.js', output: { entryFileNames: CE9178">'app.js', }, }, }, server: { proxy: { CE9178">'/wp-admin': CE9178">'http://localhost:8888', // Your local WP URL }, }, });
The biggest challenge with Vite is that it serves assets from a dynamic dev server (e.g., http://localhost:5173), but WordPress expects files from the plugin directory. We use a development helper to inject the Vite client script during local development.
PHP#6A9955">// In your Service Provider or Asset Loader class public function enqueue_assets() { if (defined('WP_DEBUG') && WP_DEBUG) { #6A9955">// Inject Vite HMR client wp_enqueue_script('vite-client', 'http:#6A9955">//localhost:5173/@vite/client', [], null); wp_enqueue_script('my-plugin-js', 'http:#6A9955">//localhost:5173/src/index.js', [], null, true); } else { #6A9955">// Enqueue production build wp_enqueue_script('my-plugin-js', plugin_dir_url(__FILE__) . 'build/app.js', [], '1.0.0', true); } }
Development and production builds serve different masters. Development favors speed; production favors cacheability and minification.
| Feature | Development (Vite) | Production (Rollup) |
|---|---|---|
| Bundling | On-demand (ESM) | Pre-bundled (IIFE/CJS) |
| Speed | Instant HMR | Slower (Minification) |
| Sourcemaps | Enabled by default | Usually disabled |
| Assets | Served from memory | Written to disk |
Add these to your package.json:
JSON"scripts": { "dev": "vite", "build": "vite build" }
When you run npm run build, Vite uses Rollup to create a production-ready, minified asset in your /build folder. Unlike older tools, you don't need complex Babel configurations—Vite handles modern JavaScript transpilation out of the box.
npm init -y and npm install vite @vitejs/plugin-react --save-dev in your plugin directory.vite.config.js as shown above.npm run dev. Verify that the browser console shows the Vite connection and that a change to a React component updates the UI without a full page refresh.localhost:8888 and Vite is on 5173, the browser may block requests. Ensure your vite.config.js includes proper proxy settings or that your local server headers allow requests from the Vite port.wp-includes or heavy libraries into your client entry. Use import statements carefully to keep the initial load small.WP_DEBUG or a dedicated constant.By shifting to Vite, you've optimized your developer experience. As we move into React component architecture, you'll find that this setup allows for rapid prototyping, which is essential for building complex, scalable interfaces.
Up next: React Component Architecture — where we'll define how to structure our UI code for maximum reuse and testability.
Learn how to implement search and filtering for your WordPress Knowledge Base. We'll cover updating REST API GET requests, React state, and empty result handling.
Read moreMaster the @wordpress/data architecture. Learn how the store, selector, and action pattern provides a robust, global state for your WordPress plugins.
Modern Build Tooling with Vite
Custom Hooks for React