feat(schema,vite): allow $client and $server vite env overrides (#22302)

This commit is contained in:
Daniel Roe 2023-07-24 18:32:12 +01:00 committed by GitHub
parent 4aadfac631
commit e2a6ad353d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 6 deletions

View File

@ -10,7 +10,7 @@ export default defineUntypedSchema({
* See https://vitejs.dev/config for more information.
* Please note that not all vite options are supported in Nuxt.
*
* @type {typeof import('../src/types/config').ViteConfig}
* @type {typeof import('../src/types/config').ViteConfig & { $client?: typeof import('../src/types/config').ViteConfig, $server?: typeof import('../src/types/config').ViteConfig }}
*/
vite: {
root: {

View File

@ -20,7 +20,7 @@ import { viteNodePlugin } from './vite-node'
import { createViteLogger } from './utils/logger'
export async function buildClient (ctx: ViteBuildContext) {
const clientConfig: ViteConfig = vite.mergeConfig(ctx.config, {
const clientConfig: ViteConfig = vite.mergeConfig(ctx.config, vite.mergeConfig({
configFile: false,
base: ctx.nuxt.options.dev
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
@ -82,7 +82,7 @@ export async function buildClient (ctx: ViteBuildContext) {
server: {
middlewareMode: true
}
} satisfies vite.InlineConfig)
} satisfies vite.InlineConfig, ctx.nuxt.options.vite.$client || {}))
clientConfig.customLogger = createViteLogger(clientConfig)

View File

@ -16,7 +16,7 @@ export async function buildServer (ctx: ViteBuildContext) {
const helper = ctx.nuxt.options.nitro.imports !== false ? '' : 'globalThis.'
const entry = ctx.nuxt.options.ssr ? ctx.entry : await resolvePath(resolve(ctx.nuxt.options.appDir, 'entry-spa'))
const nitroDependencies = await tryResolveModule('nitropack/package.json', ctx.nuxt.options.modulesDir).then(r => import(r!)).then(r => Object.keys(r.dependencies || {})).catch(() => [])
const serverConfig: ViteConfig = vite.mergeConfig(ctx.config, {
const serverConfig: ViteConfig = vite.mergeConfig(ctx.config, vite.mergeConfig({
configFile: false,
base: ctx.nuxt.options.dev
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
@ -100,7 +100,7 @@ export async function buildServer (ctx: ViteBuildContext) {
functions: ['defineComponent', 'defineAsyncComponent', 'defineNuxtLink', 'createClientOnly', 'defineNuxtPlugin', 'defineNuxtRouteMiddleware', 'defineNuxtComponent', 'useRuntimeConfig']
})
]
} satisfies vite.InlineConfig)
} satisfies vite.InlineConfig, ctx.nuxt.options.vite.$server || {}))
serverConfig.customLogger = createViteLogger(serverConfig)

View File

@ -56,6 +56,8 @@ export async function bundle (nuxt: Nuxt) {
allowDirs = allowDirs.filter(d => !d.startsWith(dir) || d === dir)
}
const { $client, $server, ...viteConfig } = nuxt.options.vite
const ctx: ViteBuildContext = {
nuxt,
entry,
@ -121,7 +123,7 @@ export async function bundle (nuxt: Nuxt) {
}
}
} satisfies ViteConfig,
nuxt.options.vite
viteConfig
)
}