Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rg83's avatar
Level 4

Minify livewire.js

I need some help to try and minify my livewire.js that is generated when in production similar to the default app.js using vite.

0 likes
2 replies
LaryAI's avatar
Level 58

To minify your livewire.js when using Vite, you can follow these steps:

  1. Ensure that you are running Vite in production mode. When you build your assets for production, Vite automatically minifies your JavaScript files. You can do this by running the build command:
npm run build

or

yarn build

This command should be defined in your package.json file under the scripts section and typically looks like this:

"scripts": {
  "build": "vite build"
}
  1. If you have a custom setup or need to include livewire.js specifically, you can import it in your main JavaScript file and let Vite handle the minification as part of the build process. For example, in your main.js or app.js file, you can add:
import 'alpinejs'
import Livewire from 'laravel-livewire'

// Any other imports or custom code

Livewire.start()
  1. Make sure that your vite.config.js file is properly set up to minify JavaScript files. Vite uses esbuild or terser for minification by default, so you shouldn't need to add any custom configuration for minification.

Here's an example of a basic vite.config.js file:

import { defineConfig } from 'vite';

export default defineConfig({
  // Your Vite configuration
  build: {
    // Other build options
    minify: 'terser' // or 'esbuild', which is the default
  }
});
  1. If you are loading livewire.js separately and not bundling it with your other JavaScript assets, you can use Vite's build.lib configuration to build it as a library and minify it. Here's an example configuration:
import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    lib: {
      entry: 'path/to/your/livewire.js',
      name: 'Livewire',
      fileName: (format) => `livewire.${format}.js`
    },
    rollupOptions: {
      // Externalize deps that shouldn't be bundled into your library
      external: ['alpinejs'],
      output: {
        // Provide global variables to use in the UMD build for externalized deps
        globals: {
          alpinejs: 'Alpine'
        }
      }
    }
  }
});

Remember to replace 'path/to/your/livewire.js' with the actual path to your livewire.js file if you are using this approach.

By following these steps, your livewire.js should be minified along with your other assets when you build your project for production using Vite.

bluedreamer's avatar

If APP_DEBUG is set to false a minified version of livewire will be used

            return config('app.debug')
                ? Route::get('/livewire/livewire.js', $handle)
                : Route::get('/livewire/livewire.min.js', $handle);

Please or to participate in this conversation.