You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
🚀 Feature Request: Enable code-splitting in the deploy esbuild config #7344
Description
Currently, when deploying a Worker using Wrangler, all code is bundled into a single file. For larger projects, this can result in significant startup delays due to the inclusion of code that may not be needed for every execution path. Dynamic imports could help mitigate this by allowing unused code to be split into separate chunks and loaded only when required. However, Wrangler does not currently support this behavior during deployment.
Use Case
In scenarios where the Worker bundle grows large, startup times can negatively impact performance. Code splitting would allow developers to manage their Worker bundles more efficiently by:
Reducing initial payload size.
Improving execution performance by loading code only when necessary.
Challenges
Although the bundling documentation mentions the additionalModules configuration as a potential workaround, this approach feels more complex than expected for a common use case like code splitting. Having built-in support for code splitting during deployment would simplify workflows significantly.
Proposed Solution
Enable support for ESBuild's code-splitting capabilities in Wrangler. This could involve updating the deploy bundle configuration to:
Automatically detect dynamic imports.
Generate separate chunks for dynamically imported modules.
Ensure the runtime environment supports fetching and executing these chunks.
Benefits
Improved Worker startup times.
Smaller initial bundle sizes.
Easier optimization for projects with modular architecture.
Questions for Maintainers
Are there any technical or runtime constraints preventing this feature from being implemented?
Could this be added as an optional configuration in wrangler.toml for users who want to opt-in to code splitting?
Reacted by Micael Dias, Wendell Misiedjan and John Resig
Fair enough, I didn't realize that in cloudflare/workerd#2372 someone ran a benchmark for code / bundle split workers / pages, and that work needs to happen still to improve performance there.
It sounds like it would still be beneficial to have those perf optimizations in workerd, but until that time, this PR is less relevant: #9240
I'm going to close this for now. It's still blocked on underlying workerd improvements, and going forward we'd generally recommend @cloudflare/vite-plugin as a way to deeply customise your build setup.
Activity
vicb commentedon May 15, 2025
Currently all the code (main bundle + async loaded external modules) are parsed at startup. Code splitting should not show major perf differences.
As explained earlier, this should not make a major difference.
It is possible to tests by building the code with external modules and then uploading it using
--no-bundle.Please report if you see an improvement.
Not sure what this brings, can you expand ?
Can you expand on what you mean here ?
ItsWendell commentedon May 15, 2025
Fair enough, I didn't realize that in cloudflare/workerd#2372 someone ran a benchmark for code / bundle split workers / pages, and that work needs to happen still to improve performance there.
It sounds like it would still be beneficial to have those perf optimizations in workerd, but until that time, this PR is less relevant: #9240
penalosa commentedon Mar 19, 2026
I'm going to close this for now. It's still blocked on underlying workerd improvements, and going forward we'd generally recommend
@cloudflare/vite-pluginas a way to deeply customise your build setup.