adriansio's avatar

Migrating from Mix to Vite, can't disable chunk/module splitting

Vite is really fast but it's been a headache to set up. I don't want Vite to split my JS files in different modules because some of these files are embedded on my users website and they must be "self-contained" with all their dependencies.

Here's my vite.config.js:

import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/sass/styles.scss',
                'resources/js/app.js',
            ],
            buildDirectory: 'build',
            refresh: true
        }),
    ],
    build: {
        manifest: true,
        rollupOptions: {
            input: {
                'styles.css': 'resources/sass/styles.scss',

                'js/app': 'resources/js/app.js',
                'js/widget-1': 'resources/js/widget-1.js', // These files import a common module and I don't want it separated
                'js/widget-2': 'resources/js/widget-2.js', // These files import a common module and I don't want it separated
            },
            output: {
                entryFileNames: `[name].js`,
                chunkFileNames: `[name].js`,
                assetFileNames: `assets/[name].[ext]`,
            }
        }
    }
})

This is part of the output generated by npm run build:

...
// I don't want this
public/build/_commonjsHelpers.js              0.69 kB │ gzip:   0.38 kB
// I don't want this
public/build/es6-promise.js                   6.99 kB │ gzip:   2.86 kB 
// I don't want this
public/build/module.esm.js                   41.60 kB │ gzip:  15.17 kB
// I don't want this
public/build/confetti.module.js              43.62 kB │ gzip:  15.64 kB
public/build/js/widget-2.js                 66.10 kB │ gzip:  15.43 kB
public/build/js/widget-1.js                    82.75 kB │ gzip:  18.06 kB
public/build/js/app.js                    1,962.32 kB │ gzip: 414.66 kB

I feel like it's a really normal scenario to want to disable code splitting, yet I've been spending 2 hours without finding any solution. Is my only option to switch back to Mix?

0 likes
7 replies
adriansio's avatar

Is this such an uncommon situation? I am suprised. I think my only option is to switch back to Laravel Mix.

sidneygijzen's avatar

By accident bumped into this SO answer about disabling chunking and remembered your question. Hopefully it helps or provides a clue on where to look.

adriansio's avatar

@sidneygijzen Not working unfortunately. I wish I could just switch to Vite and maintain my configuration.

vlamy's avatar

Had the same problem. This helped

export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        manualChunks: () => 'everything.js',
      },
    },
  },
});

Please or to participate in this conversation.