/    Sign up×
Bounties /Pin to ProfileBookmark

Why does Rollup produce larger bundles than Vercel/ncc, and how can this be optimized?

+ 3,000
Copy linkTweet thisAlerts:
Sep 27.2023

Seeing a big size difference in output when bundling with Rollup and Vercel/ncc (Webpack). Rollup is giving me a 14mb file, while Vercel/ncc is just 5mb. I’m using CommonJS and node-resolve plugins and suspect Rollup might be pulling in too much from node_modules. Is there a way or a different approach to fix this?

to post a answer
Full-stack Developer

1 Replies

Davinci•003 auto-replyRollup and Vercel/ncc are both popular tools for bundling JavaScript applications, but they have different philosophies and use cases which can lead to different bundle sizes.

Rollup is designed to create small, efficient bundles by eliminating unused code (also known as tree shaking). However, it includes all dependencies in the bundle by default, which can lead to larger bundle sizes if your project has a lot of dependencies or if those dependencies are large.

Vercel/ncc, on the other hand, is designed to create standalone, self-contained bundles that include only the code that is actually used in your application. This can result in smaller bundles, especially for applications with a lot of dependencies.

If you're finding that Rollup is producing larger bundles than Vercel/ncc, there are a few things you can do to optimize your Rollup configuration:

1. Enable tree shaking: Rollup's tree shaking feature can significantly reduce the size of your bundles by eliminating unused code. You can enable tree shaking by setting the treeshake option to true in your Rollup configuration.


export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
treeshake: true
};


2. Use the rollup-plugin-node-resolve plugin: This plugin tells Rollup how to find external modules in node_modules.


import resolve from 'rollup-plugin-node-resolve';

export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins: [
resolve()
]
};


3. Use the rollup-plugin-commonjs plugin: This plugin converts CommonJS modules to ES6, so they can be included in a Rollup bundle.


import commonjs from 'rollup-plugin-commonjs';

export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins: [
commonjs()
]
};


4. Use code splitting: If your application is large, you can split it into multiple chunks to reduce the size of the initial bundle. This can be done by providing multiple entry points to the input option in your Rollup configuration.


export default {
input: ['src/main.js', 'src/other.js'],
output: {
dir: 'output',
format: 'cjs'
}
};


Remember, the best tool for your project depends on your specific needs and constraints. Both Rollup and Vercel/ncc have their strengths and weaknesses, and it's important to choose the tool that best fits your project.

Was this helpful?

×

Success!

Help @cosmic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...