Skip to content

[Feature Request]: skip barrel files when all imports in the barrel files are marked as side effect free #8920

Description

@sapphi-red

What problem does this feature solve?

graph LR                                                                                                                                              
    entry-light["entry-light.js"] -->|"import { light }"| barrel["barrel.js"]
    entry-heavy["entry-heavy.js"] -->|"import { createService }"| barrel                                                                                
                                                                                                                                                            barrel -->|"import { HeavyService }"| heavy["heavy.js"]                                                                                             
    barrel -->|"export { light }"| light["light.js"]                                                                                                    

    light:::leaf
    heavy:::leaf

    classDef leaf fill:#e8f5e9,stroke:#388e3c
Loading

https://github.com/sapphi-red-repros/barrel-split-export-repro

For example, if heavy.js is marked as sideeffect free, we can ignore that import when bundling light.js. So we can have with 2 chunks like Rollup does. But Rolldown now generates 3 chunks: one for entry-light, one for entry-heavy, one for the shared barrel file.

What does the proposed API look like?

Not an API change

Although I'm not sure how the other bundlers define "sideEffect-free" for modules. We need to change this definition to do this optimization.

While you can mark individual expressions or functions, you can also mark entire modules as side-effect-free. If you mark a module as side-effect-free, Rolldown will treat every statement in that module as side-effect-free when none of its exports are used.
https://rolldown.rs/in-depth/dead-code-elimination#marking-entire-modules-as-side-effect-free:~:text=While%20you%20can%20mark%20individual%20expressions%20or%20functions%2C%20you%20can%20also%20mark%20entire%20modules%20as%20side%2Deffect%2Dfree.%20If%20you%20mark%20a%20module%20as%20side%2Deffect%2Dfree%2C%20Rolldown%20will%20treat%20every%20statement%20in%20that%20module%20as%20side%2Deffect%2Dfree%20when%20none%20of%20its%20exports%20are%20used.


Originally reported at vitejs/vite#21966

Metadata

Metadata

Fields

Priority

None yet

Start date

None yet

Target date

None yet

Effort

None yet

Projects

Status
Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions