From 17ca50354b88552e098db260e1d0b1e023199bd9 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sun, 30 Apr 2023 00:37:06 +0200 Subject: [PATCH] feat(vite): introduce `vite:configResolved` hook (#20411) --- docs/3.api/4.advanced/1.hooks.md | 1 + packages/schema/src/types/hooks.ts | 7 +++++++ packages/vite/src/client.ts | 2 ++ packages/vite/src/server.ts | 2 ++ 4 files changed, 12 insertions(+) diff --git a/docs/3.api/4.advanced/1.hooks.md b/docs/3.api/4.advanced/1.hooks.md index 412199d94f..2a3c60c801 100644 --- a/docs/3.api/4.advanced/1.hooks.md +++ b/docs/3.api/4.advanced/1.hooks.md @@ -71,6 +71,7 @@ Hook | Arguments | Description `schema:written` | - | Called after the schema is written. `vite:extend` | `viteBuildContext` | Allows to extend Vite default context. `vite:extendConfig` | `viteInlineConfig, env` | Allows to extend Vite default config. +`vite:configResolved` | `viteInlineConfig, env` | Allows to read the resolved Vite config. `vite:serverCreated` | `viteServer, env` | Called when the Vite server is created. `vite:compiled` | - | Called after Vite server is compiled. `webpack:config` | `webpackConfigs` | Called before configuring the webpack compiler. diff --git a/packages/schema/src/types/hooks.ts b/packages/schema/src/types/hooks.ts index eaa1c0a91d..f4f9725a65 100644 --- a/packages/schema/src/types/hooks.ts +++ b/packages/schema/src/types/hooks.ts @@ -290,6 +290,13 @@ export interface NuxtHooks { * @returns Promise */ 'vite:extendConfig': (viteInlineConfig: ViteConfig, env: { isClient: boolean, isServer: boolean }) => HookResult + /** + * Allows to read the resolved Vite config. + * @param viteInlineConfig The vite inline config object + * @param env Server or client + * @returns Promise + */ + 'vite:configResolved': (viteInlineConfig: Readonly, env: { isClient: boolean, isServer: boolean }) => HookResult /** * Called when the Vite server is created. * @param viteServer Vite development server diff --git a/packages/vite/src/client.ts b/packages/vite/src/client.ts index 2f2af64eac..d33e8d2e22 100644 --- a/packages/vite/src/client.ts +++ b/packages/vite/src/client.ts @@ -127,6 +127,8 @@ export async function buildClient (ctx: ViteBuildContext) { viteJsxPlugin(clientConfig.vueJsx) ) + await ctx.nuxt.callHook('vite:configResolved', clientConfig, { isClient: true, isServer: false }) + if (ctx.nuxt.options.dev) { // Dev const viteServer = await vite.createServer(clientConfig) diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index 6cfaa0e54d..e94daf7e3b 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -148,6 +148,8 @@ export async function buildServer (ctx: ViteBuildContext) { viteJsxPlugin(serverConfig.vueJsx) ) + await ctx.nuxt.callHook('vite:configResolved', serverConfig, { isClient: false, isServer: true }) + const onBuild = () => ctx.nuxt.callHook('vite:compiled') // Production build