Skip to content

🚀 Feature Request: Enable code-splitting in the deploy esbuild config #7344

Closed

Description

@dmaretskyi

Describe the solution

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:

  1. Automatically detect dynamic imports.
  2. Generate separate chunks for dynamically imported modules.
  3. 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?

Activity

  1. vicb commented on May 15, 2025

    @vicb
    Contributor

    Benefits

    • Improved Worker startup times.
    • Smaller initial bundle sizes.
    • Easier optimization for projects with modular architecture.

    Currently all the code (main bundle + async loaded external modules) are parsed at startup. Code splitting should not show major perf differences.

    Improved Worker startup times

    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.

    Smaller initial bundle sizes

    Not sure what this brings, can you expand ?

    Easier optimization for projects with modular architecture

    Can you expand on what you mean here ?

  2. added
    awaiting-response:reporter
    package:wrangler
    on May 15, 2025
  3. ItsWendell commented on May 15, 2025

    @ItsWendell
    Contributor

    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

  4. added theissue type on Oct 30, 2025
  5. penalosa commented on Mar 19, 2026

    @penalosa
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting-response:reporterpackage:wrangler

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions