mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
feat: top level environments for server and builder
This commit is contained in:
parent
66d1548151
commit
7d67ac1f93
@ -31,8 +31,8 @@ class ModuleRunner {
|
||||
*/
|
||||
}
|
||||
|
||||
if (isRunnableDevEnvironment(server.environments.$ssr)) {
|
||||
await server.environments.$ssr.runner.import('/entry-point.js')
|
||||
if (isRunnableDevEnvironment(server.$ssr)) {
|
||||
await server.$ssr.runner.import('/entry-point.js')
|
||||
}
|
||||
```
|
||||
|
||||
@ -57,7 +57,7 @@ const server = await createServer({
|
||||
|
||||
// You might need to cast this to RunnableDevEnvironment in TypeScript or use
|
||||
// the "isRunnableDevEnvironment" function to guard the access to the runner
|
||||
const environment = server.environments.$server
|
||||
const environment = server.$server
|
||||
|
||||
app.use('*', async (req, res, next) => {
|
||||
const url = req.originalUrl
|
||||
@ -107,7 +107,7 @@ For example, the following example uses the value of the user module from the co
|
||||
import { createServer } from 'vite'
|
||||
|
||||
const server = createServer()
|
||||
const ssrEnvironment = server.environments.$ssr
|
||||
const ssrEnvironment = server.$ssr
|
||||
const input = {}
|
||||
|
||||
const { createHandler } = await ssrEnvironment.runner.import('./entrypoint.js')
|
||||
@ -138,7 +138,7 @@ const server = createServer({
|
||||
},
|
||||
],
|
||||
})
|
||||
const ssrEnvironment = server.environments.$ssr
|
||||
const ssrEnvironment = server.$ssr
|
||||
const input = {}
|
||||
|
||||
// use exposed functions by each environment factories that runs the code
|
||||
@ -212,7 +212,7 @@ const server = createServer({
|
||||
},
|
||||
],
|
||||
})
|
||||
const ssrEnvironment = server.environments.$ssr
|
||||
const ssrEnvironment = server.$ssr
|
||||
const input = {}
|
||||
|
||||
// use exposed functions by each environment factories that runs the code
|
||||
@ -281,4 +281,4 @@ export default {
|
||||
|
||||
## Environment agnostic code
|
||||
|
||||
Most of the time, the current `environment` instance will be available as part of the context of the code being run so the need to access them through `server.environments` should be rare. For example, inside plugin hooks the environment is exposed as part of the `PluginContext`, so it can be accessed using `this.environment`. See [Environment API for Plugins](./api-environment-plugins.md) to learn about how to build environment aware plugins.
|
||||
Most of the time, the current `environment` instance will be available as part of the context of the code being run so the need to access them by name should be rare. For example, inside plugin hooks the environment is exposed as part of the `PluginContext`, so it can be accessed using `this.environment`. See [Environment API for Plugins](./api-environment-plugins.md) to learn about how to build environment aware plugins.
|
||||
|
@ -13,15 +13,15 @@ Please share with us your feedback as you test the proposal.
|
||||
|
||||
## Accessing the environments
|
||||
|
||||
During dev, the available environments in a dev server can be accessed using `server.environments`:
|
||||
During dev, the available environments in a dev server can be accessed using `server`:
|
||||
|
||||
```js
|
||||
// create the server, or get it from the configureServer hook
|
||||
const server = await createServer(/* options */)
|
||||
|
||||
const environment = server.environments.$client
|
||||
const environment = server.$client
|
||||
environment.transformRequest(url)
|
||||
console.log(server.environments.$ssr.moduleGraph)
|
||||
console.log(server.$ssr.moduleGraph)
|
||||
```
|
||||
|
||||
You can also access the current environment from plugins. See the [Environment API for Plugins](./api-environment-plugins.md#accessing-the-current-environment-in-hooks) for more details.
|
||||
|
@ -33,7 +33,7 @@ Plugins can add new environments in the `config` hook (for example to have a sep
|
||||
|
||||
```ts
|
||||
config(config: UserConfig) {
|
||||
config.environments.rsc ??= {}
|
||||
config.$rsc ??= {}
|
||||
}
|
||||
```
|
||||
|
||||
@ -41,7 +41,7 @@ An empty object is enough to register the environment, default values from the r
|
||||
|
||||
## Configuring environment using hooks
|
||||
|
||||
While the `config` hook is running, the complete list of environments isn't yet known and the environments can be affected by both the default values from the root level environment config or explicitly through the `config.environments` record.
|
||||
While the `config` hook is running, the complete list of environments isn't yet known and the environments can be affected by both the default values from the root level environment config or explicitly using `config.$environment`.
|
||||
Plugins should set default values using the `config` hook. To configure each environment, they can use the new `configEnvironment` hook. This hook is called for each environment with its partially resolved config including resolution of final defaults.
|
||||
|
||||
```ts
|
||||
|
@ -66,7 +66,7 @@ export default {
|
||||
and frameworks can use an environment with the workerd runtime to do SSR using:
|
||||
|
||||
```js
|
||||
const ssrEnvironment = server.environments.$ssr
|
||||
const ssrEnvironment = server.$ssr
|
||||
```
|
||||
|
||||
## Creating a new environment factory
|
||||
|
@ -624,7 +624,7 @@ describe('resolveBuildOutputs', () => {
|
||||
},
|
||||
},
|
||||
})
|
||||
const result = await builder.build(builder.environments.$ssr)
|
||||
const result = await builder.build(builder.$ssr)
|
||||
expect(result).toMatchObject({
|
||||
output: [
|
||||
{
|
||||
@ -652,7 +652,7 @@ describe('resolveBuildOutputs', () => {
|
||||
},
|
||||
},
|
||||
})
|
||||
const result = await builder.build(builder.environments.$ssr)
|
||||
const result = await builder.build(builder.$ssr)
|
||||
expect((result as RollupOutput).output[0].code).not.toContain('preload')
|
||||
})
|
||||
|
||||
@ -671,7 +671,7 @@ describe('resolveBuildOutputs', () => {
|
||||
},
|
||||
},
|
||||
})
|
||||
const result = await builder.build(builder.environments.$custom)
|
||||
const result = await builder.build(builder.$custom)
|
||||
expect((result as RollupOutput).output[0].code).not.toContain('preload')
|
||||
})
|
||||
})
|
||||
|
@ -141,7 +141,7 @@ describe('custom environment conditions', () => {
|
||||
'$custom3',
|
||||
'$custom3_2',
|
||||
]) {
|
||||
const runner = createServerModuleRunner(server.environments[key], {
|
||||
const runner = createServerModuleRunner(server[key], {
|
||||
hmr: {
|
||||
logger: false,
|
||||
},
|
||||
@ -175,7 +175,7 @@ describe('custom environment conditions', () => {
|
||||
'$custom3',
|
||||
'$custom3_2',
|
||||
]) {
|
||||
const runner = createServerModuleRunner(server.environments[key], {
|
||||
const runner = createServerModuleRunner(server[key], {
|
||||
hmr: {
|
||||
logger: false,
|
||||
},
|
||||
@ -228,7 +228,7 @@ describe('custom environment conditions', () => {
|
||||
'$custom3',
|
||||
'$custom3_2',
|
||||
]) {
|
||||
const output = await builder.build(builder.environments[key])
|
||||
const output = await builder.build(builder[key])
|
||||
const chunk = (output as RollupOutput).output[0]
|
||||
const mod = await import(
|
||||
path.join(
|
||||
|
@ -13,7 +13,7 @@ describe('import and resolveId', () => {
|
||||
},
|
||||
})
|
||||
onTestFinished(() => server.close())
|
||||
const runner = createServerModuleRunner(server.environments.$ssr, {
|
||||
const runner = createServerModuleRunner(server.$ssr, {
|
||||
hmr: {
|
||||
logger: false,
|
||||
},
|
||||
@ -27,7 +27,7 @@ describe('import and resolveId', () => {
|
||||
const mod = await runner.import(
|
||||
'/fixtures/test-dep-conditions-app/entry-with-module',
|
||||
)
|
||||
const resolved = await server.environments.$ssr.pluginContainer.resolveId(
|
||||
const resolved = await server.$ssr.pluginContainer.resolveId(
|
||||
'@vitejs/test-dep-conditions/with-module',
|
||||
)
|
||||
expect([mod.default, resolved?.id]).toEqual([
|
||||
@ -38,7 +38,7 @@ describe('import and resolveId', () => {
|
||||
|
||||
test('resolveId first', async () => {
|
||||
const { server, runner } = await createTestServer()
|
||||
const resolved = await server.environments.$ssr.pluginContainer.resolveId(
|
||||
const resolved = await server.$ssr.pluginContainer.resolveId(
|
||||
'@vitejs/test-dep-conditions/with-module',
|
||||
)
|
||||
const mod = await runner.import(
|
||||
|
@ -24,7 +24,7 @@ export function getDefaultResolvedEnvironmentOptions(
|
||||
}
|
||||
|
||||
export class PartialEnvironment {
|
||||
name: string
|
||||
name: `$${string}`
|
||||
getTopLevelConfig(): ResolvedConfig {
|
||||
return this._topLevelConfig
|
||||
}
|
||||
@ -50,9 +50,9 @@ export class PartialEnvironment {
|
||||
_topLevelConfig: ResolvedConfig
|
||||
|
||||
constructor(
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
topLevelConfig: ResolvedConfig,
|
||||
options: ResolvedEnvironmentOptions = topLevelConfig.environments[name],
|
||||
options: ResolvedEnvironmentOptions = topLevelConfig[name],
|
||||
) {
|
||||
// only allow some characters so that we can use name without escaping for directory names
|
||||
// and make users easier to access with `environments.*`
|
||||
@ -140,9 +140,9 @@ export class BaseEnvironment extends PartialEnvironment {
|
||||
_initiated: boolean = false
|
||||
|
||||
constructor(
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
options: ResolvedEnvironmentOptions = config.environments[name],
|
||||
options: ResolvedEnvironmentOptions = config[name],
|
||||
) {
|
||||
super(name, config, options)
|
||||
}
|
||||
|
@ -269,7 +269,7 @@ export interface BuildEnvironmentOptions {
|
||||
* create the Build Environment instance
|
||||
*/
|
||||
createEnvironment?: (
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
) => Promise<BuildEnvironment> | BuildEnvironment
|
||||
}
|
||||
@ -517,7 +517,7 @@ export async function build(
|
||||
// remove the default values that shouldn't be used at all once the config is resolved
|
||||
const environmentName = resolved.build.ssr ? '$ssr' : '$client'
|
||||
;(resolved.build as ResolvedBuildOptions) = {
|
||||
...resolved.environments[environmentName].build,
|
||||
...resolved[environmentName].build,
|
||||
}
|
||||
}
|
||||
const config = await resolveConfigToBuild(inlineConfig, patchConfig)
|
||||
@ -531,9 +531,10 @@ export async function buildWithResolvedConfig(
|
||||
config: ResolvedConfig,
|
||||
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
|
||||
const environmentName = config.build.ssr ? '$ssr' : '$client'
|
||||
const environment = await config.environments[
|
||||
environmentName
|
||||
].build.createEnvironment(environmentName, config)
|
||||
const environment = await config[environmentName].build.createEnvironment(
|
||||
environmentName,
|
||||
config,
|
||||
)
|
||||
await environment.init()
|
||||
return buildEnvironment(environment)
|
||||
}
|
||||
@ -1464,14 +1465,13 @@ export class BuildEnvironment extends BaseEnvironment {
|
||||
mode = 'build' as const
|
||||
|
||||
constructor(
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
setup?: {
|
||||
options?: EnvironmentOptions
|
||||
},
|
||||
) {
|
||||
let options =
|
||||
config.environments[name] ?? getDefaultResolvedEnvironmentOptions(config)
|
||||
let options = config[name] ?? getDefaultResolvedEnvironmentOptions(config)
|
||||
if (setup?.options) {
|
||||
options = mergeConfig(
|
||||
options,
|
||||
@ -1493,6 +1493,7 @@ export class BuildEnvironment extends BaseEnvironment {
|
||||
|
||||
export interface ViteBuilder {
|
||||
environments: Record<string, BuildEnvironment>
|
||||
[key: `$${string}`]: BuildEnvironment
|
||||
config: ResolvedConfig
|
||||
buildApp(): Promise<void>
|
||||
build(
|
||||
@ -1557,7 +1558,9 @@ export async function createBuilderWithResolvedConfig(
|
||||
},
|
||||
}
|
||||
|
||||
for (const environmentName of Object.keys(config.environments)) {
|
||||
for (const environmentName of Object.keys(
|
||||
config.environments,
|
||||
) as `$${string}`[]) {
|
||||
// We need to resolve the config again so we can properly merge options
|
||||
// and get a new set of plugins for each build environment. The ecosystem
|
||||
// expects plugins to be run for the same environment once they are created
|
||||
@ -1571,7 +1574,7 @@ export async function createBuilderWithResolvedConfig(
|
||||
// We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
|
||||
// remove the default values that shouldn't be used at all once the config is resolved
|
||||
;(resolved.build as ResolvedBuildOptions) = {
|
||||
...resolved.environments[environmentName].build,
|
||||
...resolved[environmentName].build,
|
||||
}
|
||||
}
|
||||
const patchPlugins = (resolvedPlugins: Plugin[]) => {
|
||||
@ -1607,7 +1610,7 @@ export async function createBuilderWithResolvedConfig(
|
||||
|
||||
await environment.init()
|
||||
|
||||
environments[environmentName] = environment
|
||||
builder[environmentName] = environments[environmentName] = environment
|
||||
}
|
||||
|
||||
return builder
|
||||
|
@ -319,7 +319,7 @@ cli
|
||||
// remove the default values that shouldn't be used at all once the config is resolved
|
||||
const environmentName = resolved.build.ssr ? '$ssr' : '$client'
|
||||
;(resolved.build as ResolvedBuildOptions) = {
|
||||
...resolved.environments[environmentName].build,
|
||||
...resolved[environmentName].build,
|
||||
}
|
||||
}
|
||||
const config = await build.resolveConfigToBuild(
|
||||
|
@ -179,7 +179,7 @@ export interface DevEnvironmentOptions {
|
||||
* create the Dev Environment instance
|
||||
*/
|
||||
createEnvironment?: (
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
context: CreateDevEnvironmentContext,
|
||||
) => Promise<DevEnvironment> | DevEnvironment
|
||||
@ -201,7 +201,7 @@ export interface DevEnvironmentOptions {
|
||||
}
|
||||
|
||||
function defaultCreateClientDevEnvironment(
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
context: CreateDevEnvironmentContext,
|
||||
) {
|
||||
@ -211,13 +211,16 @@ function defaultCreateClientDevEnvironment(
|
||||
}
|
||||
|
||||
function defaultCreateSsrDevEnvironment(
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
): DevEnvironment {
|
||||
return createRunnableDevEnvironment(name, config)
|
||||
}
|
||||
|
||||
function defaultCreateDevEnvironment(name: string, config: ResolvedConfig) {
|
||||
function defaultCreateDevEnvironment(
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
) {
|
||||
return new DevEnvironment(name, config, {
|
||||
hot: false,
|
||||
})
|
||||
@ -576,7 +579,7 @@ export type ResolvedConfig = Readonly<
|
||||
worker: ResolvedWorkerOptions
|
||||
appType: AppType
|
||||
experimental: ExperimentalOptions
|
||||
environments: Record<string, ResolvedEnvironmentOptions>
|
||||
environments: Record<`$${string}`, ResolvedEnvironmentOptions>
|
||||
[key: `$${string}`]: ResolvedEnvironmentOptions
|
||||
/** @internal */
|
||||
fsDenyGlob: AnymatchFn
|
||||
|
@ -97,12 +97,11 @@ export class DevEnvironment extends BaseEnvironment {
|
||||
*/
|
||||
hot: HotChannel
|
||||
constructor(
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
context: DevEnvironmentContext,
|
||||
) {
|
||||
let options =
|
||||
config.environments[name] ?? getDefaultResolvedEnvironmentOptions(config)
|
||||
let options = config[name] ?? getDefaultResolvedEnvironmentOptions(config)
|
||||
if (context.options) {
|
||||
options = mergeConfig(
|
||||
options,
|
||||
|
@ -9,7 +9,7 @@ import { createServerHotChannel } from '../hmr'
|
||||
import type { Environment } from '../../environment'
|
||||
|
||||
export function createRunnableDevEnvironment(
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
context: RunnableDevEnvironmentContext = {},
|
||||
): DevEnvironment {
|
||||
@ -47,7 +47,7 @@ class RunnableDevEnvironment extends DevEnvironment {
|
||||
private _runnerOptions: ServerModuleRunnerOptions | undefined
|
||||
|
||||
constructor(
|
||||
name: string,
|
||||
name: `$${string}`,
|
||||
config: ResolvedConfig,
|
||||
context: RunnableDevEnvironmentContext,
|
||||
) {
|
||||
|
@ -253,15 +253,13 @@ export async function handleHMRUpdate(
|
||||
modules: [...mixedMods],
|
||||
}
|
||||
|
||||
const clientEnvironment = server.environments.$client
|
||||
const ssrEnvironment = server.environments.$ssr
|
||||
const clientEnvironment = server.$client
|
||||
const ssrEnvironment = server.$ssr
|
||||
const clientContext = { environment: clientEnvironment }
|
||||
const clientHotUpdateOptions = hotMap.get(clientEnvironment)!.options
|
||||
const ssrHotUpdateOptions = hotMap.get(ssrEnvironment)?.options
|
||||
try {
|
||||
for (const plugin of getSortedHotUpdatePlugins(
|
||||
server.environments.$client,
|
||||
)) {
|
||||
for (const plugin of getSortedHotUpdatePlugins(server.$client)) {
|
||||
if (plugin.hotUpdate) {
|
||||
const filteredModules = await getHookHandler(plugin.hotUpdate).call(
|
||||
clientContext,
|
||||
@ -340,7 +338,7 @@ export async function handleHMRUpdate(
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
hotMap.get(server.environments.$client)!.error = error
|
||||
hotMap.get(server.$client)!.error = error
|
||||
}
|
||||
|
||||
for (const environment of Object.values(server.environments)) {
|
||||
|
@ -275,9 +275,10 @@ export interface ViteDevServer {
|
||||
*/
|
||||
pluginContainer: PluginContainer
|
||||
/**
|
||||
* Module execution environments attached to the Vite server.
|
||||
* Dev environments attached to the Vite server.
|
||||
*/
|
||||
environments: Record<'$client' | '$ssr' | (string & {}), DevEnvironment>
|
||||
[key: `$${string}`]: DevEnvironment
|
||||
/**
|
||||
* Module graph that tracks the import relationships, url to file mapping
|
||||
* and hmr state.
|
||||
@ -492,7 +493,7 @@ export async function _createServer(
|
||||
config.environments,
|
||||
)) {
|
||||
environments[name] = await environmentOptions.dev.createEnvironment(
|
||||
name,
|
||||
name as `$${string}`,
|
||||
config,
|
||||
{
|
||||
ws,
|
||||
@ -526,6 +527,8 @@ export async function _createServer(
|
||||
hot: createDeprecatedHotBroadcaster(ws),
|
||||
|
||||
environments,
|
||||
...environments,
|
||||
|
||||
pluginContainer,
|
||||
get moduleGraph() {
|
||||
warnFutureDeprecation(config, 'removeServerModuleGraph')
|
||||
@ -555,13 +558,12 @@ export async function _createServer(
|
||||
'removeServerTransformRequest',
|
||||
'server.transformRequest() is deprecated. Use environment.transformRequest() instead.',
|
||||
)
|
||||
const environment = server.environments[options?.ssr ? '$ssr' : '$client']
|
||||
const environment = server[options?.ssr ? '$ssr' : '$client']
|
||||
return transformRequest(environment, url, options)
|
||||
},
|
||||
async warmupRequest(url, options) {
|
||||
try {
|
||||
const environment =
|
||||
server.environments[options?.ssr ? '$ssr' : '$client']
|
||||
const environment = server[options?.ssr ? '$ssr' : '$client']
|
||||
await transformRequest(environment, url, options)
|
||||
} catch (e) {
|
||||
if (
|
||||
@ -586,10 +588,10 @@ export async function _createServer(
|
||||
return ssrLoadModule(url, server, opts?.fixStacktrace)
|
||||
},
|
||||
ssrFixStacktrace(e) {
|
||||
ssrFixStacktrace(e, server.environments.$ssr.moduleGraph)
|
||||
ssrFixStacktrace(e, server.$ssr.moduleGraph)
|
||||
},
|
||||
ssrRewriteStacktrace(stack: string) {
|
||||
return ssrRewriteStacktrace(stack, server.environments.$ssr.moduleGraph)
|
||||
return ssrRewriteStacktrace(stack, server.$ssr.moduleGraph)
|
||||
},
|
||||
async reloadModule(module) {
|
||||
if (serverConfig.hmr !== false && module.file) {
|
||||
@ -765,7 +767,7 @@ export async function _createServer(
|
||||
const path = file.slice(publicDir.length)
|
||||
publicFiles[isUnlink ? 'delete' : 'add'](path)
|
||||
if (!isUnlink) {
|
||||
const clientModuleGraph = server.environments.$client.moduleGraph
|
||||
const clientModuleGraph = server.$client.moduleGraph
|
||||
const moduleWithSamePath =
|
||||
await clientModuleGraph.getModuleByUrl(path)
|
||||
const etag = moduleWithSamePath?.transformResult?.etag
|
||||
|
@ -53,7 +53,7 @@ export function logError(server: ViteDevServer, err: RollupError): void {
|
||||
error: err,
|
||||
})
|
||||
|
||||
server.environments.$client.hot.send({
|
||||
server.$client.hot.send({
|
||||
type: 'error',
|
||||
err: prepareError(err),
|
||||
})
|
||||
|
@ -131,8 +131,7 @@ const processNodeUrl = (
|
||||
// prefix with base (dev only, base is never relative)
|
||||
const replacer = (url: string) => {
|
||||
if (server) {
|
||||
const mod =
|
||||
server.environments.$client.moduleGraph.urlToModuleMap.get(url)
|
||||
const mod = server.$client.moduleGraph.urlToModuleMap.get(url)
|
||||
if (mod && mod.lastHMRTimestamp > 0) {
|
||||
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
|
||||
}
|
||||
@ -251,7 +250,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
|
||||
const modulePath = `${proxyModuleUrl}?html-proxy&index=${inlineModuleIndex}.${ext}`
|
||||
|
||||
// invalidate the module so the newly cached contents will be served
|
||||
const clientModuleGraph = server?.environments.$client.moduleGraph
|
||||
const clientModuleGraph = server?.$client.moduleGraph
|
||||
const module = clientModuleGraph?.getModuleById(modulePath)
|
||||
if (module) {
|
||||
clientModuleGraph!.invalidateModule(module)
|
||||
@ -360,15 +359,14 @@ const devHtmlHook: IndexHtmlTransformHook = async (
|
||||
const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css`
|
||||
|
||||
// ensure module in graph after successful load
|
||||
const mod =
|
||||
await server!.environments.$client.moduleGraph.ensureEntryFromUrl(
|
||||
url,
|
||||
false,
|
||||
)
|
||||
const mod = await server!.$client.moduleGraph.ensureEntryFromUrl(
|
||||
url,
|
||||
false,
|
||||
)
|
||||
ensureWatchedFile(watcher, mod.file, config.root)
|
||||
|
||||
const result = await server!.pluginContainer.transform(code, mod.id!, {
|
||||
environment: server!.environments.$client,
|
||||
environment: server!.$client,
|
||||
})
|
||||
let content = ''
|
||||
if (result) {
|
||||
@ -391,15 +389,14 @@ const devHtmlHook: IndexHtmlTransformHook = async (
|
||||
// will transform with css plugin and cache result with css-post plugin
|
||||
const url = `${proxyModulePath}?html-proxy&inline-css&style-attr&index=${index}.css`
|
||||
|
||||
const mod =
|
||||
await server!.environments.$client.moduleGraph.ensureEntryFromUrl(
|
||||
url,
|
||||
false,
|
||||
)
|
||||
const mod = await server!.$client.moduleGraph.ensureEntryFromUrl(
|
||||
url,
|
||||
false,
|
||||
)
|
||||
ensureWatchedFile(watcher, mod.file, config.root)
|
||||
|
||||
await server?.pluginContainer.transform(code, mod.id!, {
|
||||
environment: server!.environments.$client,
|
||||
environment: server!.$client,
|
||||
})
|
||||
|
||||
const hash = getHash(cleanUrl(mod.id!))
|
||||
|
@ -50,7 +50,7 @@ export function cachedTransformMiddleware(
|
||||
): Connect.NextHandleFunction {
|
||||
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
|
||||
return function viteCachedTransformMiddleware(req, res, next) {
|
||||
const environment = server.environments.$client
|
||||
const environment = server.$client
|
||||
|
||||
// check if we can return 304 early
|
||||
const ifNoneMatch = req.headers['if-none-match']
|
||||
@ -87,7 +87,7 @@ export function transformMiddleware(
|
||||
const publicPath = `${publicDir.slice(root.length)}/`
|
||||
|
||||
return async function viteTransformMiddleware(req, res, next) {
|
||||
const environment = server.environments.$client
|
||||
const environment = server.$client
|
||||
|
||||
if (req.method !== 'GET' || knownIgnoreList.has(req.url!)) {
|
||||
return next()
|
||||
|
@ -53,7 +53,7 @@ describe('module runner initialization', async () => {
|
||||
(code) => '\n\n\n\n\n' + code + '\n',
|
||||
)
|
||||
runner.evaluatedModules.clear()
|
||||
server.environments.$ssr.moduleGraph.invalidateAll()
|
||||
server.$ssr.moduleGraph.invalidateAll()
|
||||
|
||||
const methodErrorNew = await getError(async () => {
|
||||
const mod = await runner.import('/fixtures/throws-error-method.ts')
|
||||
|
@ -73,7 +73,7 @@ export async function createModuleRunnerTester(
|
||||
],
|
||||
...config,
|
||||
})
|
||||
t.environment = t.server.environments.$ssr
|
||||
t.environment = t.server.$ssr
|
||||
t.runner = createServerModuleRunner(t.environment, {
|
||||
hmr: {
|
||||
logger: false,
|
||||
|
@ -13,7 +13,7 @@ export async function ssrLoadModule(
|
||||
server: ViteDevServer,
|
||||
fixStacktrace?: boolean,
|
||||
): Promise<SSRModule> {
|
||||
const environment = server.environments.$ssr
|
||||
const environment = server.$ssr
|
||||
server._ssrCompatModuleRunner ||= new SSRCompatModuleRunner(environment)
|
||||
url = unwrapId(url)
|
||||
|
||||
|
@ -46,8 +46,8 @@ export default defineConfig((env) => ({
|
||||
|
||||
builder: {
|
||||
async buildApp(builder) {
|
||||
await builder.build(builder.environments.$client)
|
||||
await builder.build(builder.environments.$ssr)
|
||||
await builder.build(builder.$client)
|
||||
await builder.build(builder.$ssr)
|
||||
},
|
||||
},
|
||||
}))
|
||||
@ -64,7 +64,7 @@ export function vitePluginSsrMiddleware({
|
||||
name: vitePluginSsrMiddleware.name,
|
||||
|
||||
configureServer(server) {
|
||||
const runner = createServerModuleRunner(server.environments.$ssr, {
|
||||
const runner = createServerModuleRunner(server.$ssr, {
|
||||
hmr: { logger: false },
|
||||
})
|
||||
const handler: Connect.NextHandleFunction = async (req, res, next) => {
|
||||
|
@ -498,7 +498,7 @@ if (!isBuild) {
|
||||
beforeAll(async () => {
|
||||
clientLogs.length = 0
|
||||
// so it's in the module graph
|
||||
const ssrEnvironment = server.environments.$ssr
|
||||
const ssrEnvironment = server.$ssr
|
||||
await ssrEnvironment.transformRequest(testFile)
|
||||
await ssrEnvironment.transformRequest('non-tested/dep.js')
|
||||
})
|
||||
@ -1079,7 +1079,7 @@ async function setupModuleRunner(
|
||||
...serverOptions,
|
||||
})
|
||||
|
||||
runner = (server.environments.$ssr as RunnableDevEnvironment).runner
|
||||
runner = (server.$ssr as RunnableDevEnvironment).runner
|
||||
|
||||
await waitForWatcher(server, waitForFile)
|
||||
|
||||
|
@ -17,12 +17,9 @@ export default defineConfig({
|
||||
}
|
||||
},
|
||||
configureServer(server) {
|
||||
server.environments.$ssr.hot.on(
|
||||
'custom:remote-add',
|
||||
({ a, b }, client) => {
|
||||
client.send('custom:remote-add-result', { result: a + b })
|
||||
},
|
||||
)
|
||||
server.$ssr.hot.on('custom:remote-add', ({ a, b }, client) => {
|
||||
client.send('custom:remote-add-result', { result: a + b })
|
||||
})
|
||||
},
|
||||
},
|
||||
virtualPlugin(),
|
||||
@ -48,14 +45,12 @@ export const virtual = _virtual + '${num}';`
|
||||
}
|
||||
},
|
||||
configureServer(server) {
|
||||
server.environments.$ssr.hot.on('virtual:increment', async () => {
|
||||
server.$ssr.hot.on('virtual:increment', async () => {
|
||||
const mod =
|
||||
await server.environments.$ssr.moduleGraph.getModuleByUrl(
|
||||
'\0virtual:file',
|
||||
)
|
||||
await server.$ssr.moduleGraph.getModuleByUrl('\0virtual:file')
|
||||
if (mod) {
|
||||
num++
|
||||
server.environments.$ssr.reloadModule(mod)
|
||||
server.$ssr.reloadModule(mod)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
@ -19,12 +19,9 @@ export default defineConfig({
|
||||
}
|
||||
},
|
||||
configureServer(server) {
|
||||
server.environments.$client.hot.on(
|
||||
'custom:remote-add',
|
||||
({ a, b }, client) => {
|
||||
client.send('custom:remote-add-result', { result: a + b })
|
||||
},
|
||||
)
|
||||
server.$client.hot.on('custom:remote-add', ({ a, b }, client) => {
|
||||
client.send('custom:remote-add-result', { result: a + b })
|
||||
})
|
||||
},
|
||||
},
|
||||
virtualPlugin(),
|
||||
@ -50,14 +47,12 @@ export const virtual = _virtual + '${num}';`
|
||||
}
|
||||
},
|
||||
configureServer(server) {
|
||||
server.environments.$client.hot.on('virtual:increment', async () => {
|
||||
server.$client.hot.on('virtual:increment', async () => {
|
||||
const mod =
|
||||
await server.environments.$client.moduleGraph.getModuleByUrl(
|
||||
'\0virtual:file',
|
||||
)
|
||||
await server.$client.moduleGraph.getModuleByUrl('\0virtual:file')
|
||||
if (mod) {
|
||||
num++
|
||||
server.environments.$client.reloadModule(mod)
|
||||
server.$client.reloadModule(mod)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
@ -283,7 +283,7 @@ describe.runIf(isServe)('invalid', () => {
|
||||
await page.keyboard.press('Escape')
|
||||
await hiddenPromise
|
||||
|
||||
viteServer.environments.$client.hot.send({
|
||||
viteServer.$client.hot.send({
|
||||
type: 'error',
|
||||
err: {
|
||||
message: 'someError',
|
||||
@ -395,9 +395,7 @@ describe.runIf(isServe)('warmup', () => {
|
||||
// here might take a while to load
|
||||
await withRetry(async () => {
|
||||
const mod =
|
||||
await viteServer.environments.$client.moduleGraph.getModuleByUrl(
|
||||
'/warmup/warm.js',
|
||||
)
|
||||
await viteServer.$client.moduleGraph.getModuleByUrl('/warmup/warm.js')
|
||||
expect(mod).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
@ -4,7 +4,7 @@ import { isServe, page, viteServer } from '~utils'
|
||||
test.runIf(isServe)('importedUrls order is preserved', async () => {
|
||||
const el = page.locator('.imported-urls-order')
|
||||
expect(await el.textContent()).toBe('[success]')
|
||||
const mod = await viteServer.environments.$client.moduleGraph.getModuleByUrl(
|
||||
const mod = await viteServer.$client.moduleGraph.getModuleByUrl(
|
||||
'/imported-urls-order.js',
|
||||
)
|
||||
const importedModuleIds = [...mod.importedModules].map((m) => m.url)
|
||||
|
@ -12,7 +12,7 @@ async function runTest(userRunner) {
|
||||
})
|
||||
let mod
|
||||
if (userRunner) {
|
||||
const runner = await createServerModuleRunner(server.environments.$ssr, {
|
||||
const runner = await createServerModuleRunner(server.$ssr, {
|
||||
hmr: false,
|
||||
})
|
||||
mod = await runner.import('/src/network-imports.js')
|
||||
|
@ -13,7 +13,7 @@ const server = await createServer({
|
||||
},
|
||||
})
|
||||
|
||||
const runner = await createServerModuleRunner(server.environments.$ssr, {
|
||||
const runner = await createServerModuleRunner(server.$ssr, {
|
||||
sourcemapInterceptor: 'prepareStackTrace',
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user