To set up a WordPress project with Vite for modern frontend development, you can follow these steps. Vite is a fast build tool that serves as an alternative to traditional bundlers like Webpack, offering fast development and build times.

Prerequisites

  1. Node.js: Ensure you have Node.js installed.
  2. WordPress Installation: Have a WordPress site installed locally (e.g., using XAMPP, MAMP, or similar).

Step 1: Install Vite

  1. Navigate to your theme’s directory:
   cd /path-to-your-wordpress/wp-content/themes/your-theme-name
  1. Initialize a new Node.js project:
   npm init -y
  1. Install Vite and any necessary dependencies:
   npm install vite --save-dev

Step 2: Configure Vite

  1. Create a vite.config.js file in your theme’s root directory:
   import { defineConfig } from 'vite';

   export default defineConfig({
     root: './',
     base: '/wp-content/themes/your-theme-name/',
     build: {
       outDir: 'dist',
       assetsDir: 'assets',
       rollupOptions: {
         input: {
           main: './src/main.js',
           // Add other entry points if needed
         },
       },
     },
     server: {
       watch: {
         usePolling: true,
       },
     },
   });

Explanation:

  • root: The root directory of your Vite project.
  • base: The base public path of your theme.
  • build: Configuration for building your assets.
  • server: Configures the development server.
  1. Create a src directory in your theme folder and place your main JavaScript/CSS files there.

Step 3: Modify WordPress Theme

  1. Enqueue your assets in functions.php:
   function enqueue_vite_assets() {
       // Development
       if (WP_DEBUG) {
           wp_enqueue_script('vite', 'http://localhost:5173/@vite/client', [], null, true);
           wp_enqueue_script('vite-js', 'http://localhost:5173/src/main.js', [], null, true);
       } else {
           // Production
           wp_enqueue_style('theme-style', get_template_directory_uri() . '/dist/assets/main.css', [], filemtime(get_template_directory() . '/dist/assets/main.css'));
           wp_enqueue_script('theme-script', get_template_directory_uri() . '/dist/assets/main.js', [], filemtime(get_template_directory() . '/dist/assets/main.js'), true);
       }
   }
   add_action('wp_enqueue_scripts', 'enqueue_vite_assets');

Explanation:

  • During development, it loads the assets from the Vite development server.
  • For production, it enqueues the built assets.

Step 4: Start Development Server

  1. Run Vite’s development server:
   npm run dev
  1. Open your WordPress site in your browser and Vite should now handle live reloading and module bundling.

Step 5: Build for Production

  1. When ready to deploy, build your assets:
   npm run build
  1. Vite will output your production-ready files to the dist folder.

Step 6: Deploy

  • Deploy your theme as usual, making sure to include the built assets from the dist directory.

By integrating Vite, your WordPress development process will be much faster and more streamlined, especially when working with modern JavaScript frameworks like React or Vue.

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending