Laravel Upgrade from Webpack to Vite
Upgrade Guide
Migrating from Laravel Mix to Vite
Note This upgrade guide does not cover all possible Mix use cases, such as Sass compilation. Please consult the Vite documentation for information on configuring Vite for these scenarios.
Update Laravel Framework
To make use of the new Vite integration, you will need to update to at least version 9.19.0
of the laravel/framework
:
Install Vite and the Laravel Plugin
First, you will need to install Vite and the Laravel Vite Plugin using your npm package manager of choice:
You may also need to install additional Vite plugins for your project, such as the Vue or React plugins:
Configure Vite
Create a vite.config.js
file in the root of your project:
If you are building an SPA, you will get a better developer experience by removing the CSS entry point above and importing your CSS from Javascript.
Update Aliases
If you are migrating aliases from your webpack.mix.js
file to your vite.config.js
file, you should ensure that the paths start with /
. For example, resources/js
would become /resources/js
:
For your convenience, the Laravel Vite plugin automatically adds an @
alias for your /resources/js
directory. If you do not need to customize your aliases, you may omit this section from your vite.config.js
file.
Update NPM scripts
Update your NPM scripts in package.json
:
Vite compatible imports
Vite only supports ES modules, so if you are upgrading an existing application you will need to replace any require()
statements with import
. You may refer to this pull request for an example.
Inertia
Inertia makes use of a require()
call that is more complex to replicate with Vite.
The following function can be used instead:
Additionally, you should ensure you have updated to at least version 0.6.3
of the inertia-laravel
package:
Update environment variables
You will need to update the environment variables that are explicitly exposed in your .env
files and in hosting environments such as Forge to use the VITE_
prefix instead of MIX_
:
Note You may optionally maintain the
MIX_
prefix by configuring Vite to use it.
You will also need to update these references in your JavaScript code to use the new variable name and Vite syntax:
Importing your CSS from your JavaScript entry point(s)
If you are building an SPA, you will get a better experience by importing your CSS from your JavaScript entry point(s), such as your resources/js/app.js
entry point:
In development mode, Vite will automatically inject your CSS into the page. In production, a dedicated stylesheet will be generated that the @vite
directive will load from the manifest.
Replace mix()
with @vite
mix()
with @vite
When using Vite, you will need to use the @vite
Blade directive instead of the mix()
helper.
This will automatically detect whether you are running in serve or build mode and include all of the required <script>
and <link rel="stylesheet">
for you:
The entry points should match those used in your vite.config.js
.
React
If you are using React and hot-module replacement, you will need to include an additional directive before the @vite
directive:
This loads a React "refresh runtime" in development mode only, which is required for hot module replacement to work correctly.
JavaScript files containing JSX must use a .jsx
extension
.jsx
extensionYou will need to rename any .js
files containing JSX to instead have a .jsx
extension. If you need to rename your entry point then you should read the entry point docs to learn how to configure the Laravel plugin for your project.
See this tweet from Vite's creator for more information.
Note If you are using Tailwind, remember to update the paths in your
tailwind.config.js
file.
Vue imports must include the .vue
extension
.vue
extensionRemove Laravel Mix
The Laravel Mix package can now be uninstalled:
And you may remove your Mix configuration file:
If you are using StyleCI and have ignored the webpack.mix.js
file in your configuration, you may also wish to remove the ignore rule.
Update Test Helpers
If you are using the $this->withoutMix();
helper in your tests, you should replace this with $this->withoutVite()
:
Vapor
If you are deploying your application to Laravel Vapor, there are a few things you will want to handle before deploying.
Ensure you have updated to at least version 1.40.0
of the Vapor CLI package:
Next, if you are using the Vapor asset helper in your application, you only need to utilize the asset helper when you are referencing assets you don't want bundled, such as those that already live in your public directory.
If you want to use the asset helper with your Vite project, you will also need to ensure you have updated to the latest version:
Then you will need to specify the base URL for assets in your application's entry point, for example in your resources/js/app.js,
like so:
Optional: Configure Tailwind
If you are using Tailwind, perhaps with one of Laravel's starter kits, you will need to create a postcss.config.js
file. Tailwind can generate this for you automatically:
Or, you can create it manually:
If you are using other PostCSS plugins, such as postcss-import
, you will need to include them in your configuration.
Optional: Git ignore the build directory
Vite will place all of your build assets into a build
subdirectory inside your public directory. If you prefer to build your assets on deploy instead of committing them to your repository, you may wish to add this directory to your .gitignore
file:
Optional: Update SSR configuration
You may remove your dedicated Laravel Mix SSR configuration:
In most cases, you won't need a dedicated SSR configuration file when using Vite. You can specify your SSR entry point by passing a configuration option to the Laravel plugin:
You may wish to add the following additional scripts to your package.json
:
If you prefer to build your assets on deploy instead of committing them to your repository, you may wish to add the SSR output directory to your .gitignore
file:
You may start the SSR server using node
:
Optional: Expose Vite port when using Laravel Sail
If you would like to run the npm run dev
command in a Laravel Sail container, you will need to publish a port in your docker-compose.yml
file:
Wrapping up
You should now be able to build your assets using dev
command. This will also invoke the Vite server and Vite will watch for file changes:
Alternatively, if you need to build files without watching or if you need to build them for production, you may use the build
command:
For further information on how to use Vite, please check out the Laravel Vite documentation.
Troubleshooting
If you have followed the upgrade guide, but are still having issues you should try the following steps:
Run
php artisan view:clear
to clear any compiled view assets.If your development web server is running on HTTPS, check out the "Working With A Secure Development Server" section of the documentation.
Migrating from Vite to Laravel Mix
Install Laravel Mix
First, you will need to install Laravel Mix using your npm package manager of choice:
Configure Mix
Create a webpack.mix.js
file in the root of your project:
Update NPM scripts
Update your NPM scripts in package.json
:
Inertia
Vite requires a helper function to import page components which is not required with Laravel Mix. You can remove this as follows:
Update environment variables
You will need to update the environment variables that are explicitly exposed in your .env
files and in hosting environments such as Forge to use the MIX_
prefix instead of VITE_
:
You will also need to update these references in your JavaScript code to use the new variable name and Node syntax:
Remove CSS imports from your JavaScript entry point(s)
If you are importing your CSS via JavaScript, you will need to remove these statements:
Replace @vite
with mix()
@vite
with mix()
You will need to replace the @vite
Blade directive with <script>
and <link rel="stylesheet">
tags and the mix()
helper:
Remove Vite and the Laravel Plugin
Vite and the Laravel Plugin can now be uninstalled:
Next, you may remove your Vite configuration file:
You may also wish to remove any .gitignore
paths you are no longer using:
Last updated