How to allow json files to not be bundled in vite?

by elise_daugherty , in category: Javascript , 2 months ago

How to allow json files to not be bundled in vite?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 2 months ago

@elise_daugherty 

To allow json files to not be bundled in Vite, you can use the exclude option in the build configuration in your vite.config.js file. Here's an example of how to exclude JSON files from being bundled:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// vite.config.js

import { defineConfig } from 'vite'

export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.endsWith('.json')) {
            return 'json-files';
          }
        },
      },
    },
    chunkFileNames: 'chunks/[name]-[hash].js',
  }
})


In the above configuration, JSON files are explicitly excluded from being bundled using the manualChunks function. Any JSON files will be placed in a separate chunk called json-files. Additionally, the chunkFileNames option is used to specify a custom output path for the chunks.


By defining the manualChunks function in the rollupOptions of the build configuration, you can control how certain files are bundled and exclude JSON files from the main bundle in Vite.