Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do multiple bundles with vite?

Using vite js to bundle my library, I need to provide two versions at the same time:

  • production usage
  • development specific code and warnings with devtools integration.

When I was using webpack, I had:

module.exports = [
  defaultUmdBuild("production"),
  defaultUmdBuild("development"),
];

which outputs two files and then I have this entrypoint to my library:

'use strict';

if (process.env.NODE_ENV === 'production') {
  module.exports = require('./mylib.production.js');
} else {
  module.exports = require('./mylib.development.js');
}

How to do the same using vite ?

Thanks.

like image 269
Incepter Avatar asked Jan 25 '26 08:01

Incepter


2 Answers

For people coming here for an answer,

Here is what I ended up doing (Please edit if this ever changes)

In package.json:

"build": "tsc && vite build --config vite.config.lib.dev.ts && vite build --config vite.config.lib.prod.ts"

And then define your both files accordingly.

like image 193
Incepter Avatar answered Jan 26 '26 23:01

Incepter


I think you can achieve this using vite modes.

Run the build command using different modes:

vite build --mode production #default
vite build --mode development

In your vite.config file you can then have different build configurations based on the mode value.

// vite.config.js
import { defineConfig } from 'vite';

export default defineConfig(({ mode }) => {
  if (mode === 'production') {
    return {
      // ...
      build: {
        outDir: 'build_production'
      }
    }
  }

  if (mode === 'development') {
    return {
      // ...
      build: {
        outDir: 'build_development'
      }
    }
  }
  return {}
});

like image 45
Buhanov Avatar answered Jan 26 '26 21:01

Buhanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!