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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

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.

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.

1 participant