šŸš€ Using Vite with Inertia ā€” Laravel, Vue & Tailwind

Kodeas
3 min readJun 27, 2021

--

Weā€™ve been using Laravel-Mix for years and itā€™s been doing pretty good. However, recently we decided to build a project using Inertia.js (which was an awesome decision).

As the project started to grow, the development became a pain to wait for the webpack compiling.

VILT šŸ¤ VITE

Then, we decided to give Vite (from Vueā€™s creator Evan You) and the results wereā€¦ ASTONISHING!

Iā€™ve been seeing Vite around in Twitter, but to be honest with you, I was not expecting THIS MUCH OF SPEED! šŸš€

Laravel-Mix was getting too so slow.

Benchmark Test on Hot Reloading (with 16" MBP 64gb ram, 2.4 GHz 8-Core Intel Core i9)

|    Compilation     | Laravel Mix | Vite  |
|--------------------|-------------|-------|
| Initial Compile | 13257ms | 636ms |
| Change Reflection | 5848ms | 224ms |

Thaā€™s like ā€˜20x for initial compilationā€™ and ā€˜25x when code changeā€™ šŸ˜²

We are fascinated by results, so let me tell you how to set it up, so you can try it out too.

Migrating To Vite

  • First, youā€™ll need to install Vite: npm install vite
  • Then, install Tailwind
npm i tailwindcss postcss autoprefixer -D
  • Create ā€œvite.config.jsā€ and ā€œpostcss.config.jsā€ in your project base
Mostly taken from Sebastian De Deyne ā€” https://sebastiandedeyne.com/vite-with-laravel/

For the sake of completeness, here is Tailwind config (JIT is amazing as well!)

And finally, you need to configure your app.js for Vite to work with Inertia.js. (The production compiling part kept me in the dark for a few hours)

Few things to keep in mind are:

  • You canā€™t use require(ā€œfileā€) syntax, so you always need to use import * from file.js
  • You need to specify file extensions when importing Vue components, like ā€œimport FormInput from ā€œ@/Shared/Form/FormInput.vueā€
  • ā€œapp.jsā€ is the only point of entry for your app, so you need to import your app.css file in your app.js.

ā€¦and your front-end is ready šŸŽ‰

Setting up Laravel and package.json scripts

I wanted to run ā€œhot reloadingā€ as well as ā€œproductionā€ environment interchangeably in my local, so I came up with the below solution. (If you figure out a better way, Iā€™d be happy to hear)

Vite in ā€˜dev modeā€™ creates a local server in https://localhost:3000 (which can be configured in vite.config.js) whereas in ā€˜production modeā€™, it creates files in ā€˜public/distā€™.

  • Edit your ā€˜package.jsonā€™ file accordingly:

npm run vite is hot reloading itself and npm run dev is just for alias. The ā€œpreā€ hooks are used to create a file in public directory so that the backend can figure out which mode is running.

Finally, you need to create a helper to resolve the path in your blade ā€” just like Laravel Mixā€™s {{ mix('/js/app.js') }} helper.

You can create this php file in ā€˜app/Helpers/vite.phpā€™ (or anywhere you like)

Mostly taken from Sebastian De Deyne ā€” https://sebastiandedeyne.com/vite-with-laravel/

And include it to your ā€˜composer.jsonā€™

"autoload": {
"psr-4": {...},
"files": [
"app/Helpers/vite.php"
]
},

[make sure to run:composer dump-autoload]

And finally add it to your master.blade.php

<!DOCTYPE html>
<html>
<head>
<!-- Stuff -->
{{ vite_assets() }}
<!-- More Stuff -->
</head>

šŸ You are all set. Enjoy the super-speed compiling times šŸš€

I believe this will change your development experience as drastic as it did to me! šŸš€

Iā€™m really curious about your compiling speeds, please leave a comment. šŸ’¬

And lastly, if you enjoyed this read šŸ“š or find it helpful, please give us a šŸ‘ and if youā€™d like to read topics like this, go ahead and follow šŸ˜Š

--

--