Closed james0r closed 3 months ago
@james0r By curiosity, are you using a JS framework (ex: Vue) or not at all? And are you using vite v5 or v4? My incomprehension (#16) seems to be related with your FR
I tried using your code aswell but i have multiple concerns:
/hot
doesn't exists ?? so i get an exception
(Vite doesn't generate a /hot
path, i don't know what you put in there) 😄manifest.json
but not in .vite
folder which is strange to me (maybe because it's not Vite 5?)@vite/client
init
function? _(because you say to run it inside header.php
so i tried using wp_head
hook in functions.php
)_@DamChtlv No framework. I'm using Laravel Vite that creates the /hot file when in dev mode. Using Vite v4.5. Vite config is as follows
import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'
import fs from 'fs'
import { resolve } from 'path'
export default defineConfig({
base: '',
build: {
emptyOutDir: true,
manifest: true,
outDir: 'build',
assetsDir: 'assets'
},
server: {
host: "0.0.0.0",
https: false,
hmr: {
host: 'localhost',
},
},
css: {
postcss: {
plugins: [
require('postcss-import'),
require('tailwindcss/nesting'),
require('tailwindcss')(resolve(__dirname, './tailwind.config.js')),
require('autoprefixer')
]
}
},
plugins: [
laravel({
publicDirectory: 'build',
input: [
'src/theme.js',
'src/theme.css',
// 'src/scss/theme.scss'
],
refresh: [
'*/**/**.php'
]
})
],
resolve: {
alias: [
{
find: /~(.+)/,
replacement: process.cwd() + '/node_modules/$1'
},
]
}
})
Not sure what you mean about missing an init function. When in dev mode, scripts are injected here in Vite.lib.php
public static function enqueue_module(string $buildPath = null): void
{
// we only want to continue if we have a client.
if (!$client = Vite::init($buildPath, false)) {
return;
}
if (!is_admin()) {
// enqueue our client script
wp_enqueue_script('vite-client',$client,[],null);
}
// update html script type to module wp hack
Vite::script_type_module('vite-client');
}
and when in production mode they're injected via idleberg's package here via the inject() method
$viteAssets->inject($jsEntry, [
'action' => 'wp_head',
'integrity' => false
]);
$viteAssets->inject($cssEntry, [
'action' => 'wp_head',
'integrity' => false
]);
Thank you for taking time to answer!
I finally managed to make it work (after digging a lot of vite config, github repos, try & errors haha) I had few incomprehensions coming from webpack / gulp which is why i didn't understand how vite worked behind the scene. You're using laravel vite which also confused me 😅 I ended up creating my own integration in the end
Otherwise your proposal seems great :)
What's the relationship between Laravel Vite and the script linked in the initial post? Does one depend on the other? Is it the same script or a stripped down version?
I have also been looking to get this to work with WordPress and Svelte and have run into similar issues of not having a manifest file in development mode.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 14 days.
This issue was closed because it has been stalled for 14 days with no activity.
I've been using this script for a while to integrate my Vite assets in my WordPress theme, but noticed the other day that when I went to import CSS into my JS entryfile the PHP script failed to traverse my Vite manifest to find CSS chunks to inject.
So today I find php-wordpress-vite-assets which seems to do this just fine, but I'm noticing it doesn't handle the Vite client at all for running my Vite dev server.
I combined the two to achieve what I wanted, and seems to work fine but it would be nice if this package included Vite client injection so I could just depend on this package.
Here's what I got
Vite.lib.php
functions.php
Just wanted to hear thoughts on this.
Thanks!