Search code examples
viterollupjs

How to override the rollup output.format setting in vite?


I'm trying to solve the Vite build error I get:

RollupError: Invalid value "iife" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.

The file name reported with this error points to my web worker code, so I assumed that this setting belongs to the worker section in vite.config.ts:

import { defineConfig } from "vite";
import preact from "@preact/preset-vite";
import basicSsl from "@vitejs/plugin-basic-ssl";

import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
import rollupNodePolyFill from "rollup-plugin-node-polyfills";

export default defineConfig({
    plugins: [
        preact(),
        basicSsl(),
    ],
    server: {
        port: 3001,
        https: true,
    },
    optimizeDeps: {
        esbuildOptions: {
            // Node.js global to browser globalThis
            define: {
                global: "globalThis",
            },
            // Enable esbuild polyfill plugins
            plugins: [
                NodeGlobalsPolyfillPlugin({
                    process: true,
                    buffer: true,
                }),
                NodeModulesPolyfillPlugin(),
            ],
        },
    },
    worker: {
        rollupOptions: {
            output: {
                format: "esm",
            },
        },
    },
    build: {
        rollupOptions: {
            plugins: [
                // Enable rollup polyfills plugin
                // used during production bundling
                rollupNodePolyFill(),
            ],
            output: {
                format: "esm",
            },
        },
    },
});

Additionally, I set the output format in the build rollup options. However, neither of the two settings are applied and I still get the said error.

What is the correct way to change the rollup output format setting in Vite?


Solution

  • The worker output format must be specified directly in the worker config key, not its rollup options:

    import { defineConfig } from "vite";
    import preact from "@preact/preset-vite";
    import basicSsl from "@vitejs/plugin-basic-ssl";
    
    import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
    import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
    import rollupNodePolyFill from "rollup-plugin-node-polyfills";
    
    export default defineConfig({
        plugins: [
            preact(),
            basicSsl(),
        ],
        server: {
            port: 3001,
            https: true,
        },
        optimizeDeps: {
            esbuildOptions: {
                // Node.js global to browser globalThis
                define: {
                    global: "globalThis",
                },
                // Enable esbuild polyfill plugins
                plugins: [
                    NodeGlobalsPolyfillPlugin({
                        process: true,
                        buffer: true,
                    }),
                    NodeModulesPolyfillPlugin(),
                ],
            },
        },
        worker: {
            format: "es",
        },
        build: {
            rollupOptions: {
                plugins: [
                    // Enable rollup polyfills plugin
                    // used during production bundling
                    rollupNodePolyFill(),
                ],
                output: {
                    format: "esm",
                },
            },
        },
    });