I was recently doing a PoC and was surprised to know that dynamic
imports feature works fine in dev
mode but fails in production build without a special configuration. Reason behind this (probably) is that vite
uses esbuild
as bundler for dev
mode while using rollup
as bundler for production
build.
Due to this discrepancy in the behavior between two modes, I am making sure that I always test a new concept in both dev
and production
modes to make sure it works in production
build too otherwise you will end up developing a feature in dev
mode only to realize at a later stage that it is not working in production
mode.
You will need to list all the dynamic imports under rollupOptions
of vite.configs.ts
file in order to make it work in production
mode -
import { defineConfig } from 'vite'
export default defineConfig({
build: {
rollupOptions: {
external: [
"/path/to/external/module.es.js"
]
}
}
})