feat: add virtualModules option to deduplicate macro results - #160
Merged
Conversation
Extract macro results into shared virtual modules instead of inlining the serialized value at every usage site, so the same data is emitted only once in the bundle. Closes #130
|
|
commit: |
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
sxzz
approved these changes
Aug 7, 2026
virtualModules option to deduplicate macro results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Currently the macro result is serialized and inlined at every usage site. When a macro returns a large payload (e.g. data fetched at build time) that is used in multiple modules, the same data ends up duplicated in the bundle once per usage.
This PR adds an opt-in
virtualModulesoption (default:false). When enabled, each macro result is extracted into a shared virtual module and usage sites import it instead of inlining the value, so the same data is emitted only once in the bundle.Before (both
a.tsandb.tscontain a full copy of the data):After, with
virtualModules: true:The virtual module is emitted once and shared by all importers.
Linked Issues
Closes #130
Additional context
How it works
virtual:unplugin-macros/<hash>containingexport default <data>. The key is a content hash (sha256, truncated to 16 hex chars), so identical values collapse into a single module regardless of which file uses them._macro_<hash>); the import statement is prepended once per module, even if the value is used multiple times.resolveId/loadhooks are only registered when the option is enabled — with the option off, the plugin output is byte-for-byte identical to the current behavior.$macros$wraphelper is emitted inside the self-contained virtual module instead of the host module.new Functioncontext.export { x } from './macro' with { type: 'macro' }) are still inlined; extending deduplication to them could be a follow-up.Notes
loadthrows a clear error suggesting to clear the cache in that case.