Skip to content

atomjoy/laravel-vue3-vite-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 

Repository files navigation

Vue 3 in Laravel

How To Install Vue 3 in Laravel 10 with Vite.

Create project

composer create-project --prefer-dist laravel/laravel:^10.0 laravel-vue
cd laravel-vue

Install

npm install
npm install vue@latest
npm install --save-dev @vitejs/plugin-vue

Config

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/css/app.css', 'resources/js/app.js'],
			refresh: true,
		}),
		vue({
			template: {
				transformAssetUrls: {
					base: null,
					includeAbsolute: false,
				},
			},
		}),
	],
	// Change default url /build directory
	base: './', 
	build: {
		emptyOutDir: true,
	},
	// Or change assets dir
	// build: {
	// 	rollupOptions: {
	// 		output: {
	// 			assetFileNames: 'assets/[ext]/[name].[hash].[extname]',
	// 			chunkFileNames: 'chunks/[name].[hash].js',
	// 			entryFileNames: 'js/[name].[hash].js',
	// 		},
	// 	},
	// },
});

resources/js/app.js

require('./bootstrap');

import {createApp} from 'vue'

import App from './App.vue'

createApp(App).mount("#app")

resources/js/App.vue

<template>
    How To Install Vue 3 in Laravel 10 with Vite.
</template>
<script setup></script>

welcome.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How To Install Vue 3 in Laravel 10 with Vite</title>

    @vite('resources/css/app.css')
</head>
<body>
    <div id="app"></div>

    @vite('resources/js/app.js')
</body>
</html>

.env

APP_URL=http://localhost:8000

routes/web.php

<?php

// Last route
Route::fallback(function() {
    return view('welcome');
});

Build and run

npm run build
php artisan serve

Install optional packages

npm install axios
npm install pinia
npm install vue-i18n@9
npm install vue-router@4
npm install @googlemaps/js-api-loader

Override ** /build ** dir in page url

// Add this in vue router/index.js
history: createWebHistory('/')

// Or add in vite.config.js
base: './', 

Links