-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
feat: environment in hooks, context vs param #16261
feat: environment in hooks, context vs param #16261
Conversation
Run & review this pull request in StackBlitz Codeflow. |
AFAIK it doesn't matter for rolldown whether the environment is passed from a param or a context.
I felt the param one to be more aligned with rollup. Rollup has |
In I'm thinking on |
It's outputOptions, which depends on the output format. I felt But I was convinced. It definitely is similar to |
Thanks for checking this out @sapphi-red! Let's merge it so we can keep iterating. We can keep discussing here and later revert if necessary. About rolldown, I think we will end up finding a good way to share the environment. Two thoughts here:
|
In my strategy of parallelizing JS plugins, calling a main thread function from workers synchronously seems to work (sapphi-red/parallel-js-plugin-experiment#4). I'm not sure if it's safe to do it though. 😅But there's at least some hope. /cc @hyf0 For rust plugins and non-parallel JS plugins, sync things should be fine. |
Yes. It's true if using napi-rs to wrap the function to syncify it. But I'm not certainly sure if this solution works well(I knew it works on some cases. We used to use this solution to make |
Even if we release sync APIs as part of |
Description
This PR implements a change discussed with @antfu and @sheremet-va to the way Plugin hooks can access the current Environment. For reference, current implementation and docs for the feature:
Context
Plugin hooks have always been able to know the current environment by the
ssr
flag passed toresolveId
,load
andtransform
hooks. We control dev, so we can pass it directly, and during build, we inject it in rollup by wrapping plugin hooks.Plugin hooks use this boolean flag to access environment specific configuration (for example
config.ssr.resolve.conditions
). To access the deps optimizer associated with the environment (through a weak map against the resolved configgetDepsOptimizer(config, ssr)
, but the optimizer has a internal dep against the server). To trigger processing of imports for warmup (usingserver.warmupRequest(url, { ssr })
). Between other things, where most of them aren't specific to SSR but are conditions due to the need of separating requests from the client and SSR (as in, any other environment will need to do the same, even if not used for rendering in the server).For the Environment API, initially we thought of following the same pattern deprecating the
ssr
boolean flag and replacing it with aenvironment
string options param to hooks. When we implemented this version, it become clear that this forced a coupling of plugins against the whole server. If a plugin needed to accessenvironment.config.resolve.conditons
, then it would have to store theserver
inconfigureServer
and callserver.environments[environment]
in the hook. Same forenvironment.depsOptimizer
,environment.warmupRequest(url)
, etc. We have then an opportunity to reduce the coupling of hooks from the whole server to only the currentenvironment
for most plugins. So instead of passing aenvironment
string, we modified the proposal to directly pass theenvironment
instance. This move simplified a lot of code, and made it a lot more clear thatresolveId
,load
, andtransform
are running against a specific environment (and that the plugin can completely ignore other environments and the server in most cases).Environment instance in hooks: param vs context
We discussed a bit in the last Vite team meeting about how passing the environment instance to hooks could work in Rolldown. I think that for Rolldown, having the environment inside hooks is positive if we want to port vite internal hooks to Rust in the future. If we can share the environment state, then both Rust and JS can access it.
Passing the environment instance as a param to hooks doesn't feel right though. This PR moves the current environment to the
PluginContext
instead. This is more aligned with the way Rollup is designed. So, instead of:Plugins would do:
@sapphi-red @hyf0 opening a PR so we can discuss what is the best move here, so we don't introduce a change that will later on make it difficult to refactor the code to use Rolldown.