PHP code example of stubbornweb / vite-with-wordpress

1. Go to this page and download the library: Download stubbornweb/vite-with-wordpress library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stubbornweb / vite-with-wordpress example snippets


root/
├── resources/
├── public/

// Bootstrap plugin/theme file

use StubbornWeb\ViteWithWordPress\Vite;

if (file_exists(__DIR__ . '/vendor/autoload.php')) {
    

// Bootstrap plugin/theme file


use StubbornWeb\ViteWithWordPress\Vite;

add_action('wp_enqueue_scripts', function () {
    // ✅ Recommended: Include "vite" in handle to enable `type="module"`
    wp_enqueue_script(
        'vite-main-script-file',
        Vite::asset('resources/js/main.js'),
        ['jquery'],
        '1.0.0',
        true
    );

    wp_enqueue_style(
        'vite-main-style-file',
        Vite::asset('resources/js/main.js', 'css'),
        [],
        '1.0.0',
    );

    // 🚫 Not recommended if you want `type="module"` automatically
    // This script will not get the type="module" attribute
    wp_enqueue_script(
        'main-script-file',
        Vite::asset('resources/js/main.js'),
        ['jquery'],
        '1.0.0',
        true
    );
});

Vite::asset(string $assetPath, string|bool $css = ''): ?string