Skip to content

inlineCommonChunks: observable effects to plugins and ecosystem #9

Open

Description

@hyfdev

From the behaviors described in rolldown/rolldown#10693, there are some effects we could reason out:

Behaviors

inlineCommonChunks removes the original common chunk and allows its modules to appear in multiple final chunks, while the runtime still ensures that each module is executed only once.

https://github.com/rolldown/rolldown/blob/5cfde698ca6ec0d481dc3accd1462f9a11f5b320/packages/rolldown/src/types/rolldown-output.ts#L70-L72

Vite CSS plugin

Some reasoning:

  • inlineCommonChunks will only affect vite css behavior if it's enabled. From this aspect, implementing inlineCommonChunk won't introduce any breaking behavior to the current vite css. So the key here is the new behaviors after enabling inlineCommonChunks are positive or negative.
  • Following above reasoning, the css related issues can be handled separate from the implementation.

Investigations

Plain CSS

For main.js -> lib.css -> @import util.css, Vite resolves the CSS @import before chunking. Rolldown sees one empty JavaScript module for lib.css, while Vite stores the compiled lib.css + util.css under that module ID.

When A and B each have their own CSS and also import shared.css:

  1. Without inlineCommonChunks, the module for shared.css stays in a common chunk. Vite emits the shared rules once in a CSS asset used by both A and B.
  2. When inlineCommonChunks selects that common chunk, the module for shared.css appears in both the A and B chunks.
  3. Vite collects CSS by walking each chunk's chunk.modules, so it writes the shared rules into both A's and B's CSS assets.

The separate shared CSS request disappears, but the shared rules are duplicated. If both A and B are loaded, loading the later CSS asset can change which rule wins in the cascade. For this common chunk, no JavaScript output is removed: Vite already deletes its empty JavaScript output.

Size issue

inlineCommonChunks.maxSize measures the transformed module source, not the stylesheet stored by Vite. A plain CSS module therefore contributes zero bytes even when its stylesheet is large. A pure CSS common chunk falls below any positive maxSize; in a mixed chunk, the CSS still contributes zero bytes.

A workaround would be the css plugin just generate enough placeholders to make the generated proxy js module have the same size of the css content.

A real solution would be allow plugins delcare a module's logic size so rolldown will use it for calculation instead of using original source's size.

CSS Modules

A shared.module.css import also becomes one Rolldown module. Its generated JavaScript exports the class-name map, while Vite stores its stylesheet separately under the same module ID.

When A and B each have their own CSS and also import shared.module.css, selecting its common chunk puts the class-name-map code in both JavaScript chunks, and Vite writes the shared rules into both CSS assets. maxSize counts the class-name-map source but not the stylesheet, so a large stylesheet with a small class-name map can still be treated as small.

With cssCodeSplit: false, the copied rules are repeated in the single output stylesheet, so no CSS request disappears.

Effects

  • css chunks are affected by inlineCommonChunks becuase the js proxy modules are affected by inlineCommonChunks.
  • For pure css chunk(chunk that only contains js proxy modules), it would be duplicate into their consumer entries. Vite css will extract them into corresponding entry css chunk. If there are not entry css chunks, themselves will become entry css chunks.
  • For non-pure css chunks(chunk that contains normal js modules and proxy modules), if they got inlined, js modules are processed normally, js proxy modules are processed the same as pure css chunks.
  • What inlineCommonChunks brings to Vite css is positve (declaring the proxy module size correctly)
    • inlineCommonChunks can reduce common css chunks(css modules shared by multiple entries)

Open questions

How to make inlineCommonChunks not to touch pure css chunks or chunks that will render css assets? Or do we want to?

rolldown-plugin-dts

#9 (comment)

