fix(vite): ignore and warn if vite config file exists (#21588)

This commit is contained in:
Nozomu Ikuta 2023-06-16 23:19:53 +09:00 committed by GitHub
parent 3c88b50bbc
commit 48c2b458a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import { createViteLogger } from './utils/logger'
export async function buildClient (ctx: ViteBuildContext) { export async function buildClient (ctx: ViteBuildContext) {
const clientConfig: ViteConfig = vite.mergeConfig(ctx.config, { const clientConfig: ViteConfig = vite.mergeConfig(ctx.config, {
configFile: false,
base: ctx.nuxt.options.dev base: ctx.nuxt.options.dev
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir) ? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
: './', : './',

View File

@ -18,6 +18,7 @@ export async function buildServer (ctx: ViteBuildContext) {
const helper = ctx.nuxt.options.nitro.imports !== false ? '' : 'globalThis.' 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 entry = ctx.nuxt.options.ssr ? ctx.entry : await resolvePath(resolve(ctx.nuxt.options.appDir, 'entry-spa'))
const serverConfig: ViteConfig = vite.mergeConfig(ctx.config, { const serverConfig: ViteConfig = vite.mergeConfig(ctx.config, {
configFile: false,
base: ctx.nuxt.options.dev base: ctx.nuxt.options.dev
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir) ? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
: undefined, : undefined,

View File

@ -1,6 +1,6 @@
import { existsSync } from 'node:fs' import { existsSync } from 'node:fs'
import * as vite from 'vite' import * as vite from 'vite'
import { dirname, join, resolve } from 'pathe' import { basename, dirname, join, resolve } from 'pathe'
import type { Nuxt, ViteConfig } from '@nuxt/schema' import type { Nuxt, ViteConfig } from '@nuxt/schema'
import { addVitePlugin, isIgnored, logger, resolvePath } from '@nuxt/kit' import { addVitePlugin, isIgnored, logger, resolvePath } from '@nuxt/kit'
import replace from '@rollup/plugin-replace' import replace from '@rollup/plugin-replace'
@ -8,6 +8,7 @@ import { sanitizeFilePath } from 'mlly'
import { withoutLeadingSlash } from 'ufo' import { withoutLeadingSlash } from 'ufo'
import { filename } from 'pathe/utils' import { filename } from 'pathe/utils'
import { resolveTSConfig } from 'pkg-types' import { resolveTSConfig } from 'pkg-types'
import { consola } from 'consola'
import { buildClient } from './client' import { buildClient } from './client'
import { buildServer } from './server' import { buildServer } from './server'
import virtual from './plugins/virtual' import virtual from './plugins/virtual'
@ -25,6 +26,13 @@ export interface ViteBuildContext {
} }
export async function bundle (nuxt: Nuxt) { export async function bundle (nuxt: Nuxt) {
// https://github.com/vitejs/vite/blob/8fe69524d25d45290179175ba9b9956cbce87a91/packages/vite/src/node/constants.ts#L38
const viteConfigPrefix = resolve(nuxt.options.rootDir, 'vite.config')
const viteConfigFile = await resolvePath(viteConfigPrefix).catch(() => null)
if (viteConfigFile && viteConfigFile !== viteConfigPrefix) {
consola.warn(`Using \`${basename(viteConfigFile)}\` is not supported together with Nuxt. Use \`options.vite\` instead. You can read more in \`https://nuxt.com/docs/api/configuration/nuxt-config#vite\`.`)
}
const useAsyncEntry = nuxt.options.experimental.asyncEntry || const useAsyncEntry = nuxt.options.experimental.asyncEntry ||
(nuxt.options.vite.devBundler === 'vite-node' && nuxt.options.dev) (nuxt.options.vite.devBundler === 'vite-node' && nuxt.options.dev)
const entry = await resolvePath(resolve(nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')) const entry = await resolvePath(resolve(nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry'))