Skip to content

fix(terser): share nameCache between chunks by serializing minify tasks - #2020

Open
itsjamie wants to merge 1 commit into
rollup:masterfrom
itsjamie:fix/terser-name-cache-chunks
Open

fix(terser): share nameCache between chunks by serializing minify tasks#2020
itsjamie wants to merge 1 commit into
rollup:masterfrom
itsjamie:fix/terser-name-cache-chunks

Conversation

@itsjamie

Copy link
Copy Markdown

Rollup Plugin Name: terser

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)

Breaking Changes?

  • no

List any relevant issue numbers:

resolves #1970

Description

The plugin already returns the mutated nameCache from its workers and merges it back, but the merge never takes effect across chunks. Rollup calls renderChunk for every chunk concurrently, so each chunk's options are serialized with the still-empty cache before any other chunk's cache comes back. maxWorkers: 1 doesn't help because the pool dispatches the next queued task synchronously from the worker's message handler, before the awaiting merge-back microtask runs. The result is the divergence in #1970: the same method mangled to .o() in one chunk and .m() in another.

Sharing a name cache only works if each minify() call sees the names the previous call assigned, so when nameCache is provided the minify tasks now run through a promise chain: each chunk's options are snapshotted and serialized only after the previous chunk's cache has been merged back. Builds without a nameCache keep the current parallel behaviour.

Tests cover a shared cache across two entries importing a common module (the shape from the issue) and across two unrelated entries, each with default workers and with maxWorkers: 1. Two more tests pin the surrounding behaviour: without a nameCache unrelated chunks still mangle independently, and with one the build uses a single worker even when maxWorkers: 4 is set.

A side effect worth a second look: a shared nameCache now effectively serializes the whole build onto one worker. I documented that in the README rather than emitting a runtime warning. Open to thoughts on whether it deserves more than a docs note.

@itsjamie
itsjamie requested a review from tada5hi as a code owner August 10, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

plugin-terser does not share nameCache between minify invocations when chunking across multiple entrypoint and common files

1 participant