Implement shouldTransformCachedModule hook #4341
Merged
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.
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
rollup/plugins#1038
Description
Handling caching can be taxing when a plugin heavily relies on not only transforming in the
transform
hook but also uses the hook to gather data about a module that may affect how other modules are rendered (in the example of rollup/plugins#1038, a wrapped module needs to be imported differently from a "normal" module).The proposed hook will be called after the
load
hook if a module has a cached copy. It will allow plugins to opt-in to transforming a cached module, basically discarding the cached copy iftrue
is returned, as well as give access to a cached module's meta data.@patak-dev, @antfu, @marvinhagemeister, @yyx990803 I suppose this new hook would not be relevant for WMR/Vite anyway as I assume you do not skip the transform hook for modules the way Rollup cache does it. Still tagging you so that you can have a look for yourself.
Will keep this open for a few days anyway so that I can test if this solves the issues I have in rollup/plugins#1038.
documentation (see also changed files in PR):
shouldTransformCachedModule
Type:
({id: string, code: string, ast: ESTree.Program, meta: {[plugin: string]: any}, moduleSideEffects: boolean | "no-treeshake", syntheticNamedExports: string | boolean}) => boolean
Kind:
async, first
Previous Hook:
load
where the cached file was loaded to compare its code with the cached version.NextHook:
moduleParsed
if no plugin returnstrue
, otherwisetransform
.If the Rollup cache is used (e.g. in watch mode or explicitly via the JavaScript API), Rollup will skip the
transform
hook of a module if after theload
hook, the loadedcode
is identical to the code of the cached copy. To prevent this, discard the cached copy and instead transform a module, plugins can implement this hook and returntrue
.This hook can also be used to find out which modules were cached and access their cached meta information.
If a plugin does not return
true
, Rollup will trigger this hook for other plugins, otherwise all remaining plugins will be skipped.