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 new APIs for inter-plugin communication #3807
Conversation
Thank you for your contribution! ❤️You can try out this pull request locally by installing Rollup via
or load it into the REPL: |
Excited to see this so there's finally a blessed and much-safer approach to solving #2662! |
So can I mark this as closing #2662? |
One hope I have is to use this in the commonjs plugin for two things:
|
BTW if someone has suggestions for how to improve documentation, this is more than welcome. This PR includes a HUGE docs update that would really benefit from some reviewing. |
Codecov Report
@@ Coverage Diff @@
## master #3807 +/- ##
==========================================
+ Coverage 97.03% 97.05% +0.01%
==========================================
Files 185 185
Lines 6477 6482 +5
Branches 1876 1877 +1
==========================================
+ Hits 6285 6291 +6
Misses 101 101
+ Partials 91 90 -1
Continue to review full report at Codecov.
|
4844742
to
ec46fdc
Compare
docs/05-plugin-development.md
Outdated
} | ||
``` | ||
|
||
When triggering this hook from a plugin via [`this.resolve(source, importer, options)`](guide/en/#thisresolvesource-string-importer-string-options-skipself-boolean--promiseid-string-external-boolean--null), it is possible to pass a custom options object to this hook. While this object will be passed unmodified, plugins should follow the convention of adding a `custom` property with an object where the keys correspond to the names of the plugins that the options are intended for. For details see **TODO: Section about custom parameter passing**. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we show an example of how this custom
property would look like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would place this section more to the bottom, so that first the hook return values are explained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we show an example of how this custom property would look like
With the proper link here, maybe the example is not needed?
Co-authored-by: Lars den Bakker <larsdenbakker@gmail.com>
Co-authored-by: Lars den Bakker <larsdenbakker@gmail.com>
Co-authored-by: Lars den Bakker <larsdenbakker@gmail.com>
Co-authored-by: Lars den Bakker <larsdenbakker@gmail.com>
… info, make module info lazy.
0ce01ea
to
c99cf12
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some top quality API work, great to see this finally!
The only thing worth thinking about a little might be how this affects caching. Ideally we should encourage that the metadata is cached by default in the cache model, I didn't check if that is handled here or not though.
88f0c6b
to
75bad86
Compare
Thanks! Caching is indeed an important point, otherwise we might run into trouble once transform hooks are skipped that add essential meta data. Therefore, the metadata is properly cached and restored, which is also tested. I was hoping to utilize this to make the commonjs plugin cacheable at last, but as it turns out, this will still require another hook that is run for each module, cached or not, once Rollup has finished parsing them and all meta data is in their final state. I am actually working on this in another upcoming PR because I think this might be useful to others as well. |
@lukastaegert I was trying to add an unrelated new feature to latest master but found that |
Not really, because for me, I do no see a change (also a Mac, though with macos 10.15). I remember seeing something like this once when my battery was nearly dead. Could not reproduce ever since, though. If this is reproducible for you, it would be interesting to gain more insights. Like attaching a browser for debugging ( |
Battery is not an issue. The commit immediately preceding this one is fast. All subsequent commits are slow. |
It was a stale node_modules issue. Once reinstalled the speed is fast once again. This commit must have exercised a perf bug in a dependency for the first time that was subsequently fixed. |
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
Resolves #2662
Description
This is in response to various issues with the node-resolve and commonjs plugin and will provide plugins with a way to pass additional information to resolve as well as a way to annotate modules with custom meta-informaiton. See the attached documentation update, for reference:
Inter-plugin communication
At some point when using many dedicated plugins, there may be the need for unrelated plugins to be able to exchange information during the build. There are several mechanisms through which Rollup makes this possible.
Custom resolver options
Assume you have a plugin that should resolve a module to different ids depending on how it is imported. One way to achieve this would be to use special proxy ids when importing this module, e.g. a transpiled import via
require("foo")
could be denoted with an idfoo?require=true
so that a resolver plugin knows this.The problem here, however, is that this proxy id may or may not cause unintended side-effects when passed to other resolvers. Moreover, if the id is created by plugin
A
and the resolution happens in pluginB
, it creates a dependency between these plugins so that oneA
is not usable withoutB
.Custom resolver option offer a solution here by allowing to pass additional options for plugins when manually resolving a module. This happens without changing the id and thus without impairing the ability for other plugins to resolve the module correctly if the intended target plugin is not present.
Note the convention that custom options should be added using a property corresponding to the plugin name of the resolving plugin. It is responsibility of the resolving plugin to specify which options it respects.
Custom module meta-data
Plugins can annotate modules with custom meta-data which can be accessed by themselves and other plugins via the
resolveId
,load
, andtransform
hooks. This meta-data should always be JSON.stringifyable and will be persisted in the cache e.g. in watch mode.Note the convention that plugins that add or modify data should use a property corresponding to the plugin name, in this case
annotating
. On the other hand, any plugin can read all meta-data from other plugins viathis.getModuleInfo
.If several plugins add meta-data or meta-data is added in different hooks, then these
meta
objects will be merged shallowly. That means if pluginfirst
adds{meta: {first: {resolved: "first"}}}
in the resolveId hook and{meta: {first: {loaded: "first"}}}
in the load hook wile pluginsecond
adds{meta: {second: {transformed: "second"}}}
in thetransform
hook, then the resultingmeta
object will be{first: {loaded: "first"}, second: {transformed: "second"}}
. Here the result of theresolveId
hook will be overwritten by the result of theload
hook as the plugin was both storing them under itsfirst
top-level property. Thetransform
data of the other plugin on the other hand will be placed next to it.Direct plugin communication
For any other kind of inter-plugin communication, we recommend the pattern below. Note that
api
will never conflict with any upcoming plugin hooks.