Transitioning from Laravel Mix to Vite

Karl Hill
2 min readDec 21, 2022

--

If you’re a Laravel developer, you’re probably familiar with Laravel Mix, the popular build tool for compiling and optimizing assets in Laravel projects. However, if you’re looking for a faster and more efficient build tool, you may want to consider transitioning to Vite.

Vite is a lightweight, fast, and easy-to-use build tool specifically designed for modern JavaScript development. It uses native ES modules and has a simple configuration file, making it easy to start and maintain.

One of the most significant benefits of Vite is its speedy build times. It uses a hot module replacement (HMR) system that allows you to see changes in your code almost instantly without needing a complete build. This can save you time and frustration when developing and debugging your projects.

If you’re ready to switch from Laravel Mix to Vite, it’s important to note that the two tools use different configuration files and have other APIs. However, the Vite documentation provides clear and detailed instructions on migrating your existing projects so that you can get up and running with Vite in no time.

To migrate your Laravel Mix project to Vite, you’ll need to follow these steps:

  1. Install Vite: Run npm install -D vite to install Vite as a dev dependency in your project.

2. Create a vite.config.js file: In the root of your project, create a new file named vite.config.js and add the following code:

import { defineConfig } from 'vite';

export default defineConfig({
// Vite configuration options go here
});

3. Configure your build process: In the defineConfig Object, you'll need to specify how Vite should handle your assets. For example, to compile and minify your CSS and JavaScript files, you can use the following configuration:

import { defineConfig } from 'vite';
import postcss from 'rollup-plugin-postcss';
import postcssImport from 'postcss-import';
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';

export default defineConfig({
rollupInputOptions: {
input: 'resources/js/app.js',
plugins: [
postcss({
plugins: [
postcssImport(),
tailwindcss(),
autoprefixer(),
cssnano(),
],
}),
],
},
rollupOutputOptions: {
file: 'public/js/app.js',
format: 'iife',
},
});

4. Update your package scripts: In your package.json file, update the scripts section to use Vite instead of Laravel Mix. For example:

"scripts": {
"dev": "vite",
"build": "vite build"
},

5. Start the development server: Run npm run dev to start the development server and begin using Vite in your project.

Following these steps, you can easily transition your Laravel Mix project to Vite and enjoy the benefits of faster build times and a simpler configuration process.

--

--

Karl Hill

Full Stack Engineer at NASA. Volunteer at Smithsonian’s The Renwick Gallery, and former drummer of #GovernmentIssue. https://www.karlhill.com