|
1 | | -# A simple, elegant Vite integration for WordPress themes and plugins — inspired by Laravel's Vite helper, providing seamless asset management, HMR support, and production-ready manifest handling. |
2 | | - |
| 1 | +# Vite With WordPress |
3 | 2 | [](https://packagist.org/packages/yevheniivolosiuk/vite-with-wordpress) |
4 | 3 | [](https://github.com/yevheniivolosiuk/vite-with-wordpress/actions/workflows/run-tests.yml) |
5 | 4 | [](https://packagist.org/packages/yevheniivolosiuk/vite-with-wordpress) |
6 | 5 |
|
7 | | -This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example. |
8 | | - |
9 | | -## Support us |
| 6 | +A simple, elegant Vite integration for WordPress themes and plugins — inspired by Laravel's Vite helper, providing seamless asset management, HMR support, and production-ready manifest handling. |
| 7 | +--- |
| 8 | +Seamlessly integrate [Vite](https://vitejs.dev/) into your WordPress theme or plugin for fast, modern frontend development with hot module replacement (HMR) and efficient production builds. |
10 | 9 |
|
11 | | -[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/vite-with-wordpress.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/vite-with-wordpress) |
| 10 | +## Why use this? |
12 | 11 |
|
13 | | -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). |
| 12 | +- Native WordPress support for Vite assets |
| 13 | +- Automatic dev server detection with HMR support |
| 14 | +- Manifest-based asset URL resolution for production |
| 15 | +- Easy static facade: `Vite::asset()` to get asset URLs |
| 16 | +- Injects `type="module"` on scripts for ES modules support |
| 17 | +- Supports extracting and enqueuing CSS linked from JS entrypoints |
14 | 18 |
|
15 | | -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). |
| 19 | +--- |
16 | 20 |
|
17 | 21 | ## Installation |
18 | 22 |
|
19 | | -You can install the package via composer: |
| 23 | +1. **Clone or install the package into your WordPress theme or plugin folder via composer or copy just 2 classes located in `src/` to your project** |
20 | 24 |
|
21 | 25 | ```bash |
22 | | -composer require yevheniivolosiuk/vite-with-wordpress |
| 26 | + composer require yevheniivolosiuk/vite-with-wordpress |
23 | 27 | ``` |
24 | 28 |
|
25 | | -## Usage |
| 29 | +2. **Set up your Vite project** with your assets inside the theme/plugin directory, e.g. `resources/js/main.js` |
| 30 | + |
| 31 | +3. **Ensure your Vite config outputs assets to `/public/build`** inside your theme/plugin directory |
| 32 | + |
| 33 | +4. **Include PHP classes and `autoload` or simply `require` them into your theme/plugin: |
26 | 34 |
|
27 | 35 | ```php |
28 | | -$skeleton = new YevheniiVolosiuk/ViteWithWordPress\ViteBase(); |
29 | | -echo $skeleton->echoPhrase('Hello, YevheniiVolosiuk/ViteWithWordPress!'); |
| 36 | +// Bootstrap plugin/theme file |
| 37 | + |
| 38 | +use YevheniiVolosiuk\ViteWithWordPress\Vite; |
| 39 | + |
| 40 | +if (file_exists(__DIR__ . '/vendor/autoload.php')) { |
| 41 | + require_once __DIR__ . '/vendor/autoload.php'; |
| 42 | +} |
30 | 43 | ``` |
31 | 44 |
|
32 | | -## Testing |
| 45 | +--- |
| 46 | +Bootstrap without composer via native PHP |
| 47 | +```php |
| 48 | +// Bootstrap plugin/theme file |
33 | 49 |
|
34 | | -```bash |
35 | | -composer test |
| 50 | +require_once 'path_to_new_classes/Vite.php' |
36 | 51 | ``` |
37 | 52 |
|
38 | | -## Changelog |
| 53 | +## Vite Configuration Example |
| 54 | + |
| 55 | +```js |
| 56 | +// vite.config.js |
| 57 | + |
| 58 | +import path from 'path'; |
| 59 | +import { defineConfig } from 'vite'; |
| 60 | +import laravel from 'laravel-vite-plugin'; |
| 61 | + |
| 62 | +export default defineConfig({ |
| 63 | + base: '/wp-content/themes/vite-with-wordpress/public/build', |
| 64 | + plugins: [ |
| 65 | + laravel({ |
| 66 | + input: [ |
| 67 | + 'resources/js/main.js', |
| 68 | + 'resources/js/plugins/slider.js', |
| 69 | + ], |
| 70 | + refresh: [ |
| 71 | + { |
| 72 | + paths: ['/**/*.php', '*.php'] |
| 73 | + } |
| 74 | + ], |
| 75 | + }), |
| 76 | + ], |
| 77 | + resolve: { |
| 78 | + alias: { |
| 79 | + '@styles': path.resolve(__dirname, 'resources/scss'), |
| 80 | + '@scripts': path.resolve(__dirname, 'resources/js'), |
| 81 | + '@images': path.resolve(__dirname, 'resources/images'), |
| 82 | + }, |
| 83 | + }, |
| 84 | + server: { |
| 85 | + host: "0.0.0.0", |
| 86 | + port: 5173, |
| 87 | + strictPort: true, |
| 88 | + cors: { |
| 89 | + origin: "https://your-local-dev-url.test", |
| 90 | + credentials: true, |
| 91 | + }, |
| 92 | + hmr: { |
| 93 | + host: "localhost", |
| 94 | + protocol: "ws", |
| 95 | + }, |
| 96 | + }, |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +## Usage in WordPress |
| 101 | + |
| 102 | +```php |
| 103 | +use YevheniiVolosiuk\ViteWithWordPress\ViteBase; |
| 104 | + |
| 105 | +add_action('wp_enqueue_scripts', function () { |
| 106 | + // Enqueue main JS file |
| 107 | + wp_enqueue_script( |
| 108 | + 'main-script-file', |
| 109 | + Vite::asset('resources/js/main.js'), |
| 110 | + ['jquery'], |
| 111 | + '1.0.0', |
| 112 | + true |
| 113 | + ); |
| 114 | + |
| 115 | + // Enqueue main CSS linked from the JS entry point |
| 116 | + wp_enqueue_style( |
| 117 | + 'main-style-file', |
| 118 | + Vite::asset('resources/js/main.js', 'css'), |
| 119 | + [], |
| 120 | + '1.0.0', |
| 121 | + ); |
| 122 | +}); |
| 123 | +``` |
| 124 | +--- |
| 125 | +# How It Works |
| 126 | + |
| 127 | +### Development mode (npm run dev): |
| 128 | +- Detects Vite dev server by checking hot file presence and uses HMR URLs for assets. |
| 129 | + |
| 130 | +### Production mode (npm run build): |
| 131 | +- Loads manifest.json from public/build/ to resolve hashed filenames for cache-busting. |
| 132 | + |
| 133 | +### Script tag enhancement: |
| 134 | +- Automatically injects type="module" attribute to scripts loaded via WordPress to enable native ES modules. |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +# API |
| 139 | + |
| 140 | +```php |
| 141 | +Vite::asset(string $assetPath, string|bool $css = ''): ?string |
| 142 | +``` |
| 143 | +- Returns the full URL of the asset, adapting automatically to dev server or production build. |
| 144 | +- The second argument ('css' or true) returns the CSS file linked from a JS entrypoint, if available. |
| 145 | +- Returns null if the asset is unavailable. |
39 | 146 |
|
40 | | -Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 147 | +--- |
41 | 148 |
|
42 | | -## Contributing |
| 149 | +# Troubleshooting |
| 150 | +- Verify `manifest.json` exists in public/build after running build. |
| 151 | +- Confirm `hot` file is present during dev server run. |
| 152 | +- Make sure your WordPress URL and Vite config base & server.cors.origin are correctly set. |
| 153 | +- Errors related to missing assets will trigger a detailed WordPress error message including suggestions. |
43 | 154 |
|
44 | | -Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. |
| 155 | +# Contributing |
45 | 156 |
|
46 | | -## Security Vulnerabilities |
| 157 | +Contributions welcome! Open an issue or pull request to improve the integration. |
47 | 158 |
|
48 | | -Please review [our security policy](../../security/policy) on how to report security vulnerabilities. |
| 159 | +# About |
49 | 160 |
|
50 | | -## Credits |
| 161 | +Created and maintained by Yevhenii Volosiuk for modern WordPress development with Vite. |
51 | 162 |
|
52 | | -- [Yevhenii Volosiuk](https://github.com/YevheniiVolosiuk) |
53 | | -- [All Contributors](../../contributors) |
| 163 | +Inspired by Laravel. |
54 | 164 |
|
55 | 165 | ## License |
56 | 166 |
|
|
0 commit comments