DEV Community

Cover image for Using Vite with Inertia — Laravel, Vue & Tailwind

Using Vite with Inertia — Laravel, Vue & Tailwind

Bertug Korucu on July 11, 2021

TLDR; 5800ms (Laravel Mix) to 224ms (Vite⚡️) on hot-reload! We’ve been using Laravel-Mix for years and it’s been doing pretty good. However, recen...
Collapse
 
simcha profile image
Simcha

Greetings

I created a new project based on this guide sebastiandedeyne.com/vite-with-lar...

Unfortunately there is no detailed explanation in the manual (last part) about inertia with react,

And I can in no way arrange between them, it already seems that everything works but this error returns:

Uncaught (in promise) Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
Enter fullscreen mode Exit fullscreen mode

You can check out the project here: github.com/sbc640964/laravel-vite-...

Collapse
 
nehadhiman6 profile image
Neha Dhiman • Edited

unable to add flowbite in my project

added this in tailwind.config.js

module.exports = {
    content: [
        './vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php',
        './vendor/laravel/jetstream/**/*.blade.php',
        './storage/framework/views/*.php',
        './resources/views/**/*.blade.php',
        './resources/js/**/*.vue',
    ],

    theme: {
        extend: {
            fontFamily: {
                sans: ['Nunito', ...defaultTheme.fontFamily.sans],
            },
            colors: {
                "primary-color": "var(--primary-color)",
                "secondary-color": "var(--secondary-color)"
            },
        },
    },

    plugins: [require('@tailwindcss/forms'), require('@tailwindcss/typography'),  require('flowbite/plugin')],
};

Enter fullscreen mode Exit fullscreen mode

in app.js

import 'flowbite';
Enter fullscreen mode Exit fullscreen mode

this is not working!

Also i am adding Vue plugins like this:


createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(plugin)
            .use(ZiggyVue, Ziggy)
           .mixin({ components: { FilePond } })            // THIS
            .mount(el);
    },
});
Enter fullscreen mode Exit fullscreen mode

it is working fine but is it right way to add plugins. In previous version it was Vue.use(FilePond); // not working in vue 3

Collapse
 
rrrrando profile image
Rando Õispuu

Great that more and more people are using Vite. If you want to get rid of some extra code then have look at my blade file github.com/kristjanjansen/newstack...

Collapse
 
bertugkorucu profile image
Bertug Korucu • Edited

Good idea, but I wanted to have flexibility for running npm run prod and npm run hot simultaneously in my local. And I didn't want to have that logic in my blade. If you don't need much logic code, it's ideal to place it in blade indeed.

Collapse
 
fkranenburg profile image
Ferry Kranenburg

I got this error while following this article, any ideas? I started the app with 'npm run dev' and got this:

'printf' is not recognized as an internal or external command,

Collapse
 
fkranenburg profile image
Ferry Kranenburg

If anyone is running npm in command prompt, change script in package.json to this:

"scripts": {
    "predev": "echo dev > public/hot",
    "dev": "vite",
    "preprod": "echo prod > public/hot",
    "prod": "vite build"
}
Enter fullscreen mode Exit fullscreen mode

Because 'echo' adds a newline at the end of the file you also need to change vite.php this line:

$devServerIsRunning = str_contains(file_get_contents(public_path('hot')), 'dev');

Collapse
 
bertugkorucu profile image
Bertug Korucu

Yeah, that was the reason I used printf instead of echo

Collapse
 
mwangaben profile image
Benedict Mwanga • Edited

after this inertia-link stops working as the are not render as a tag on the browser

Collapse
 
bertugkorucu profile image
Bertug Korucu

It shouldn't be happening - we built a whole app with inertia and vite and it works like charm

Collapse
 
kevmul0929 profile image
kevmul0929

Does anyone get the error

Failed to resolve import "vite/dynamic-import-polyfill" from "resources\js\app.js"
Enter fullscreen mode Exit fullscreen mode

I can't seem to find the fix for this.

Collapse
 
mitchazj profile image
Mitchell Johnson

Probably too late for this answer to be directly useful but answering in case anyone else is googling: unless you have to support older browsers (IE11 for example) then it's safe to just delete the import from your app.js file.

The import is only there to add polyfill support for es6 modules and I suspect vite removed it.
If you need it back, take a look at the legacy plugin: github.com/vitejs/vite/tree/main/p...

You'll have to reconfigure the import, and change some settings depending on your specific support needs.
But if you don't need to support legacy browsers, easiest fix is just delete the line and don't worry about it :)