Activity

  1. hyfdev commented on Aug 23, 2026

    @hyfdev
    MemberAuthor

    For reference, here is how inlineCommonChunks can affect CSS loading and cascade order with cssCodeSplit: true.

    Consider two dynamically loaded consumers that each have their own CSS and also import a shared stylesheet:

    // main.js
    await import('./A.js')
    await import('./B.js')
    
    // A.js
    import './A.css'
    import './shared.css'
    
    // B.js
    import './B.css'
    import './shared.css'

    Without inlineCommonChunks, Vite emits three CSS assets:

    A.css      contains A's rules
    B.css      contains B's rules
    shared.css contains the shared rules
    

    shared.css is loaded once. When the second consumer is loaded, Vite sees that the same CSS URL is already present and does not load it again.

    When inlineCommonChunks selects the pure CSS common chunk, the module for shared.css appears in both final JavaScript chunks. Vite collects CSS from each chunk's chunk.modules, so the output becomes:

    A.css contains A's rules and the shared rules
    B.css contains B's rules and the shared rules
    

    The separate shared.css request disappears, but its rules are transferred and parsed again when the second consumer is loaded. Because the second consumer's CSS asset is appended later, its copy of the shared rules also gets a later position in document order and can affect the cascade when declarations otherwise have equal precedence. Vite cannot deduplicate this at load time because A.css and B.css are different URLs.

    Webpack has the same behavior with mini-css-extract-plugin. When shared CSS stays in the consumer chunks, its rules are emitted into both consumer CSS assets. When SplitChunks extracts it into a common CSS chunk, the shared stylesheet is loaded once instead.

    The CSS cascade is standardized, but the mapping from JavaScript imports and JavaScript chunks to emitted CSS assets is not. This behavior follows naturally from extracting CSS per final chunk and is not specific to Rolldown.

  2. hyfdev commented on Aug 24, 2026

    @hyfdev
    MemberAuthor

    For reference, selecting a "dts" chunk with inlineCommonChunks is a correctness failure for the current rolldown-plugin-dts, not only a change in output layout.

    The plugin stores each original declaration AST in an internal map and gives Rolldown fake, top-level JavaScript that preserves only its bindings and references. After chunking, its renderChunk hook recognizes that top-level shape and turns it back into .d.ts syntax. The mechanism is described in the plugin's fake-JS implementation.

    When inlineCommonChunks selects a shared dts chunk, its fake JavaScript is moved into a runtime factory. The resulting JavaScript has this shape:

    __share("rd:0", (module, exports, define) => {
      var [User] = [2, () => [], ["", ""]]
      define(exports, { t: () => User })
    })
    var share_types_d = __share_require("rd:0")
    
    var [ApiResponse] = [0, () => [share_types_d.t], ["", ""]]

    The current renderChunk removes expression statements, including the complete __share(...) factory. It also removes variable declarations that are not declaration encodings, including share_types_d. It still reconstructs ApiResponse:

    export interface ApiResponse {
      data: share_types_d.t
    }

    The shared declaration is gone and share_types_d does not exist, so the generated declaration bundle is invalid.

    This is simpler to avoid than the Vite CSS interaction. Declaration entries form a separate dependency graph, so an automatic dts chunk containing a declaration module can be excluded as a whole. For example:

    inlineCommonChunks: {
      maxSize: 10_000,
      exclude: /\.d\.([cm]?)ts$/,
    }

    exclude should match module IDs and keep the whole candidate common chunk when any module matches. rolldown-plugin-dts can add this pattern in its outputOptions hook, keeping declaration encodings out of runtime factories without requiring users to configure it manually.

  3. overlookmotel commented on Aug 26, 2026

    @overlookmotel

    CSS

    Broad thought

    I don't know the historical reasons why CSS is dealt with this way. My guess is maybe Vite had to get what it needed from JS bundlers (Rollup, ESBuild) which it didn't control. Those bundlers dealt only with JS files, so Vite had to pretend CSS was JS.

    My suspicion is that this approach doesn't entirely make sense any more. Vite and the bundler it uses (Rolldown) are now part of the same "family", so it'd now be feasible for the bundler to make CSS a first class citizen, and treat it with the correct semantics for CSS - which aren't the same as for JS.

    It sounds to me like what we're missing is a kind of "strict execution order for CSS" or "strict cascade order".

    I'm guessing the ultimate solution for these CSS problems will lie along these lines. However, obviously that's wildly out of scope for what we're talking about now (inlineCommonChunks), so for now we need to figure out how to make the current situation work.

    The danger of surprise

    We know that inlining chunks produces execution order hazards. But we have a tool to deal with that - strict execution order.

    We have no such tool to deal with CSS.

    If CSS participates in inline chunks, it can lead to CSS cascade order problems.

    But that ISN'T the problem! The real problem is that the user can't control it, can't work around it, can't know when this problem will strike.

    Here's an example:

    • Shared chunk X contains JS code and CSS.
    • The chunk is larger than the inline maxSize, so the CSS is in a normal shared chunk.
    • A change is made to the JS code in one of the modules in that chunk.
    • That change reduces the chunk's size to below maxSize.
    • Now the chunk is inlined in multiple places, and the CSS is duplicated.
    • This alters the CSS cascade order.
    • Suddenly the website's styles are broken.

    This would be incredibly confusing to the user. They'll assume the cause is related to some CSS change, and go hunting for the bug. The last thing they'd expect is that a totally unconnected change to some JS screwed up the styles, so they'll be looking in all the wrong places. And even if they do manage to track it down, how do they fix it?

    What to do?

    In my opinion, we need to find a way to exclude CSS from chunk inlining.

    I don't know what this looks like in practice, but maybe something like:

    • JS in the shared chunk gets duplicated and inlined.
    • CSS remains in a normal statically-linked shared chunk.
    • Vite deals with CSS in usual way.
    • Vite recognises that the JS chunk is empty of any JS content, and removes it.

    In the end, CSS can benefit from duplication/inlining, same as JS can. But IMO until we have some way to produce "strict cascade order", it feels like a recipe for really horrible DX.

    More broadly: The problem space of the inlineCommonChunks feature is already broad, and I imagine we're going to hit a multitude of weird bugs and edge cases. Bringing CSS into the mix too early IMO widens what's already hard into really hard. I propose: Let's go for the big win of eradicating small JS chunks first, and defer dealing with CSS.

    (All the above is just my opinion, based on little knowledge of how Vite/Rolldown handle CSS, so I may have completely missed the point. Apologies if so.)

  4. overlookmotel commented on Aug 26, 2026

    @overlookmotel

    rolldown-plugin-dts

    Again, this is a situation where the starting point is not ideal, and we have to work around it. rolldown-plugin-dts is a completely insane hack! (no disrespect to the author, the insanity is inherited from the Rollup plugin)

    Can I just check one thing:

    Declaration entries form a separate dependency graph, so an automatic dts chunk containing a declaration module can be excluded as a whole. For example:

    inlineCommonChunks: {
      maxSize: 10_000,
      exclude: /\.d\.([cm]?)ts$/,
    }

    Are you saying that because it's a separate dependency graph, the exclude here will have no effect whatsoever on the JS bundles?

    Is there any other circumstance in which exclude option would be useful? Or are we adding this option purely to support rolldown-plugin-dts? If the latter, can we make it unstable or something, so we can remove it later when a different approach to DTS bundling becomes available?

  5. sapphi-red commented on Aug 27, 2026

    @sapphi-red
    Member

    But that ISN'T the problem! The real problem is that the user can't control it, can't work around it, can't know when this problem will strike.

    It is probably possible to workaround using CSS layers, although it can require many @layers.

  6. hyfdev commented on Aug 27, 2026

    @hyfdev
    MemberAuthor

    Or are we adding this option purely to support rolldown-plugin-dts? If the latter, can we make it unstable or something, so we can remove it later when a different approach to DTS bundling becomes available?

    @overlookmotel Yes. The issue is only show we have a solution for it, but it's not something settled.

    And just to tease you, we hard-code minify pass' logic to skip minifying the chunk ends with .d.ts. (Seems we could do the same lol

  7. hyfdev commented on Aug 27, 2026

    @hyfdev
    MemberAuthor

    For the CSS, I would say any complaint to the current bundle model(rolldown + webpack) is right and we should improve it. However, it's a separate step. In the scope of inlineCommonChunks + css, I would consider there is no fundamental blocker for this area. Things that need to be done are pretty clear.

    inlineCommonChunks is not trying to invent a new css bundle model that's wrong, it just inherits what current has. It does not introduce any new negative situation as the situation already existed.


    @overlookmotel I think what you said is resonable, just to add more context

    But that ISN'T the problem! The real problem is that the user can't control it, can't work around it, can't know when this problem will strike.

    This situation already happened in the current css bundling model no matter in vite/webpack/turbopack. It's not something new that inlineCommonChunks introduced.

    In my opinion, we need to find a way to exclude CSS from chunk inlining.

    I'm ok with this. We could provide a option or method to allow users to disable inlineCommonChunks from touching css chunks if they think the behavior is negative.

  8. hyfdev commented on Aug 27, 2026

    @hyfdev
    MemberAuthor

    Thought:

    It seems we could always split the mix-up chunk into a pure css chunk and js chunk. In this case, enabling inlineCommonChunks or not won't have difference in the css output.

  9. overlookmotel commented on Aug 28, 2026

    @overlookmotel

    We hard-code minify pass' logic to skip minifying the chunk ends with .d.ts.
    Seems we could do the same.

    Personally I think hard-coded logic for .d.ts (ugly though that is) might be preferable to adding a public option that's really only there to support rolldown-plugin-dts. If we add that option, we have to support it (for whatever weird use cases people find for it).

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

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions