fix: impôrt, wrap buildNuxt and make name internal

This commit is contained in:
Julien Huang 2025-01-09 12:17:57 +01:00
parent 8393d6701d
commit a3487893a3
4 changed files with 13 additions and 10 deletions

View File

@ -3,6 +3,7 @@ import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import type { Nuxt } from '@nuxt/schema' import type { Nuxt } from '@nuxt/schema'
import { importModule, tryImportModule } from '../internal/esm' import { importModule, tryImportModule } from '../internal/esm'
import type { LoadNuxtConfigOptions } from './config' import type { LoadNuxtConfigOptions } from './config'
import { asyncNameStorage } from '../utils'
export interface LoadNuxtOptions extends LoadNuxtConfigOptions { export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
/** Load nuxt with development mode */ /** Load nuxt with development mode */
@ -81,5 +82,5 @@ export async function buildNuxt (nuxt: Nuxt): Promise<any> {
// Nuxt 2 // Nuxt 2
const { build } = await tryImportModule<{ build: any }>('nuxt-edge', { paths: rootDir }) || await importModule<{ build: any }>('nuxt', { paths: rootDir }) const { build } = await tryImportModule<{ build: any }>('nuxt-edge', { paths: rootDir }) || await importModule<{ build: any }>('nuxt', { paths: rootDir })
return build(nuxt) return asyncNameStorage.run(nuxt.__name, () => build(nuxt))
} }

View File

@ -6,5 +6,8 @@ export function toArray<T> (value: T | T[]): T[] {
} }
export const MODE_RE = /\.(server|client)(\.\w+)*$/ export const MODE_RE = /\.(server|client)(\.\w+)*$/
/**
* @internal
* async local storage for the name of the current nuxt instance
*/
export const asyncNameStorage = new AsyncLocalStorage<string>() export const asyncNameStorage = new AsyncLocalStorage<string>()

View File

@ -46,11 +46,11 @@ import { ComposableKeysPlugin } from './plugins/composable-keys'
import { resolveDeepImportsPlugin } from './plugins/resolve-deep-imports' import { resolveDeepImportsPlugin } from './plugins/resolve-deep-imports'
import { PrehydrateTransformPlugin } from './plugins/prehydrate' import { PrehydrateTransformPlugin } from './plugins/prehydrate'
import { VirtualFSPlugin } from './plugins/virtual' import { VirtualFSPlugin } from './plugins/virtual'
import { randomUUID } from 'node:crypto' import { randomUUID } from 'uncrypto'
export function createNuxt (options: NuxtOptions): Nuxt { export function createNuxt (options: NuxtOptions): Nuxt {
const hooks = createHooks<NuxtHooks>() const hooks = createHooks<NuxtHooks>()
const name = randomUUID() // TODO find project name in package json instead const name = randomUUID()
const nuxt: Nuxt = { const nuxt: Nuxt = {
_version: version, _version: version,
options, options,
@ -62,7 +62,7 @@ export function createNuxt (options: NuxtOptions): Nuxt {
close: () => hooks.callHook('close', nuxt), close: () => hooks.callHook('close', nuxt),
vfs: {}, vfs: {},
apps: {}, apps: {},
name __name: name,
} }
hooks.hookOnce('close', () => { hooks.removeAllHooks() }) hooks.hookOnce('close', () => { hooks.removeAllHooks() })

View File

@ -79,12 +79,11 @@ export interface NuxtApp {
} }
export interface Nuxt { export interface Nuxt {
/**
* The name of the Nuxt project, this can be useful for build time debugging and mono-repos.
* Defaults to a randomUUID
*/
name?: string
// Private fields. // Private fields.
/**
* The name of the Nuxt instance, this can be useful for build time debugging and mono-repos.
*/
__name: string
_version: string _version: string
_ignore?: Ignore _ignore?: Ignore
_dependencies?: Set<string> _dependencies?: Set<string>