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

feat: resolved createEnvironment #17791

Merged
merged 5 commits into from
Jul 30, 2024

Conversation

patak-dev
Copy link
Member

Description

Environment options dev.createEnvironment and build.createEnvironment were not resolved in the config stage. They remained undefined, and later the default was set in the builder and server while creating the environments. This PR requires both in the ResolvedConfig, and forces us to properly define their type. dev.createEnvironment was not taking the third setup parameter (so the watcher was not received by custom environments or for the client and ssr envs when defining a custom createEnvironment).

Now that is exposed, we may need a better name than setup for this parameter.

Copy link

stackblitz bot commented Jul 29, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Comment on lines 240 to 248
function createDefaultDevEnvironment(
name: string,
config: ResolvedConfig,
context: CreateDevEnvironmentContext,
): DevEnvironment {
return config.environments[name].consumer === 'client'
? createDefaultClientDevEnvironment(name, config, context)
: createDefaultSsrDevEnvironment(name, config, context)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, we were creating a client-like environment for any custom named environment that didn't define a createEnvironment function. I think this is a better default given that custom named environments are SSR-like by default.

@sheremet-va how do you see the default client environment if we deprecate ws? We can't have a createClientHotChannel() as we will need the HttpServer instance, no? If we want to avoid passing the whole Vite server, maybe we'll need to pass more things in the CreateDevEnvironmentContext.

I think moving this to the config face forces us to think what is needed to create an environment and play with the same rules for the default and custom ones.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I don't understand the question. We are deprecating the ws on the server, we are not removing the websocket server.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we need the client environment.hot to be this websocket server, no? The question is how do we create hot to pass to the client once server.ws is gone. Maybe the answer is that we still pass ws to CreateDevEnvironmentContext?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it what I proposed? Pass down ws? #17791 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is what we are doing now. But you also said "But make it optional so we don't need to deprecate it when we add ws: false)". IIUC, it is ok to have it always there, it won't need to be made optional as we still need to pass the ws so the client environment can use it.

runner: {
transport: new RemoteEnvironmentTransport({
send: (data) => worker.postMessage(data),
onMessage: (listener) => worker.on('message', listener),
}),
},
watcher: context.watcher
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I moved to watcher: false | FSWatcher to force the user to pass the watcher forward. I tried doing this during init() but it doesn't feel right. Maybe it is ok for createEnvironment to be a bit more complex to implement given that only framework and runtime maintainers will be doing this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The watcher is only used in transformRequest. Maybe we can find a way to remove it from there somehow so it's not required in the environment at all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just re-checked and we also need the watcher to implement the plugin context addWatchFile(). And I found a bug, as we weren't passing the watcher when creating the plugin container for each environment. I don't think we can remove this. I moved it to environment.init({ watcher }) at 569f714. We can decide if we want environment.watcher or we leave it as internal as it is used now environment.pluginContainer.watcher. Does this look better?

@patak-dev patak-dev merged commit 94576b9 into v6/environment-api Jul 30, 2024
12 of 13 checks passed
@patak-dev patak-dev deleted the feat/resolved-create-environment branch July 30, 2024 16:40
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.

3 participants