Skip to content

Commit 910a3fe

Browse files
chore: edit README file
1 parent a44c819 commit 910a3fe

File tree

1 file changed

+135
-25
lines changed

1 file changed

+135
-25
lines changed

README.md

Lines changed: 135 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,166 @@
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
32
[![Latest Version on Packagist](https://img.shields.io/packagist/v/yevheniivolosiuk/vite-with-wordpress.svg?style=flat-square)](https://packagist.org/packages/yevheniivolosiuk/vite-with-wordpress)
43
[![Tests](https://img.shields.io/github/actions/workflow/status/yevheniivolosiuk/vite-with-wordpress/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/yevheniivolosiuk/vite-with-wordpress/actions/workflows/run-tests.yml)
54
[![Total Downloads](https://img.shields.io/packagist/dt/yevheniivolosiuk/vite-with-wordpress.svg?style=flat-square)](https://packagist.org/packages/yevheniivolosiuk/vite-with-wordpress)
65

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.
109

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?
1211

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
1418

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+
---
1620

1721
## Installation
1822

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**
2024

2125
```bash
22-
composer require yevheniivolosiuk/vite-with-wordpress
26+
composer require yevheniivolosiuk/vite-with-wordpress
2327
```
2428

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:
2634

2735
```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+
}
3043
```
3144

32-
## Testing
45+
---
46+
Bootstrap without composer via native PHP
47+
```php
48+
// Bootstrap plugin/theme file
3349

34-
```bash
35-
composer test
50+
require_once 'path_to_new_classes/Vite.php'
3651
```
3752

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.
39146

40-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
147+
---
41148

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.
43154

44-
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
155+
# Contributing
45156

46-
## Security Vulnerabilities
157+
Contributions welcome! Open an issue or pull request to improve the integration.
47158

48-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
159+
# About
49160

50-
## Credits
161+
Created and maintained by Yevhenii Volosiuk for modern WordPress development with Vite.
51162

52-
- [Yevhenii Volosiuk](https://github.com/YevheniiVolosiuk)
53-
- [All Contributors](../../contributors)
163+
Inspired by Laravel.
54164

55165
## License
56166

0 commit comments

Comments
 (0)