Skip to content

Instantly share code, notes, and snippets.

@sentiasa
Last active February 5, 2024 23:12
Show Gist options
  • Save sentiasa/01131c3691e594600a2135c588b2a0c7 to your computer and use it in GitHub Desktop.
Save sentiasa/01131c3691e594600a2135c588b2a0c7 to your computer and use it in GitHub Desktop.
Vite setup for Laravel, Inertia, Vue, Tailwind
import 'vite/dynamic-import-polyfill';
import '../css/app.css';
import { createApp, h } from 'vue'
import { App, plugin } from '@inertiajs/inertia-vue3'
let asyncViews = () => {
return import.meta.glob('./Pages/**/*.vue');
}
const app = createApp({
render: () => h(App, {
initialPage: JSON.parse(el.dataset.page),
resolveComponent: async name => {
if (import.meta.env.DEV) {
return (await import(`./Pages/${name}.vue`)).default;
} else {
let pages = asyncViews();
const importPage = pages[`./Pages/${name}.vue`];
return importPage().then(module => module.default);
}
}
})
})
"scripts": {
"predev": "printf \"dev\" > public/hot",
"dev": "vite",
"preprod": "printf \"prod\" > public/hot",
"prod": "vite build"
},
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
module.exports = {
mode: "jit",
purge: ['./resources/**/*.{js,jsx,ts,tsx,vue,blade.php}'],
theme: {},
variants: {},
plugins: [],
}
const { resolve } = require('path');
import vue from '@vitejs/plugin-vue';
export default ({ command }) => ({
base: command === 'serve' ? '' : '/dist/',
publicDir: 'fake_dir_so_nothing_gets_copied',
build: {
manifest: true,
outDir: resolve(__dirname, 'public/dist'),
rollupOptions: {
input: 'resources/js/app.js',
},
},
plugins: [vue()],
resolve: {
alias: {
'@': resolve('./resources/js'),
},
},
});
<?php
use Illuminate\Support\HtmlString;
if (! function_exists('vite_assets')) {
/**
* @return HtmlString
* @throws Exception
*/
function vite_assets(): HtmlString
{
$devServerIsRunning = false;
if (app()->environment('local')) {
try {
$devServerIsRunning = file_get_contents(public_path('hot')) == 'dev';
} catch (Exception) {}
}
if ($devServerIsRunning) {
return new HtmlString(<<<HTML
<script type="module" src="http://localhost:3000/@vite/client"></script>
<script type="module" src="http://localhost:3000/resources/js/app.js"></script>
HTML);
}
$manifest = json_decode(file_get_contents(
public_path('dist/manifest.json')
), true);
return new HtmlString(<<<HTML
<script type="module" src="/dist/{$manifest['resources/js/app.js']['file']}"></script>
<link rel="stylesheet" href="/dist/{$manifest['resources/js/app.js']['css'][0]}">
HTML);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment