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: introduce RunnableDevEnvironment #18190

Merged
merged 10 commits into from
Sep 27, 2024

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented Sep 24, 2024

Description

This PR introduces a new concept to the Environment API - the RunnableDevEnvironment. This is the environment that has a module runner in the same process. You can access it directly as environment.runner. The runner itself is evaluated eagerly when it's accessed for the first time.

By default, Vite SSR dev environment is a runnable environment. Due to environments being available during the build, there are some constraints in typing. You can either cast it to RunnableDevEnvironment or type guard it with isRunnableDevEnvironment.

import { createServer, isRunnableDevEnvironment } from 'vite'
import type { RunnableDevEnvironment } from 'vite'

const server = createServer()
const ssrEnvironment = server.environments.ssr as RunnableDevEnvironment

if (isRunnableDevEnvironment(ssrEnvironment)) {
  await ssrEnvironment.runner.import('/entry-point.js')
}

You can create your own runnable environments using the createRunnableDevEnvironment function:

import { createRunnableDevEnvironment } from 'vite'

const environment = createRunnableDevEnvironment(name, config)
await environment.runner.import('/entry-point.js')

You can configure the runner with the runner option (the previous runner option was renamed to remoteRunner):

import { createRunnableDevEnvironment, createServerModuleRunner } from 'vite'

const environment = createRunnableDevEnvironment(name, config, {
  runner: (env) => createServerModuleRunner(env, { hmr: false })
})
await environment.runner.import('/entry-point.js')

Copy link

stackblitz bot commented Sep 24, 2024

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

@sheremet-va sheremet-va marked this pull request as ready for review September 27, 2024 08:42
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

Nice! I think this should help a lot with the ssrLoadModule migration story. I think we should merge it to keep exploring. If others think we should review this, we can later revert it before the release.

@patak-dev patak-dev merged commit fb292f2 into vitejs:main Sep 27, 2024
12 checks passed
@sheremet-va sheremet-va deleted the feat/runnable-dev-environment branch September 27, 2024 12:03
@silvenon
Copy link

silvenon commented Oct 4, 2024

I like that this is already a part of SSR by default ❤️

Can you clarify what this means?

The runner itself is evaluated eagerly when it's accessed for the first time.

@sheremet-va
Copy link
Member Author

I like that this is already a part of SSR by default ❤️

Can you clarify what this means?

The runner itself is evaluated eagerly when it's accessed for the first time.

Basically, the runner doesn't exist until it's called. I don't know how else to explain it. It's evaluated lazily.

It's important to know because the initialisation of the runner has side effects. More is explained in the docs.

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