fix(nuxi): preview nitro build with custom dir config (#18882)

This commit is contained in:
xjccc 2023-04-07 21:19:53 +08:00 committed by GitHub
parent 0c9823eb87
commit 9fd398216c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import { execa } from 'execa'
import { setupDotenv } from 'c12' import { setupDotenv } from 'c12'
import { resolve } from 'pathe' import { resolve } from 'pathe'
import consola from 'consola' import consola from 'consola'
import { loadKit } from '../utils/kit'
import { defineNuxtCommand } from './index' import { defineNuxtCommand } from './index'
@ -16,8 +17,13 @@ export default defineNuxtCommand({
async invoke (args) { async invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production' process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.') const rootDir = resolve(args._[0] || '.')
const { loadNuxtConfig } = await loadKit(rootDir)
const config = await loadNuxtConfig({ cwd: rootDir })
const nitroJSONPaths = ['.output/nitro.json', 'nitro.json'].map(p => resolve(rootDir, p)) const resolvedOutputDir = resolve(config.srcDir || rootDir, config.nitro.srcDir || 'server', config.nitro.output?.dir || '.output', 'nitro.json')
const defaultOutput = resolve(rootDir, '.output', 'nitro.json') // for backwards compatibility
const nitroJSONPaths = [resolvedOutputDir, defaultOutput]
const nitroJSONPath = nitroJSONPaths.find(p => existsSync(p)) const nitroJSONPath = nitroJSONPaths.find(p => existsSync(p))
if (!nitroJSONPath) { if (!nitroJSONPath) {
consola.error('Cannot find `nitro.json`. Did you run `nuxi build` first? Search path:\n', nitroJSONPaths) consola.error('Cannot find `nitro.json`. Did you run `nuxi build` first? Search path:\n', nitroJSONPaths)

View File

@ -183,6 +183,9 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
} }
}) })
// Resolve user-provided paths
nitroConfig.srcDir = resolve(nuxt.options.rootDir, nuxt.options.srcDir, nitroConfig.srcDir!)
// Add head chunk for SPA renders // Add head chunk for SPA renders
const head = createHeadCore() const head = createHeadCore()
head.push(nuxt.options.app.head) head.push(nuxt.options.app.head)