Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement shouldTransformCachedModule hook #4341

Merged
merged 5 commits into from Jan 14, 2022

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Jan 6, 2022

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

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

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

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 if true 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 returns true, otherwise transform.

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 the load hook, the loaded code 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 return true.

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.

@github-actions
Copy link

github-actions bot commented Jan 6, 2022

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#should-transform-cached-module

or load it into the REPL:
https://rollupjs.org/repl/?pr=4341

@codecov
Copy link

codecov bot commented Jan 6, 2022

Codecov Report

Merging #4341 (68f8830) into master (da3bb43) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4341   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files         206      206           
  Lines        7350     7350           
  Branches     2088     2088           
=======================================
  Hits         7235     7235           
  Misses         55       55           
  Partials       60       60           
Impacted Files Coverage Δ
src/ModuleLoader.ts 100.00% <ø> (ø)
src/utils/PluginDriver.ts 100.00% <ø> (ø)
src/utils/transform.ts 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update da3bb43...68f8830. Read the comment docs.

@lukastaegert lukastaegert force-pushed the should-transform-cached-module branch 2 times, most recently from 5f55b5d to 6ea5894 Compare January 9, 2022 06:34
@lukastaegert lukastaegert marked this pull request as ready for review January 9, 2022 06:36
@lukastaegert lukastaegert merged commit 1d2ea22 into master Jan 14, 2022
@lukastaegert lukastaegert deleted the should-transform-cached-module branch January 14, 2022 05:15
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.

None yet

1 participant