diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 91e817cda83c00..0d1c210c7e3516 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -1605,6 +1605,7 @@ export async function createBuilder( if (useLegacyBuilder) { await setupEnvironment(config.build.ssr ? 'ssr' : 'client', config) } else { + const environmentConfigs: [string, ResolvedConfig][] = [] for (const environmentName of Object.keys(config.environments)) { // 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 @@ -1647,9 +1648,14 @@ export async function createBuilder( patchPlugins, ) } - - await setupEnvironment(environmentName, environmentConfig) + environmentConfigs.push([environmentName, environmentConfig]) } + await Promise.all( + environmentConfigs.map( + async ([environmentName, environmentConfig]) => + await setupEnvironment(environmentName, environmentConfig), + ), + ) } return builder diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 78adcdee296dfe..101562320c5c46 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -509,22 +509,24 @@ export async function _createServer( const environments: Record = {} - for (const [name, environmentOptions] of Object.entries( - config.environments, - )) { - environments[name] = await environmentOptions.dev.createEnvironment( - name, - config, - { - ws, - }, - ) - } + await Promise.all( + Object.entries(config.environments).map( + async ([name, environmentOptions]) => { + const environment = await environmentOptions.dev.createEnvironment( + name, + config, + { + ws, + }, + ) + environments[name] = environment - for (const environment of Object.values(environments)) { - const previousInstance = options.previousEnvironments?.[environment.name] - await environment.init({ watcher, previousInstance }) - } + const previousInstance = + options.previousEnvironments?.[environment.name] + await environment.init({ watcher, previousInstance }) + }, + ), + ) // Backward compatibility