Search code examples
vue.jsnpmvite

Skipping larger chunks while running "Npm run build"


Facing this problem while trying to run "npm run build"

(!) Some chunks are larger than 500 KiB after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/guide/en/#outputmanualchunks
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.

Solution

  • If you don't want to increase the chunkSizeWarningLimit and instead focus on solving the actual size issue, try this:

    export default defineConfig({
    ....
    build: {
            rollupOptions: {
                output:{
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            return id.toString().split('node_modules/')[1].split('/')[0].toString();
                        }
                    }
                }
            }
        }
    });
    

    This will automatically make separate chunks for each "vendor" in node_modules. For example, @emotion/react would go in an emotion chunk and react-dom would go in a react-dom chunk.

    One caveat is that you need to manually merge chunks for packages with peer dependencies (just add a couple if statements). The reason for this is that there is currently no way to set the order chunks are loaded. For example, @mui/material depends on react, so they need to be placed in the same chunk.