Skip to content

Commit

Permalink
feat: async createEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Apr 8, 2024
1 parent 70731ce commit d15a157
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
7 changes: 5 additions & 2 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ export interface BuildEnvironmentOptions {
/**
* create the Build Environment instance
*/
createEnvironment?: (builder: ViteBuilder, name: string) => BuildEnvironment
createEnvironment?: (
builder: ViteBuilder,
name: string,
) => Promise<BuildEnvironment> | BuildEnvironment
}

export interface BuildOptions extends BuildEnvironmentOptions {
Expand Down Expand Up @@ -1506,7 +1509,7 @@ export async function createViteBuilder(
const environmentConfig = await resolveConfig(environmentOptions)
const environmentBuilder = { ...builder, config: environmentConfig }

const environment = createEnvironment(environmentBuilder, name)
const environment = await createEnvironment(environmentBuilder, name)
environments[name] = environment
}

Expand Down
10 changes: 8 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ export interface DevEnvironmentOptions {
/**
* create the Dev Environment instance
*/
createEnvironment?: (server: ViteDevServer, name: string) => DevEnvironment
createEnvironment?: (
server: ViteDevServer,
name: string,
) => Promise<DevEnvironment> | DevEnvironment

/**
* For environments that support a full-reload, like the client, we can short-circuit when
Expand All @@ -204,7 +207,10 @@ export type ResolvedDevEnvironmentOptions = Required<
> & {
// TODO: Should we set the default at config time? For now, it is defined on server init
createEnvironment:
| ((server: ViteDevServer, name: string) => DevEnvironment)
| ((
server: ViteDevServer,
name: string,
) => Promise<DevEnvironment> | DevEnvironment)
| undefined
}

Expand Down
12 changes: 7 additions & 5 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,16 +778,18 @@ export async function _createServer(
((server: ViteDevServer, name: string) =>
new DevEnvironment(server, name, { hot: ws }))

environments.client = client_createEnvironment(server, 'client')
environments.client = await client_createEnvironment(server, 'client')

const ssr_createEnvironment =
config.environments.ssr?.dev?.createEnvironment ??
((server: ViteDevServer, name: string) =>
createNodeDevEnvironment(server, name, { hot: ssrHotChannel }))

environments.ssr = ssr_createEnvironment(server, 'ssr')
environments.ssr = await ssr_createEnvironment(server, 'ssr')

Object.entries(config.environments).forEach(([name, EnvironmentOptions]) => {
for (const [name, EnvironmentOptions] of Object.entries(
config.environments,
)) {
// TODO: move client and ssr inside the loop?
if (name !== 'client' && name !== 'ssr') {
const createEnvironment =
Expand All @@ -796,9 +798,9 @@ export async function _createServer(
new DevEnvironment(server, name, {
hot: ws, // TODO: what should we use here?
}))
environments[name] = createEnvironment(server, name)
environments[name] = await createEnvironment(server, name)
}
})
}

if (!middlewareMode) {
exitProcess = async () => {
Expand Down

0 comments on commit d15a157

Please sign in to comment.