I want to use VueJs partially in my Laravel project and with Laravel Mix it works perfectly fine but not with Vite Asset Bundling.
With Laravel Mix:
webpack.mix.js
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.vue();
resources\js\app.js
window.Vue = require('vue');
Layout Blade file
<script src="{{ mix('js/app.js') }}" type="text/javascript"></script>
@stack('scripts')
In any blade file of laravel
@push('scripts')
<script>
const app = Vue.createApp({
data() {
return {
username: 'John Doe'
}
}
});
app.mount('#app');
</script>
@endpush
This works perfectly fine.
But with Vite Asset Bundling it is not working. It gives us console errors Vue is not defined & require is not defined.
With Vite:
vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/sass/app.scss',
'resources/js/app.js',
],
refresh: true,
}),
vue(),
],
});
resources\js\app.js
import './bootstrap';
window.Vue = require('vue');
Layout Blade File
@vite(['resources/js/app.js'])
@stack('scripts')
In any blade file of laravel
@push('scripts')
<script>
const app = Vue.createApp({
data() {
return {
username: 'John Doe'
}
}
});
app.mount('#app');
</script>
@endpush
Please update if you found any solution regarding the same or let me know if I am making a silly mistake :D. Thank you!