Accessing the Environments
During dev, the available environments in a dev server can be accessed usingserver.environments:
DevEnvironment Class
During dev, each environment is an instance of theDevEnvironment class:
Properties
Unique identifier for the environment in a Vite server. By default Vite exposes ‘client’ and ‘ssr’ environments.
Communication channel to send and receive messages from the associated module runner in the target runtime
Graph of module nodes, with the imported relationship between processed modules and the cached result of the processed code
Resolved plugins for this environment, including the ones created using the per-environment
create hookAllows to resolve, load, and transform code through the environment plugins pipeline
Resolved config options for this environment. Options at the server global scope are taken as defaults for all environments, and can be overridden (resolve conditions, external, optimizedDeps)
Constructor
Environment name
Resolved Vite configuration
Environment context with HMR settings and options
Methods
transformRequest()
Resolve the URL to an id, load it, and process the code using the plugins pipeline. The module graph is also updated. Type Signature:The URL to transform
The transform result or null if the module couldn’t be processed
environment.transformRequest(url) method. This function will use the plugin pipeline to resolve the url to a module id, load it (reading the file from the file system or through a plugin that implements a virtual module), and then transform the code.
While transforming the module, imports and other metadata will be recorded in the environment module graph by creating or updating the corresponding module node. When processing is done, the transform result is also stored in the module.
warmupRequest()
Register a request to be processed with low priority. This is useful to avoid waterfalls. Type Signature:The URL to warmup
Separate Module Graphs
Each environment has an isolated module graph. All module graphs have the same signature, so generic algorithms can be implemented to crawl or query the graph without depending on the environment.hotUpdate is a good example. When a file is modified, the module graph of each environment will be used to discover the affected modules and perform HMR for each environment independently.
Vite v5 had a mixed Client and SSR module graph. Given an unprocessed or invalidated node, it isn’t possible to know if it corresponds to the Client, SSR, or both environments. Module nodes have some properties prefixed, like
clientImportedModules and ssrImportedModules (and importedModules that returns the union of both).importers contains all importers from both the Client and SSR environment for each module node. A module node also has transformResult and ssrTransformResult. A backward compatibility layer allows the ecosystem to migrate from the deprecated server.moduleGraph.EnvironmentModuleNode
Each module is represented by anEnvironmentModuleNode instance. Modules may be registered in the graph without yet being processed (transformResult would be null in that case). importers and importedModules are also updated after the module is processed.
Properties
The environment this module belongs to
The module URL
The resolved module ID (null if not yet resolved)
The file path on disk (null for virtual modules)
Module type
Modules that import this module
Modules imported by this module
The cached transform result (null if not yet processed)
Whether this module accepts HMR updates for itself
EnvironmentModuleGraph
environment.moduleGraph is an instance of EnvironmentModuleGraph:
Properties
The environment name this graph belongs to
Map from module URL to module node
Map from module ID to module node
Map from file path to set of module nodes (one file can have multiple modules)
Methods
Get a module by its URL
Get a module by its resolved ID
Get all modules associated with a file path
Handle file change events
Handle file deletion events
invalidateModule
(mod: EnvironmentModuleNode, seen?: Set, timestamp?: number, isHmr?: boolean) => void
Invalidate a module and optionally its importers
Invalidate all modules in the graph
Ensure a module entry exists for the given URL, creating it if necessary
Update the transform result for a module